Skip to main content

PHP Collective

Questions

Browse questions with relevant PHP tags

1,464,471 questions

2769 votes
27 answers
2.2m views

How can I prevent SQL injection in PHP?

If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example: $unsafe_variable = $_POST['user_input']; ...
2650 votes
36 answers
6.8m views

How do I check if a string contains a specific word?

Consider: $a = 'How are you?'; if ($a contains 'are') echo 'true'; Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?
5135 votes
24 answers
851k views

Reference Guide: What does this symbol mean in PHP? (PHP Syntax)

What is this? This is a collection of questions that come up now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list. This ...
2853 votes
31 answers
2.1m views

How do I get a YouTube video thumbnail from the YouTube API?

If I have a YouTube video URL, is there any way to use PHP and cURL to get the associated thumbnail from the YouTube API?
CodeOverload's user avatar
  • 48.8k
3111 votes
26 answers
3.9m views

Deleting an element from an array in PHP

Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element? I thought that setting it to null would do it, but apparently it does ...
Ben's user avatar
  • 68.9k
2325 votes
32 answers
482k views

How do you parse and process HTML/XML in PHP?

How can one parse HTML/XML and extract information from it?
1704 votes
37 answers
1.0m views

str_starts_with and str_ends_with functions in PHP

How can I write two functions that would take a string and return if it starts with the specified character/string or ends with it? For example: $str = '|apples}'; echo startsWith($str, '|'); //...
Ali's user avatar
  • 268k
1031 votes
62 answers
1.9m views

PHP random string generator

I'm trying to create a randomized string in PHP, and I get absolutely no output with this: <?php function RandomString() { $characters = '...
Captain Lightning's user avatar
2050 votes
27 answers
3.9m views

How do I get PHP errors to display?

I have checked my PHP ini file (php.ini) and display_errors is set and also error reporting is E_ALL. I have restarted my Apache webserver. I have even put these lines at the top of my script, and it ...
Abs's user avatar
  • 58.2k
2241 votes
24 answers
852k views

When should I use 'self' over '$this'?

In PHP 5, what is the difference between using self and $this? When is each appropriate?
Casey Watson's user avatar
  • 52.9k
1451 votes
39 answers
2.3m views

How to get the client IP address in PHP

How can I get the client IP address using PHP? I want to keep record of the user who logged into my website through his/her IP address.
Anup Prakash's user avatar
  • 14.8k
1605 votes
35 answers
4.0m views

How do I make a redirect in PHP?

Is it possible to redirect a user to a different page through the use of PHP? Say the user goes to www.example.com/page.php and I want to redirect them to www.example.com/index.php, how would I do so ...
Sam's user avatar
  • 19.2k
1359 votes
37 answers
2.2m views

Get the first element of an array

I have an array: array( 4 => 'apple', 7 => 'orange', 13 => 'plum' ) I would like to get the first element of this array. Expected result: string apple One requirement: it cannot be ...
hsz's user avatar
  • 153k
1324 votes
40 answers
687k views

Enumerations on PHP

I know that PHP doesn't yet have native Enumerations. But I have become accustomed to them from the Java world. I would love to use enums as a way to give predefined values which IDEs' auto-completion ...
Henrik Paul's user avatar
1097 votes
47 answers
3.3m views

How do I get the current date and time in PHP?

Which PHP function can return the current date/time?
user avatar
2738 votes
14 answers
263k views

Why shouldn't I use mysql_* functions in PHP?

What are the technical reasons for why one shouldn't use mysql_* functions? (e.g. mysql_query(), mysql_connect() or mysql_real_escape_string())? Why should I use something else even if they work on ...
Madara's Ghost's user avatar
1725 votes
30 answers
962k views

How Can I add HTML And CSS Into PDF [closed]

I have an HTML (not XHTML) document that renders fine in Firefox 3 and IE 7. It uses fairly basic CSS to style it and renders fine in HTML. I'm now after a way of converting it to PDF. I have tried:...
1292 votes
39 answers
286k views

Reference - What does this error mean in PHP?

