Questions tagged [php]

PHP is an open-source, multi-paradigm, dynamically-typed, and interpreted scripting language designed initially for server-side web development. Use this tag for questions about programming in the PHP language.

Filter by
Sorted by
Tagged with
5056 votes
25 answers

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

What is this? This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list. Why ...
3025 votes
25 answers

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
  • 66.9k
2780 votes
32 answers

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
  • 47.4k
2773 votes
27 answers

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']; ...
2707 votes
14 answers

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
2657 votes
36 answers

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')?
2300 votes
31 answers

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

How can one parse HTML/XML and extract information from it?
2264 votes
7 answers

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
2212 votes
24 answers

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
  • 51.6k
1977 votes
27 answers

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
  • 56.2k
1721 votes
30 answers

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:...
1691 votes
34 answers

startsWith() and endsWith() 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
  • 262k
1552 votes
35 answers

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 and I want to redirect them to, how would I do so ...
Sam's user avatar
  • 18.5k
1426 votes
28 answers

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
  • 38.9k
1418 votes
36 answers

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.4k
1383 votes
17 answers

How to Sort a Multi-dimensional Array by 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
  • 26.9k
1354 votes
29 answers

"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\...
1343 votes
13 answers

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.2k
1333 votes
11 answers

How do you use bcrypt for hashing passwords in PHP? [duplicate]

Every now and then I hear the advice "Use bcrypt for storing passwords in PHP, bcrypt rules". But what is bcrypt? PHP doesn't offer any such functions, Wikipedia babbles about a file-encryption ...
Vilx-'s user avatar
  • 105k
1314 votes
39 answers

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
  • 148k
1309 votes
39 answers

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
  • 66.9k
1270 votes
38 answers

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 ...
1267 votes
15 answers

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.4k
1247 votes
14 answers

Secure hash and salt for PHP passwords

It is currently said that MD5 is partially unsafe. Taking this into consideration, I'd like to know which mechanism to use for password protection. This question, Is “double hashing” a password less ...
luiscubal's user avatar
  • 24.8k
1185 votes
4 answers

What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

I have tried reading some articles, but I am not very clear on the concepts yet. Would someone like to take a shot at explaining to me what these technologies are: Long Polling Server-Sent Events ...
user1437328's user avatar
  • 15.6k
1168 votes
17 answers

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
  • 33.7k
1159 votes
18 answers

What is stdClass in PHP?

Please define what stdClass is.
Keira Nighly's user avatar
  • 15.3k
1145 votes
21 answers

PHP array delete by value (not key)

I have a PHP array as follows: $messages = [312, 401, 1599, 3, ...]; I want to delete the element containing the value $del_val (for example, $del_val=401), but I don't know its key. This might help:...
Adam Strudwick's user avatar
1131 votes
16 answers

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
  • 57.5k
1124 votes
20 answers

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.5k
1118 votes
17 answers

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.3k
1081 votes
14 answers

Detecting request type in PHP (GET, POST, PUT or DELETE)

How can I detect which request type was used (GET, POST, PUT or DELETE) in PHP?
UnkwnTech's user avatar
  • 88.2k
1070 votes
14 answers

Where can I find php.ini? [duplicate]

Today I needed to install IBM DB2 library. I went through all the steps up to make install, and I found in $PHP_HOME/lib/extensions/somecomplicatedname/ The great catch is the ...
necromancer's user avatar
  • 23.9k
1054 votes
42 answers

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

Which PHP function can return the current date/time?
user avatar
1028 votes
27 answers

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
  • 10.8k
1005 votes
69 answers

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
1002 votes
31 answers

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
  • 24.8k
986 votes
34 answers

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
  • 36.6k
975 votes
27 answers

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 ...
DiegoP.'s user avatar
  • 45.2k
932 votes
5 answers

What is thread safe or non-thread safe in PHP?

I saw different binaries for PHP, like non-thread or thread safe? What does this mean? What is the difference between these packages?
O..'s user avatar
  • 10.9k
923 votes
7 answers

What is the difference between single-quoted and double-quoted strings in PHP?

I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes. I just know in .NET, or the C language, if it is in a single quote, that means it ...
rob waminal's user avatar
  • 18.1k
918 votes
35 answers

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.1k
914 votes
60 answers

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 ...
855 votes
32 answers

How to 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
  • 580k
831 votes
11 answers

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 ...
827 votes
40 answers

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.2k
826 votes
7 answers

Remove trailing delimiting character from a delimited string

What is fastest way to remove the last character from a string? I have a string like a,b,c,d,e, I would like to remove the last ',' and get the remaining string back: OUTPUT: a,b,c,d,e What is ...
I-M-JM's user avatar
  • 15.7k
818 votes
25 answers

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
  • 25.3k
817 votes
34 answers

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
806 votes
17 answers

How do I implement basic "Long Polling"?

I can find lots of information on how Long Polling works (For example, this, and this), but no simple examples of how to implement this in code. All I can find is cometd, which relies on the Dojo JS ...
dbr's user avatar
  • 166k

2 3 4 5