LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
Search this Thread
Old 07-11-2005, 09:44 AM   #1
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Rep: Reputation: 121Reputation: 121
Using single quotes vs double quotes in PHP strings


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?
 
Old 07-11-2005, 10:14 AM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 45
hi there,
makes difference for some special cases...
here you will find a better explanation...
regards
slackie1000
 
Old 07-11-2005, 11:11 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.
 
Old 07-11-2005, 11:42 AM   #4
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Original Poster
Rep: Reputation: 121Reputation: 121
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.
 
Old 07-11-2005, 11:47 AM   #5
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
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;
 
Old 07-11-2005, 11:51 AM   #6
vharishankar
Senior Member
 
Registered: Dec 2003
Posts: 3,142
Blog Entries: 4

Original Poster
Rep: Reputation: 121Reputation: 121
@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 vharishankar; 07-11-2005 at 11:53 AM.
 
Old 07-11-2005, 12:41 PM   #7
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 45
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Problems with quotes and double quotes Andruha Slackware 6 01-02-2010 05:44 PM
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
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 11:30 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration