$_POST["tags"] or $_POST['tags']?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

$_POST["tags"] or $_POST['tags']?

Post by kkonline »

What is the difference if i write

Code: Select all

$_POST["tags"]
and

Code: Select all

$_POST['tags']
?
which is better or which is preferred?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

In the way the code is executed, it doesn't make a difference.

But, how it differs:

PHP will try to interpret any variables and other characters when the code is between double quotes. So, use single quotes when you don't have any variables or other characters like new lines in your string. Using single quotes lets PHP know that it doesn't need to interpret anything, and is probably a bit faster.

Which is preferred? That's your own call...
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

scottayy wrote:In the way the code is executed, it doesn't make a difference.

But, how it differs:

PHP will try to interpret any variables and other characters when the code is between double quotes. So, use single quotes when you don't have any variables or other characters like new lines in your string. Using single quotes lets PHP know that it doesn't need to interpret anything, and is probably a bit faster.

Which is preferred? That's your own call...
Ok ,
now if i am using

Code: Select all

$_POST['name']
then as name is a variable so i SHOULD use DOUBLE QUOTES ???
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

kkonline wrote:
scottayy wrote:In the way the code is executed, it doesn't make a difference.

But, how it differs:

PHP will try to interpret any variables and other characters when the code is between double quotes. So, use single quotes when you don't have any variables or other characters like new lines in your string. Using single quotes lets PHP know that it doesn't need to interpret anything, and is probably a bit faster.

Which is preferred? That's your own call...
Ok ,
now if i am using

Code: Select all

$_POST['name']
then as name is a variable so i SHOULD use DOUBLE QUOTES ???
No, 'name' isn't variable, it's a static string. This would be variable:

Code: Select all

$name = "name";
echo $_POST[$name];

//or even

echo $_POST["foo_$name_bar"]; //foo_name_bar
Post Reply