473,799 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="Sata n"; // 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($mat ch,$string);

$formdata=$_POS T["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 1972
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="Sata n"; // 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($mat ch,$string);

$formdata=$_POS T["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="testclas s">This is just a plain header</H2>';
$theHtml .= '<P>Now I add some more information, such as this link:';
$theHtml .= "<A HREF=\"$basepat h/$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******** *************@n ews4.srv.hcvlny .cv.net...
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="Sata n"; // 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($mat ch,$string);

$formdata=$_POS T["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.hcvln y.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=\"$basepat h/$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
3232
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 stumbling on a problem that I am almost certain is a problem with my inputs to the script. The program has a debug mode that is giving me some very weird results. Here's what the script does: It takes a spreadsheet like this: Column1 Column2 ...
37
2880
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 to learn, but now I'd like to ask anyone who reads this, including Aaron, for some clarification. I imagine others might benefit, too. "Aaron Bertrand - MVP" <aaron@TRASHaspfaq.com> wrote > A few suggestions. > (3) why do you constantly set...
16
2410
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 underscore suffix. class SomeClass { private:
3
2895
by: Steve | last post by:
I have some general catch clauses in my app as follows: try { } catch(Exception ex) { } try
3
1481
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 and stylesheet guides on the internet seem to be based on web sites (rather than applications) based on relatively static textual information. I have read that external stylesheet files are the way to go, but it seems to me that these only lend...
6
2901
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 may be an upcoming vote on this, so please stay informed and read news.newgroups.announce for updates. Also see message <2uu44nF2eodc0U1@uni-berlin.de> for an example of the proponent's temperament.
66
3744
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 Language" book doesn't mention it. 2) I understand that C doesn't care about whitespace that much, but why did they make it impossible to use the minus ('-') char in variable names? I now have to incorrectly name my "hi-score" variable "hiscore"....
2
1496
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 link to the site and someone here will likely look at it for about 30 seconds and tell me what's wrong. However, I'd like to be as self-sufficient as I can be so I would _really_ appreciate some guidance on the best way to find this problem for...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10485
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.