473,382 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

General Guidelines for Quotes ?

Hi guys,

I haven't done that much research on this topic but it seems I can use
either the single quotes or the double quotes. SInce I am so used to C(++) I
prefer the double quotes and am wondering if they could possible be less
efficient. Maybe I should get in the habit of using single quotes with Php
instead ?

I like my code to be consistent, so in two very similar circumstances I
don't want one set of code using single quotes and the other double (I'm
kinda anal, what can I say ?)

Here are some simple and very common usages:

$fullname="Satan"; // regular assignment that may of may not get passed to
function

$match="/[0-9]/"; // same but will definitely get passed to a function
preg_match($match,$string);

$formdata=$_POST["formData"]; // for arrays

I have lots of similar code all throughout my Php programs. So what do you
think, should I be using the single of double quotes in those situations ? I
want to get into a good habbit now so I can write some solid stuff that I
don't have to go back and change.

Take care,
Cyrus
Jul 17 '05 #1
6 1955
Cyrus D. wrote:
Hi guys,

I haven't done that much research on this topic but it seems I can use
either the single quotes or the double quotes. SInce I am so used to C(++) I
prefer the double quotes and am wondering if they could possible be less
efficient. Maybe I should get in the habit of using single quotes with Php
instead ?

I like my code to be consistent, so in two very similar circumstances I
don't want one set of code using single quotes and the other double (I'm
kinda anal, what can I say ?)

Here are some simple and very common usages:

$fullname="Satan"; // regular assignment that may of may not get passed to
function

$match="/[0-9]/"; // same but will definitely get passed to a function
preg_match($match,$string);

$formdata=$_POST["formData"]; // for arrays

I have lots of similar code all throughout my Php programs. So what do you
think, should I be using the single of double quotes in those situations ? I
want to get into a good habbit now so I can write some solid stuff that I
don't have to go back and change.

Take care,
Cyrus


Cyrus,

Have a look at the php online manual, they give some good guidelines on
when to use which and don't forget to look at HEREDOC style too.

The short of it is:
What's between single quotes will be interpreted literally, i.e. '*your
name is $name*' will print literally *your name is $name* without
evaluating $name to the value of the variable.
What's between double quotes will be evaluated, so "*your name is
$name*" would come out as for instance *your name is Cyrus*

Still, with double quotes you'll have to escape double quotes (such as
used in HTML), with single quotes, you don't have to.
Within double quotes escape characters such as \n for new line to create
readable code will be interpreted, within single quotes, those also
won't be interpreted.

So basically, don't be anal and use both.
Generally I use single quotes if it's plain HTML which I want to add to
a string and double quotes for anything that needs evaluating, i.e.:

var $theHtml = '<H2 class="testclass">This is just a plain header</H2>';
$theHtml .= '<P>Now I add some more information, such as this link:';
$theHtml .= "<A HREF=\"$basepath/$filename\">a link</A></P>";

I don't pretend that this is best practice or anything, but it works for me.

Good luck,
Juliette
Jul 17 '05 #2
If a string contains a variable reference that needs to be replaced with the
contents of that variable, then you MUST use double quotes, as in
$var = 'sample';
echo "This is a string which references $var";

this will output: This is a string which references sample

If you use single quotes, as in
$var = 'sample';
echo 'This is a string which references $var';

this will output: This is a string which references $var

If you enclose a string in double quotes and it does not contain any
variables to be substituted then you are causing PHP to do more work than
necessary. It may only be a minimal difference, but it all adds up.

--
Tony Marston

http://www.tonymarston.net


"Cyrus D." <sa***@invalid.org> wrote in message
news:oh*********************@news4.srv.hcvlny.cv.n et...
Hi guys,

I haven't done that much research on this topic but it seems I can use
either the single quotes or the double quotes. SInce I am so used to C(++)
I prefer the double quotes and am wondering if they could possible be less
efficient. Maybe I should get in the habit of using single quotes with Php
instead ?

I like my code to be consistent, so in two very similar circumstances I
don't want one set of code using single quotes and the other double (I'm
kinda anal, what can I say ?)

Here are some simple and very common usages:

$fullname="Satan"; // regular assignment that may of may not get passed to
function

$match="/[0-9]/"; // same but will definitely get passed to a function
preg_match($match,$string);

$formdata=$_POST["formData"]; // for arrays

I have lots of similar code all throughout my Php programs. So what do you
think, should I be using the single of double quotes in those situations ?
I want to get into a good habbit now so I can write some solid stuff that
I don't have to go back and change.

Take care,
Cyrus

Jul 17 '05 #3
Tony Marston wrote:
If a string contains a variable reference that needs to be replaced
with the contents of that variable, then you MUST use double quotes,
as in $var = 'sample';
echo "This is a string which references $var";

this will output: This is a string which references sample

If you use single quotes, as in
$var = 'sample';
echo 'This is a string which references $var';

this will output: This is a string which references $var

If you enclose a string in double quotes and it does not contain any
variables to be substituted then you are causing PHP to do more work
than necessary. It may only be a minimal difference, but it all adds
up.


I tested your theory, and AFAIK using strings in double quotes WITHOUT
substitution vars uses the same amount of time as the single quoted ones.
Only when substitution _does_ take place the processing becomes
significantly slower. At least, that's what I get from my server. PHP
4.3.8/Apache 2

So I'd say go ahead and use what you like best. I use single quotes for
MySql queries, as it makes for an easier and nicer string and less risk.

Rgds
Pjotr


Jul 17 '05 #4
Thanks a lot for that information guys, it clears things up for me. I had no
idea you can put a variable inside a string with double quotes and have its
value come out. I have been using the dot (period) operator for that.

Is either method more efficient than the other, like so:

$strName='Satan';

echo "Welcome to my Web site $strName";

or

echo 'Welcome to my Web site ' . $strName;

I like the first method because it's easier to read; but I'll use the later
method if it's faster. Thanks again, I'm finally getting the hang of PHP
because of all the helpful people at this newsgroup :)

Take care,
Cyrus
Jul 17 '05 #5
"Cyrus D." <sa***@invalid.org> wrote in message
news:<3t*********************@news4.srv.hcvlny.cv. net>...

Is either method more efficient than the other, like so:

$strName='Satan';
echo "Welcome to my Web site $strName";

or

echo 'Welcome to my Web site ' . $strName;


Neither. The most efficient method would be,

$strName='Satan';
echo 'Welcome to my Web site ', $strName;

The difference is that in the first case the double-quoted string
is parsed, in the second case, the single argument being passed
to echo requires concatenation, in the third case, you simply pass
two arguments to echo, so no string operation takes place.

Cheers,
NC
Jul 17 '05 #6
.oO("jrf[no]" <"jrf[no]"@[spam]jokeaday.net>)
$theHtml .= "<A HREF=\"$basepath/$filename\">a link</A></P>";


You can also use single-quotes in HTML, no need for escaping:

....
$theHtml .= "<A HREF='$basepath/$filename'>a link</A></P>";

Micha
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gcook | last post by:
Hi, I've got an old perl program running on my webserver - so old that I haven't used a perl programmer in about two years :) (we've gone all php for a variety of reasons). Anyway, I'm...
37
by: middletree | last post by:
Yesterday, I posted a problem which, by the way, I haven't been able to solve yet. But in Aaron's reply, he questioned why I did several things the way I did. My short answer is that I have a lot...
16
by: E. Robert Tisdale | last post by:
C++ Programming Style Guidelines http://geosoft.no/development/cppstyle.html I think that these guidelines are almost *all* wrong. For example: 11. Private class variables should have...
3
by: Steve | last post by:
I have some general catch clauses in my app as follows: try { } catch(Exception ex) { } try
3
by: JezB | last post by:
What's the generally accepted approach for using Styles and Stylesheets in a web application based on .aspx files, Web Controls, User Controls, and code-behind modules (c# in my case)? Most style...
6
by: Andy | last post by:
Someone posted this official proposal to create comp.databases.postgresql.general again. He wrote his own charter. As far as I know, he did not consult any of the postgresql groups first. There...
66
by: KimmoA | last post by:
Hey! Some questions about C that have been bugging me for a while... 1) Is inline a valid C keyword or not? I was kind of surprised of not finding it in C, to be honest. My "The C Programming...
2
by: rhino | last post by:
I'm having a problem with the CSS on the website I am developing. A div that works perfectly fine in IE7, FF, and Opera doesn't appear at all in IE6. Now, I realize that I could simply post a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.