LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Thread Tools
Old 07-11-2005, 09:44 AM   #1
hs123
Registered User
 
Registered: Dec 2003
Posts: 2,832
Blog Entries: 4
Thanked: 9
Using single quotes vs double quotes in PHP strings


[Log in to get rid of this advertisement]
I've read somethings on using single quotes and double quotes in PHP strings and I always use single quotes to enclose all my strings.

I was just curious as to why some people use single quotes and why some use double quotes in PHP strings.

Is it a matter of preference, convenience or is there any advantage to using one over the other?
hs123 is offline     Reply With Quote
Old 07-11-2005, 10:14 AM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,036
Thanked: 0
hi there,
makes difference for some special cases...
here you will find a better explanation...
regards
slackie1000
slackie1000 is offline     Reply With Quote
Old 07-11-2005, 11:11 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,497
Thanked: 44
Briefly:
In double-quoted strings, variables are replaced by their values:
PHP Code:
$word "Hello".
echo 
"$word there!" 
The above code will output:
Hello there!

Doing the same with single quotes:
PHP Code:
$word "Hello".
echo 
'$word there!' 
...will print:
$word there!

Using single quotes where possible, will be slightly more efficient to process, because PHP doesn't have to search single quoted strings for variables.
Hko is offline     Reply With Quote
Old 07-11-2005, 11:42 AM   #4
hs123
Registered User
 
Registered: Dec 2003
Posts: 2,832
Blog Entries: 4
Thanked: 9

Original Poster
Hmm....

Strings are quite complex, aren't they! I never realized that there were so many parsing options for double quoted strings.

I am glad that I have kept off the complexity and using single quoted strings in my whole application.
hs123 is offline     Reply With Quote
Old 07-11-2005, 11:47 AM   #5
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340
Thanked: 0
I also use heredocs for large text output (variables are still replaced, this is called interpolation)

Code:
print <<<END_HTML_CODE

  <form method="post" action="$_SERVER['PHP_SELF']">

  <table border="0" width="90%">
    <tr>
      <td><input type="text" name="something" /></td>
    </tr>

    .......

  </table>
  </form>

END_HTML_CODE;
lowpro2k3 is offline     Reply With Quote
Old 07-11-2005, 11:51 AM   #6
hs123
Registered User
 
Registered: Dec 2003
Posts: 2,832
Blog Entries: 4
Thanked: 9

Original Poster
@lowpro2k3,

I simply use templating system to output large strings or files . I find that it's so much quicker when you build a templating class to parse and output HTML so that the rest of your application can concentrate on the logic.

Try it. Building your own templating system and implementing it can be great fun as I am finding out now. It speeds up application development tremendously and helps keep the logic/code separate from the presentation (output/HTML).

Last edited by hs123; 07-11-2005 at 11:53 AM..
hs123 is offline     Reply With Quote
Old 07-11-2005, 12:41 PM   #7
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,036
Thanked: 0
hi there,
Quote:
Originally posted by Hko
Briefly:
In double-quoted strings, variables are replaced by their values:
PHP Code:
$word "Hello".
echo 
"$word there!" 
The above code will output:
Hello there!
Doing the same with single quotes:
PHP Code:
$word "Hello".
echo 
'$word there!' 
...will print:
$word there!
Using single quotes where possible, will be slightly more efficient to process, because PHP doesn't have to search single quoted strings for variables.
nice post. well explained...
just adding that this behaviour is pretty similar in script languages when you work with regular expressions...
regards
slackie1000
slackie1000 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replacing single quotes in file names adenardo Linux - General 2 05-27-2005 12:29 PM
simple: php addslashes, magic quotes, etc etc? opioid Programming 6 03-01-2005 02:43 PM
Problems with quotes and double quotes Andruha Slackware 4 02-15-2005 10:44 AM
In BASH shell, what is the difference in usage between single and double quotes? davidas Linux - Newbie 2 04-05-2004 04:00 AM
US Intl Keybd; Double quotes not showing in some apps Heinz Linux - General 6 01-10-2004 04:48 PM


All times are GMT -5. The time now is 05:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration