.: Back one page :.


QUOTES, STRINGS & VARIABLES ::

1   2   3


Quotes -- using single, double or none?

 

<?php

 

 

echo Good Morning;

THIS WON'T WORK !

www.fuse7.co.uk

?>

 

 

 

 

 

<?php

 

 

echo 'Good Morning';

OK->

www.fuse7.co.uk

?>

 

 

 

 

 

<?php

 

 

echo "Good Morning";

OK->

www.fuse7.co.uk

?>

 

 

www.fuse7.co.uk


All text strings should be therefore be quoted. Best practice is to use single quotes wherever possible. However, if a string contains a variable, or single quotes, or a \n (for new line), use double quotes.

When querying numeric only data in MySQL, *don't* use quotes (but DO check that the value actually IS numeric before allowing it to execute anything). If the value contains text as well, quote it. Ensure that any quotes are escaped once, like this:   \'  :also see Magic Quotes

This is a PHP enabled page with the above code embedded in Active Notes. The output from the latter two VIEW links has been dynamically served to your browser by PHP.

Take care when mixing single quotes with double quotes.
These are OK:

echo "color = '#336699'";
echo 'color = "#336699"';

-- but this won't work:

echo "color = "#336699"";


NEXT -- VARIABLES IN STRINGS:

First - what's a variable?