What is this? This is a number of answers about warnings, errors, and notices you might encounter while programming PHP and have no clue how to fix them. This is also a Community Wiki, so everyone is ...
1452 votes
30 answers
724k views

Difference between require, include, require_once and include_once?

In PHP: When should I use require vs. include? When should I use require_once vs. include_once?
Scott B's user avatar
  • 40.4k
940 votes
47 answers
358k views

How to check if PHP array is associative or sequential?

PHP treats all arrays as associative, so there aren't any built in functions. Can anyone recommend a fairly efficient way to check if an array "is a list" (contains only numeric keys ...
877 votes
41 answers
1.4m views

How do I get the query builder to output its raw SQL query as a string?

Given the following code: DB::table('users')->get(); I want to get the raw SQL query string that the database query builder above will generate. In this example, it would be SELECT * FROM users. ...
meiryo's user avatar
  • 11.8k
1373 votes
29 answers
2.2m views

"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP

I'm running a PHP script and continue to receive errors like: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php on line 10 Notice: Undefined index: my_index C:\wamp\www\...
1024 votes
32 answers
2.0m views

Convert a PHP object to an associative array

I'm integrating an API to my website which works with data stored in objects while my code is written using arrays. I'd like a quick-and-dirty function to convert an object to an array.
Haroldo's user avatar
  • 37.5k
726 votes
48 answers
542k views

dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac

I installed node using homebrew (Mojave), afterwards php stoped working and if I try to run php -v I get this error: php -v dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib ...
petekaner's user avatar
  • 9,001
975 votes
27 answers
4.1m views

Get the full URL in PHP

I use this code to get the full URL: $actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; The problem is that I use some masks in my .htaccess, so what we see in the URL is not always ...
DiegoP.'s user avatar
  • 45.8k
1079 votes
27 answers
1.4m views

Remove empty array elements

Some elements in my array are empty strings from users. $linksArray still has empty elements after the following: foreach($linksArray as $link) { if($link == '') { unset($link); } }...
Will's user avatar
  • 11.3k
1193 votes
21 answers
1.3m views

PHP array delete by value (not key)

I have a PHP array like so: $messages = [312, 401, 1599, 3, ...]; Given that the values in the array are unique, how can I delete the element with a given value (without knowing its key)?
Adam Strudwick's user avatar
883 votes
33 answers
880k views

How can I get a file's extension in PHP?

