View Full Version : quotations in php
HTML_Kid
02-07-2007, 03:16 AM
im trying to learn php and i have a question that i can not find an answer to
is there a difference in doubble quotes "" and single quotes '' in php and if soo how do i use them correctly
thankyou
martialtiger
02-07-2007, 04:37 AM
Double quotes is normally used when you are getting the data from a variable i.e. $a = "$b" while single quotes means that the data in there is as is without needing to evaluate any variable.
Hope that makes sense.
firepages
02-07-2007, 12:59 PM
The most obvious difference in everyday use is that PHP checks the contents of double quoted strings/indexes for variables whilst ignores the content of single quotes strings and variables.... e.g.
<?php
$hello='Hello';
echo "$hello";// prints 'Hello'
echo '$hello'; // prints '$hello'
?>
note that really you should just..
<?php
echo $hello;
?>
The double quotes allow you to create more complex strings..
<?php
$var='World';
echo "Hello $var";//prints 'Hello World'
echo 'Hello $var';//prints 'Hello $var';
?>
same same for array keys..
<?php
DEFINE('life','blah');
$_GET['blah']='blah';
$_GET['life']='life';
?>
all the above are valid but note that...
<?php
echo $_GET['life']; // prints life
echo $_GET["life"];// prints life
echo $_GET[life]; //prints blah
?>
Technically, using single quotes is faster (since PHP does not try to parse their contents) and whilst this difference is small if even noticeable it is regardless (generally accepted as) best practice.
marek_mar
02-07-2007, 01:02 PM
All three will print blah. :P
HTML_Kid
02-07-2007, 01:34 PM
thank you. that explains why my email form wasn't working :)
mlseim
02-07-2007, 03:19 PM
HTML Kid ...
.... and you have it right in front of you: $must = $_GET['life'];
That is correct.
firepages
02-08-2007, 07:24 AM
All three will print blah. :P
you had me worried for a second but no they don't ;)
marek_mar
02-08-2007, 09:35 AM
Next time I'll take a screenshot of what you posted. ;)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.