WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Newbie Notes #5 - To double quote, or single quote, that is the question
Categories : PHP, Beginner Guides, Variables Click here to Update Your Picture
Simon Booth
Date : Apr 21st 2004
Grade : 4 of 5 (graded 3 times)
Viewed : 7213
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Simon Booth
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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!




Automatic global variable definer
Categories : PHP, Variables, Beginner Guides
Newbie Notes #6 - A true lie
Categories : PHP, Beginner Guides, Variables
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
Get the self URL of current page
Categories : PHP, URLs, Beginner Guides
Define equivalents in php3.
Categories : PHP, Variables
Newbie Notes #1 - Making a form return to itself
Categories : PHP, Beginner Guides, HTML and PHP
Display variables when a form is submitted using POST/GET
Categories : PHP, Functions, Variables, Debugging
Multiple Select box, Select multiple Items from Menu.List box
Categories : PHP, HTML and PHP, Beginner Guides
mySQL/PHP/search with multientry form and table output with colored rows
Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases
A very simple PHP single password cookie based login without usernames.
Categories : PHP, Cookies, Security, Beginner Guides
How to implement a session tracking system.
Categories : PHP, Sessions, Variables
Newbie Notes #3 - What went wrong? A useful little debugging aid
Categories : PHP, Beginner Guides, Debugging
Script to check values being submitted by POST or GET method from a form. This script may help diagnose what variables are being supplied by a browser to other php scripts.
Categories : HTML, Variables, Debugging, PHP, HTTP
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
The following snippet gives complete info about all submitted HTTP_POST_VARS and HTTP_GET_VARS
Categories : Variables, HTTP, PHP
 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 . `&lt;br&gt;`."\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`]}.