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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-11-2005, 08:44 AM
|
#1
|
|
Senior Member
Registered: Dec 2003
Posts: 3,142
Rep: 
|
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?
|
|
|
|
07-11-2005, 09:14 AM
|
#2
|
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
hi there,
makes difference for some special cases...
here you will find a better explanation...
regards
slackie1000
|
|
|
|
07-11-2005, 10:11 AM
|
#3
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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.
|
|
|
|
07-11-2005, 10:42 AM
|
#4
|
|
Senior Member
Registered: Dec 2003
Posts: 3,142
Original Poster
Rep: 
|
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.
|
|
|
|
07-11-2005, 10:47 AM
|
#5
|
|
Member
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340
Rep:
|
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;
|
|
|
|
07-11-2005, 10:51 AM
|
#6
|
|
Senior Member
Registered: Dec 2003
Posts: 3,142
Original Poster
Rep: 
|
@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.
|
|
|
|
07-11-2005, 11:41 AM
|
#7
|
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:50 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|