Click to See Complete Forum and Search --> : $_POST[value] ..or.. $_POST['value'] ?


callumd
08-29-2006, 09:32 PM
Hello,

I'm trying to learn PHP at the moment, and one thing that I keep encountering with no consistency, and can find no explanation on, is why do some PHP sources use a single quotation mark around the name of the value being taken from $_POST, such as:

$_POST['value']

Where as others don't use it? Both seem to work ok..

Thanks.

Callum.

balloonbuffoon
08-29-2006, 09:46 PM
echo $_POST[value]; In this case, the key you're using, "value", has no spaces or punctuation so you are allowed to go without quotes. They are essential however, if you have a space or punctuation in the key:
echo $_POST["my.value 1"];Still, imo, it is best to always use quotes, although I'm not sure if there is any true advantage to it.

--Steve

NogDog
08-29-2006, 09:52 PM
http://www.php.net/manual/en/language.types.array.php#language.types.array.foo-bar

The gist of this is that $foo[bar] will work if "bar" has not yet been defined elsewhere as a constant, as it will then become an undeclared constant whose value is the same as its name. This is bad usage, though, because you may change your code, or re-use this code somewhere else where "bar" is defined as a constant, and suddenly your code stops working and you'll spend hours trying to figure out why.

chazzy
08-29-2006, 10:26 PM
you can also think of it like this...


//before define
echo $_POST[bar];
//echo's the value of the field named "bar" and throws a warning
echo $_POST['bob'];
//echo's the value of the field named "bob", no warning.
define('bar','bob'); //this is it being defined
echo bar; //echos bob
echo $_POST[bar];
//echo's the value of the form field "bob" with no warning since the value of bar was defined.
echo $_POST['bob'];
//acts the same as above.

Basically using it without the quotes requires it to be defined and throws a warning if it's not defined (the warning says "value whatever no defined, defaulting to whatever.." meaning that a defined constant's value is itself before being defined.

callumd
08-29-2006, 11:53 PM
Thank you everyone, that makes it clearer for me.

The Little Guy
08-30-2006, 10:21 AM
Some times if you use quotes, you will encounter an error.

Won't work:
"SELECT * from db WHERE id='$_POST['cat']'";

Will work:
"SELECT * from db WHERE id='$_POST[cat]'";

NogDog
08-30-2006, 10:28 AM
Some times if you use quotes, you will encounter an error.

Won't work:
"SELECT * from db WHERE id='$_POST['cat']'";

Will work:
"SELECT * from db WHERE id='$_POST[cat]'";
Better would be:

// "complex" variable notation:
$query = "SELECT * FROM db WHERE id='{$_POST['cat']}'";
// or concatenation:
$query = "SELECT * FROM db WHERE id='".$_POST['cat']."'";

chazzy
08-30-2006, 12:39 PM
Some times if you use quotes, you will encounter an error.

Won't work:
"SELECT * from db WHERE id='$_POST['cat']'";

Will work:
"SELECT * from db WHERE id='$_POST[cat]'";

I suppose this is what happens when you don't follow examples posted earlier in the thread.

if you put
define('cat','dog');
before
"SELECT * from db WHERE id='$_POST['cat']'";

and before
"SELECT * from db WHERE id='$_POST[cat]'";

then which will work? the point is that when you skip the quotes, you tell php to find the constant, when you use quotes you tell php to use the string constant.