|
|
|
NewbieNotes is a little series of tips for people who are new to PHP to give them a few handy tips that the more experienced of us use often
Assuming that $name = "Simon"..
| (1) echo "hello $name<br>\n";
(2) echo 'hello $name<br>\n'; | |
What's the difference?
Try it!
(2) not quite what you expected?
When PHP gets a string with double quotes it decyphers the contents of the string, when it gets single quotes it doesn't bother, just pumps out what came in.
Why is this important?
For one thing the double quote conversion of whatever is inside the quotes to what gets printed only handles simple variables.
If you have an array entity ($array[1]) in there you'll get 'hello Array' out. That's to say that it only looks at the variable, it don't go no further, You can quite conceviably have something horriblew like $array[1][2]->fred[4]->harry->joe, PHP won't go futher than $array[1]
This might seem, at first looks, like a bug or a limitation. It's exactly the opposite!
You should, ideally, never use double quotes. single quote normal strings and just echo, print, whatever, variables by themselves.
The dot operator is your friend when doing this. A dot (full stop, whatever you wanna call it) tacks two strings together.
The best wayt to write the above is ...
| | echo 'hello ' . $name . '<br>\n'; | |
Avoid double quotes whenever you can, if you think about it the reason is obvious. If PHP is going to convert $namke to Simon if it's in double quotes and not if it's in single quotes then whenever it see's double quotes it's gotta try decoding the string just in case there's a variable in there.
This means your code runs slower, your server has more to do, and the end user has to wait longer to get the page (even if it's only milliseconds)
And before anyone mentions it - yes I'm a creature of (bad) habit, I do what I say you shouldn't - I know I do, it's like smoking, just because I do it doesn't meant that you should!
|
|
| Newbie Notes #6 - A true lie Categories : PHP, Beginner Guides, Variables | | | Automatic global variable definer Categories : PHP, Variables, Beginner Guides | | | Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET Categories : Algorithms, HTML and PHP, PHP, Variables | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | clearing variables in php3 Categories : Variables, Arrays, PHP | | | A very simple PHP single password cookie based login without usernames. Categories : PHP, Cookies, Security, Beginner Guides | | | PHP based Contact email form with multiple recipients, text file based, supports departments. Categories : PHP, Email, Beginner Guides, Filesystem | | | A simple function to prevent undefined $_POST/$_GET/$_SESSION variable errors Categories : PHP, Variables, Errors and Logging | | | Newbie Notes #8 - A cron trick Categories : PHP, CURL, Beginner Guides | | | This functions compares the current PHP version with a
desired version. Because of the 3 tiered version system, a
direct compare of a string to phpversion() will not be
accurate. Categories : PHP Configuration, PHP, Variables | | | A beginner's session handling class Categories : PHP, PHP Classes, Sessions, Beginner Guides | | | Get the self URL of current page Categories : PHP, URLs, Beginner Guides | | | Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides | | | Easy alert box pop-up function Categories : PHP, Java Script, Beginner Guides | | | How to control the number of decimal places when outputting numbers. Categories : PHP, Strings, Variables | |
| | | | sean meese wrote : 1082
Nice explanation. I wish I had read this a few years ago!
| | | | Gerd Klingenspor wrote : 1083
It´s wrong, isn´t it?
This is the output: (Do you really want to show to \n?)
hello
hello $name
\n
The ´\n´ is not being interpreted either in single quotes - you would have to write:
echo `hello ` . $name . `<br>`."\n";
| | | | Simon Booth wrote : 1098
Sorry, quite correct, the second one does need double quotes around the \n
I write a lot of these things without actually typing in any code :)
| | | | Dean Wood wrote :1103
Putting {} around your array will help you e.g. {$_POST[`email`]}.
| |
|
|
|