[PHP] Double Quotes or not?

Questions about programming languages and debugging
Post Reply
User avatar
Broken Angel
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 432
Joined: 05 Jul 2010, 04:58
13
Contact:

[PHP] Double Quotes or not?

Post by Broken Angel »

$foo = "Hello";
$bar = $foo . " world! <br />";
echo $bar; // Output: Hello world!

In the above why are we not using DOUBLE QUOTES are not used after the echo
I have read that the DOUBLE QUOTES give the value as output and in this case we have defined $bar with a value i.e. $foo??
Where am I wrong and am I missing a big fundamental in here... Pls guide??
God Blessed Me With Forgiveness And I Forgive You With My Revenge...!



-Broken Angel

User avatar
lilrofl
Siliconoclast
Siliconoclast
Posts: 1363
Joined: 28 Jan 2009, 17:00
15
Location: California, USA
Contact:

Re: [PHP] Double Quotes or not?

Post by lilrofl »

I'll open up by saying that I don't know jack about PHP, however this looks veryy similar to other language hello world programs, so I'll take a stab at it. Someone will correct me if I'm wrong :D

Code: Select all

$foo = "Hello"
$bar = $foo . " world! <br />";
echo $bar; // Output: Hello world!
Quotes are used to literal something in this case, 'world! = world, and hello = hello, where variables are containers that are = to whatever is in them.

In this case foo is a container holding the string "hello", where bar is a container holding the container foo, and the string "world!" when you echo the container, no quotes are needed; inside the container is returned.

so I would think that:

Code: Select all

echo "hello world <br/>"
or tell me these two strings, is the same as

Code: Select all

echo $bar
or look in that container over there and tell me what you find

I could be wrong though :D
knuffeltjes voor mijn knuffel
[img]http://i911.photobucket.com/albums/ac320/stuphsack/Sig.jpg[/img]

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: [PHP] Double Quotes or not?

Post by Gogeta70 »

You don't need to use quotes when you're only outputting the contents of a variable.

Code: Select all

<?PHP

$foo = " world!";
echo Hello, $foo; // This is wrong.

echo "Hello, $foo"; // This is right, since we're mixing a literal string and a variable together.

echo 'Hello, $foo'; // This is wrong. If you use single quotes, $foo is taken as a literal string instead of a variable. (Output would be: Hello, $foo)

$bar = "Hello," . $foo; // Concatenate ' world!' and 'Hello,'

echo $bar; // This is okay without double quotes because we're only echoing a variable.

?>
¯\_(ツ)_/¯ It works on my machine...

User avatar
Broken Angel
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 432
Joined: 05 Jul 2010, 04:58
13
Contact:

Re: [PHP] Double Quotes or not?

Post by Broken Angel »

Fanks guys both the guys are correct and here is what I was toold by one more friend of mine. I would like to share so that if anyone else is having the same problem can read it :)


$foo = "hello"; // We need quotes here because it's a string
$bar = "world"; // same as above, this is a string too.

$foobar = $foo . $bar; // So these are both variables, they are just representing hello and world and when we use variables in php we don't wrap them in quotes.

echo $foobar; // again this is a variable here, so no quotes.

echo "Hello world php example"; // we want to print a string so we have to wrap it in quotes


lilrofl you kinda beat the bush but I must say your right :D :D I love this forum just for this :) M doing a while and do while operator now :)
God Blessed Me With Forgiveness And I Forgive You With My Revenge...!



-Broken Angel

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: [PHP] Double Quotes or not?

Post by ayu »

Double quotes interpret the content, while single quotes do not.

Example

Code: Select all

$foo = 'hello';
echo "$foo bar";
The above would give

Code: Select all

hello bar
Example 2

Code: Select all

$foo = 'hello';
echo '$foo bar';
The above would give

Code: Select all

$foo bar
So if you are not including a variable in a string, or anything else that has to be interpreted, then don't use double quotes, use single ones.
Because it means that it will be slightly faster.
"The best place to hide a tree, is in a forest"

User avatar
Broken Angel
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 432
Joined: 05 Jul 2010, 04:58
13
Contact:

Re: [PHP] Double Quotes or not?

Post by Broken Angel »

Yes this was what I was wanting to know in the example that I sighted in here it says that we don't need to use quotes when we are using variables, but in your example catz sir you used it. Cause double quotes interpeert the value and then give the output.
So where am I wrong if I put my example this way?

$foo = "Hello";
$bar = $foo . " world! <br />";
echo "$bar"; // Output: Hello world!

Because we want to display what's in $bar and not $bar itself right? :-k
God Blessed Me With Forgiveness And I Forgive You With My Revenge...!



-Broken Angel

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: [PHP] Double Quotes or not?

Post by ayu »

Broken Angel wrote:Yes this was what I was wanting to know in the example that I sighted in here it says that we don't need to use quotes when we are using variables, but in your example catz sir you used it. Cause double quotes interpeert the value and then give the output.
So where am I wrong if I put my example this way?

$foo = "Hello";
$bar = $foo . " world! <br />";
echo "$bar"; // Output: Hello world!

Because we want to display what's in $bar and not $bar itself right? :-k
Well, first of all, you don't have to write

Code: Select all

$bar = $foo . " world! <br />";
You can change it to

Code: Select all

$bar = $foo . ' world! <br />';
As there is nothing within the quotes that needs to be interpreted.

And second of all, yes, but it depends on what you want of course.
Bottom line is that if you want your variable to be interpreted, as in show what is inside it and not the text $bar itself, then you use double quotes or simply put the variable outside of the quotes.
And if you want the text itself ($bar) then you use single quotes.

Are you with me? ;)
"The best place to hide a tree, is in a forest"

User avatar
Broken Angel
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 432
Joined: 05 Jul 2010, 04:58
13
Contact:

Re: [PHP] Double Quotes or not?

Post by Broken Angel »

Yeah I get the point and I agree to it too :) so basically about my query double quotes or not it will simply work cause:
1. It is a variable that we are talking about and strings need to be put that way and
2. Even if we don't use the above rule simply put the quotes as in double qoutes and make this happen.??
=D>
God Blessed Me With Forgiveness And I Forgive You With My Revenge...!



-Broken Angel

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: [PHP] Double Quotes or not?

Post by ayu »

Broken Angel wrote:Yeah I get the point and I agree to it too :) so basically about my query double quotes or not it will simply work cause:
1. It is a variable that we are talking about and strings need to be put that way and
2. Even if we don't use the above rule simply put the quotes as in double qoutes and make this happen.??
=D>

Basically yes :)

Personally I always use this method

Code: Select all

$someString = $anotherString . ' I like pies';
Rather than this method

Code: Select all

$someString = "$anotherString  I like pies";
And not for any reason that I can really motivate, more than I like it more, and it looks more clear to me.
"The best place to hide a tree, is in a forest"

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Re: [PHP] Double Quotes or not?

Post by Lundis »

The advantage with "" is that it'll be a lot less typing if you want a lot of variables embedded into your string

Code: Select all

echo "$a blabla $b foo $c bar $d lol $e;"
echo $a . ' blabla ' . $b . ' foo ' . $c . ' bar ' . $d . ' lol ' . $e;
In most other common languages you have to do it the long way (or preferable use printf ;) )

User avatar
CommonStray
Forum Assassin
Forum Assassin
Posts: 1215
Joined: 20 Aug 2005, 16:00
18

Re: [PHP] Double Quotes or not?

Post by CommonStray »

Double quotes also let you interpret more escape sequences, which if you try to escape a special character within single quotes they wont be expanded.

In double quotes you can also use curly braces around more complex expressions as well...
e.g.

Code: Select all

echo "Hey suck-o, {$this->username} is showing an example";


and arrays

Code: Select all

echo "Hey suck-o, {$array['key']} works too!";
I generally don't expand variables within double quotes because my IDE doesn't highlight the variable, which would make it harder to read. Also its much easier and cleaner looking to use single quotes when evaluating strings which contain double quotes and vise versa, so that you don't have to continually escape out of them.

Post Reply