This is a question you can read everywhere on the web with various answers: $ext = end(explode('.', $filename)); $ext = substr(strrchr($filename, '.'), 1); $ext = substr($filename, strrpos($filename, '...
Bite code's user avatar
  • 600k
811 votes
36 answers
1.2m views

Finding the number of days between two dates

How to find number of days between two dates using PHP?
PHP Ferrari's user avatar
  • 15.7k
821 votes
39 answers
2.3m views

Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted

I have a bunch of client point of sale (POS) systems that periodically send new sales data to one centralized database, which stores the data into one big database for report generation. The client ...
ArcticZero's user avatar
  • 8,251
1423 votes
18 answers
1.2m views

Sort a 2d array by a column value

How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be. Array ( [0] => Array ( [...
stef's user avatar
  • 28k
1007 votes
31 answers
778k views

mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource

I am trying to select data from a MySQL table, but I get one of the following error messages: mysql_fetch_array() expects parameter 1 to be resource, boolean given This is my code: $username = $...
iamjonesy's user avatar
  • 25.2k
832 votes
33 answers
1.2m views

How to calculate the difference between two dates using PHP?

I have two dates of the form: Start Date: 2007-03-24 End Date: 2009-06-26 Now I need to find the difference between these two in the following form: 2 years, 3 months and 2 days How can I do this ...
user avatar
970 votes
26 answers
3.1m views

How do I convert a string to a number in PHP?

I want to convert these types of values, '3', '2.34', '0.234343', etc. to a number. In JavaScript we can use Number(), but is there any similar method available in PHP? Input Output '2' ...
Sara's user avatar
  • 14.7k
1161 votes
20 answers
1.6m views

Returning JSON from a PHP Script

I want to return JSON from a PHP script. Do I just echo the result? Do I have to set the Content-Type header?
Scott Nicol's user avatar
  • 11.9k
658 votes
41 answers
747k views

How can I get useful error messages in PHP?

Quite often I will try and run a PHP script and just get a blank screen back. No error message; just an empty screen. The cause might have been a simple syntax error (wrong bracket, missing semicolon),...
Candidasa's user avatar
  • 8,740
830 votes
16 answers
2.1m views

How to fix "Headers already sent" error in PHP

When running my script, I am getting several errors like this: Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23 ...
1145 votes
16 answers
645k views

What is the difference between public, private, and protected?

When and why should I use public, private, and protected functions and variables inside a class? What is the difference between them? Examples: // Public public $variable; public function ...
Adam Halasz's user avatar
  • 58.5k
1163 votes
18 answers
1.6m views

How do I use PHP to get the current year?

I want to put a copyright notice in the footer of a web site, but I think it's incredibly tacky for the year to be outdated. How would I make the year update automatically with PHP?
JD Graffam's user avatar
  • 11.7k
1170 votes
18 answers
683k views

What is stdClass in PHP?

Please define what stdClass is.
Keira Nighly's user avatar
  • 15.5k
812 votes
27 answers
902k views

Pretty-Printing JSON with PHP

I'm building a PHP script that feeds JSON data to another script. My script builds data into a large associative array, and then outputs the data using json_encode. Here is an example script: $data = ...
Zach Rattner's user avatar
  • 21.5k
1184 votes
18 answers
1.2m views

How do I expire a PHP session after 30 minutes?

I need to keep a session alive for 30 minutes and then destroy it.
Tom's user avatar
  • 34.5k
805 votes
24 answers
1.9m views

Show a number to two decimal places

What's the correct way to round a PHP string to two decimal places? $number = "520"; // It's a string from a database $formatted_number = round_to_2dp($number); echo $formatted_number; The output ...
Rich Bradshaw's user avatar
1287 votes
17 answers
708k views

How can I sanitize user input with PHP?

Is there a catchall function somewhere that works well for sanitizing user input for SQL injection and XSS attacks, while still allowing certain types of HTML tags?
Brent's user avatar
  • 23.7k
827 votes
25 answers
663k views

Get first key in a (possibly) associative array?

What's the best way to determine the first key in a possibly associative array? My first thought it to just foreach the array and then immediately breaking it, like this: foreach ($an_array as $key =&...
Alex S's user avatar
  • 26.2k
1104 votes
15 answers
1.3m views

Where can I find php.ini?

Today I needed to install the IBM DB2 library. I went through all the steps up to make install, and I found ibm_db2.so in $PHP_HOME/lib/extensions/somecomplicatedname/ibm_db2.so. The great catch is ...
necromancer's user avatar
  • 24.7k
1371 votes
14 answers
221k views

UTF-8 all the way through

I'm setting up a new server and want to support UTF-8 fully in my web application. I have tried this in the past on existing servers and always seem to end up having to fall back to ISO-8859-1. Where ...
mercutio's user avatar
  • 22.6k
332 votes
71 answers
717k views

Amazon S3 - How to fix 'The request signature we calculated does not match the signature' error?

I have searched on the web for over two days now, and probably have looked through most of the online documented scenarios and workarounds, but nothing worked for me so far. I am on AWS SDK for PHP V2....
Joseph Lam's user avatar
  • 6,229
2288 votes
7 answers
463k views

How does PHP 'foreach' actually work?

Let me prefix this by saying that I know what foreach is, does and how to use it. This question concerns how it works under the bonnet, and I don't want any answers along the lines of "this is how you ...
DaveRandom's user avatar
  • 88.8k
575 votes
34 answers
575k views

How to get the last element of an array without deleting it?

Ok, I know all about array_pop(), but that deletes the last element. How to get the last element of an array without deleting it? Here's a bonus: $array = array('a' => 'a', 'b' => 'b', 'c' =>...
Theodore R. Smith's user avatar


15 30 50 per page
1
2 3 4 5
29290