Re: [PHP] Using ' to access session variables?
If used outside any strings (like in your $query example) $_SESSION[foo] -- here foo technically is a constant and php ought to produce a notice $_SESSION['foo'] -- is the correct way as there is no doubt that foo is a string When used inside a string the rules are slightly altered @see http://www.php.net/manual/en/language.types.array.php section Array do's and don'ts Richard Friday, April 16, 2004, 6:32:11 PM, you wrote: > Whats the difference between $_SESSION[foo] and $_SESSION['foo'] I have been > using the 's but they seem to be unecessary? > I run into trouble if I try something like: > $query = " select * from table where (test.id = '$_SESSION['foo']') "; > but the following works: > $query = " select * from table where (test.id = '$_SESSION[foo]') "; > I know there is some way to properly escape characters but is there any > reason why I shouldn't just omit the 's? > Thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using ' to access session variables?
Michal Migurski wrote: Whats the difference between $_SESSION[foo] and $_SESSION['foo'] I have been using the 's but they seem to be unecessary? Use the single-quotes -- array references often work without them, but the potential for conflict with constants and future PHP incompatibility is a possibility. If you (or PHP) had a constant foo defined, it would behave unexpectedly. I run into trouble if I try something like: $query = " select * from table where (test.id = '$_SESSION['foo']') "; but the following works: $query = " select * from table where (test.id = '$_SESSION[foo]') "; http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing // Works but note that this works differently outside string-quotes echo "A banana is $fruits[banana]."; // Works echo "A banana is {$fruits['banana']}."; // Works but PHP looks for a constant named banana first // as described below. echo "A banana is {$fruits[banana]}."; // Won't work, use braces. This results in a parse error. echo "A banana is $fruits['banana']."; - michal migurski- contact info and pgp key: sf/cahttp://mike.teczno.com/contact.html Or you could just use single quotes and concatenate. echo 'A banana is '.$fruits['banana'].'.'; It may look bad here, but it's just fine when syntaxt highlighted. In fact, more syntax highlighters will work on that than the in-string version. IMHO, it's also more readable. That being said, it's also slightly faster as PHP doesn't have to do any variable finding/substritution in the string. Of course, this is just my opinion. -- paperCrane -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Using ' to access session variables?
>Whats the difference between $_SESSION[foo] and $_SESSION['foo'] I have >been using the 's but they seem to be unecessary? Use the single-quotes -- array references often work without them, but the potential for conflict with constants and future PHP incompatibility is a possibility. If you (or PHP) had a constant foo defined, it would behave unexpectedly. >I run into trouble if I try something like: >$query = " select * from table where (test.id = '$_SESSION['foo']') "; >but the following works: >$query = " select * from table where (test.id = '$_SESSION[foo]') "; http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing // Works but note that this works differently outside string-quotes echo "A banana is $fruits[banana]."; // Works echo "A banana is {$fruits['banana']}."; // Works but PHP looks for a constant named banana first // as described below. echo "A banana is {$fruits[banana]}."; // Won't work, use braces. This results in a parse error. echo "A banana is $fruits['banana']."; - michal migurski- contact info and pgp key: sf/cahttp://mike.teczno.com/contact.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Using ' to access session variables?
Whats the difference between $_SESSION[foo] and $_SESSION['foo'] I have been using the 's but they seem to be unecessary? I run into trouble if I try something like: $query = " select * from table where (test.id = '$_SESSION['foo']') "; but the following works: $query = " select * from table where (test.id = '$_SESSION[foo]') "; I know there is some way to properly escape characters but is there any reason why I shouldn't just omit the 's? Thanks! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php