LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 08:44 AM   #1
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
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, 09:14 AM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

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

Rep: Reputation: 111Reputation: 111
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, 10:42 AM   #4
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
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, 10: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, 10:51 AM   #6
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178

Original Poster
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
@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 10:53 AM.
 
Old 07-11-2005, 11:41 AM   #7
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

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



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 04:44 PM
replacing single quotes in file names adenardo Linux - General 2 05-27-2005 11:29 AM
simple: php addslashes, magic quotes, etc etc? opioid Programming 6 03-01-2005 01:43 PM
In BASH shell, what is the difference in usage between single and double quotes? davidas Linux - Newbie 2 04-05-2004 03:00 AM
US Intl Keybd; Double quotes not showing in some apps Heinz Linux - General 6 01-10-2004 03:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration