Re: Using ' to access session variables?

From: Date: Fri, 16 Apr 2004 16:49:09 +0000
Subject: Re: Using ' to access session variables?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-183644@lists.php.net to get a copy of this message
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/ca            http://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 <Justin Patrin>

« previous php.general (#183644) next »