By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

difference between ' and "

P: n/a
Today I spent a couple of hours searching for a bug, until I finally
found out that the problems was the following:

Correct: $path_upload = "$path/$file";

Instead of:

Wrong: $path_upload = '$path/$file';

Note a difference in quotation marks. Since I have been programming in
PHP I found out that there is very little difference between ' and ",
but is there somebody who can explain me, when to use ' and when to
use ". It would be really great if there is somebody to explain this
to me!

Thanks,
Jochem
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Jochem wrote:
Since I have been programming in
PHP I found out that there is very little difference between ' and ",
but is there somebody who can explain me, when to use ' and when to
use ". It would be really great if there is somebody to explain this
to me!


*NEVER* use ", unless you _need_ them

I only need " for two things:
embedding \n, \t and similar
simplify SQL queries, but I don't have to like it :)

$name1 = 'Pedro Graca'; // ok
$name2 = "Pedro Graca"; // forces PHP to do extra work
$name3 = $name1; // ok
$name4 = "$name2"; // UGH! *TRIPLE* UGH!!!!
// people who do this should be whipped
echo 'Your name is: ', $name, "<br/>\n";

$sql = "insert into table values($new_id, '$new_name')";
// for comparison here is the echo with only " (simpler, but not needed)
// echo "Your name is: $name<br/>\n";

// and the $sql with only ' (more awkward)
// $sql = 'insert into table values(' . $new_id . ', \'' . $new_name . '\')';
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
On 2003-12-30, Jochem <jd******@operamail.com> wrote:
Today I spent a couple of hours searching for a bug, until I finally
found out that the problems was the following:

Correct: $path_upload = "$path/$file";

Instead of:

Wrong: $path_upload = '$path/$file';

Note a difference in quotation marks. Since I have been programming in
PHP I found out that there is very little difference between ' and ",
but is there somebody who can explain me, when to use ' and when to
use ". It would be really great if there is somebody to explain this
to me!

http://www.php.net/manual/en/language.types.string.php

--
verum ipsum factum
Jul 17 '05 #3

P: n/a
On 2003-12-30, Pedro Graca <he****@hotpop.com> wrote:
$name4 = "$name2"; // UGH! *TRIPLE* UGH!!!!
// people who do this should be whipped
I think people only come up with this one if they have a Bash background
;)
// for comparison here is the echo with only " (simpler, but not needed)
// echo "Your name is: $name<br/>\n";


In this case you would need: "Your name is: {$name}<br>\n";

--
verum ipsum factum
Jul 17 '05 #4

P: n/a
Jochem wrote:
Today I spent a couple of hours searching for a bug, until I finally
found out that the problems was the following:

Correct: $path_upload = "$path/$file";

Instead of:

Wrong: $path_upload = '$path/$file';

Note a difference in quotation marks. Since I have been programming in
PHP I found out that there is very little difference between ' and ",
but is there somebody who can explain me, when to use ' and when to
use ". It would be really great if there is somebody to explain this
to me!


Main difference to me is that when using "" php is looking for variables
between the "".

- D -
Jul 17 '05 #5

P: n/a
Tim Van Wassenhove wrote:
On 2003-12-30, Pedro Graca <he****@hotpop.com> wrote:
$name4 = "$name2"; // UGH! *TRIPLE* UGH!!!!
// people who do this should be whipped


I think people only come up with this one if they have a Bash
background ;)
// for comparison here is the echo with only " (simpler, but not
needed) // echo "Your name is: $name<br/>\n";


In this case you would need: "Your name is: {$name}<br>\n";


Actually no,
echo "Some variable is $somevar dude!"; is perfectly fine. You only need { }
for arrays inside a string, e.g.
echo "Some variable is {$somevar['name']} dude!";
although inside of a string you can leave off the quotes on the key and have
echo "Some variable is $somevar[name] dude!";
While the last is proper according to the manual, I find it poor form and
prefer "somevar is {$something['blah']}".
Jul 17 '05 #6

P: n/a
On 2003-12-30, Agelmar <if**********@comcast.net> wrote:
Tim Van Wassenhove wrote:
On 2003-12-30, Pedro Graca <he****@hotpop.com> wrote:
$name4 = "$name2"; // UGH! *TRIPLE* UGH!!!!
// people who do this should be whipped
I think people only come up with this one if they have a Bash
background ;)
// for comparison here is the echo with only " (simpler, but not
needed) // echo "Your name is: $name<br/>\n";


In this case you would need: "Your name is: {$name}<br>\n";


Actually no,
echo "Some variable is $somevar dude!"; is perfectly fine. You only need { }
for arrays inside a string, e.g.


Actually yes, the OP wrote: echo "Your name is: $name<br/>\n";
No space between the variable and <br>.
echo "Some variable is {$somevar['name']} dude!";
although inside of a string you can leave off the quotes on the key and have
echo "Some variable is $somevar[name] dude!";
While the last is proper according to the manual, I find it poor form and
prefer "somevar is {$something['blah']}".


Not using quotes is not only poor, it is also slower.
--
verum ipsum factum
Jul 17 '05 #7

P: n/a
Tim Van Wassenhove wrote:
Actually yes,
Actually no. SCNR. ;-)
the OP wrote: echo "Your name is: $name<br/>\n";
No space between the variable and <br>.


A LESS-THAN SIGN, just like a SPACE, isn't a valid variable name
character, so the variable $name will be recognised.

http://www.php.net/manual/en/language.variables.php

--
Jock
Jul 17 '05 #8

P: n/a
In message <bs***********@ID-203069.news.uni-berlin.de>, Pedro Graca
<he****@hotpop.com> writes
<snip>

$name1 = 'Pedro Graca'; // ok
$name2 = "Pedro Graca"; // forces PHP to do extra work

<snip>

Could you explain why the double quotes make PHP do more work than the
single quotes, please?

--
Five Cats
Email to: cats_spam at uk2 dot net
Jul 17 '05 #9

P: n/a
Five Cats uttered the immortal words:
<snip>

$name1 = 'Pedro Graca'; // ok
$name2 = "Pedro Graca"; // forces PHP to do extra work

<snip>

Could you explain why the double quotes make PHP do more work than the
single quotes, please?


Click the link Tim posted in his first post to this thread.

--
Andy.
Jul 17 '05 #10

P: n/a
"Five Cats" <ca*******@[127.0.0.1]> wrote in message
news:Wv**************@[127.0.0.1]...
In message <bs***********@ID-203069.news.uni-berlin.de>, Pedro Graca
<he****@hotpop.com> writes
<snip>

$name1 = 'Pedro Graca'; // ok
$name2 = "Pedro Graca"; // forces PHP to do extra work

<snip>

Could you explain why the double quotes make PHP do more work than the
single quotes, please?

--
Five Cats
Email to: cats_spam at uk2 dot net


Bottom line: PHP parses anything in " " looking for variables to replace
with values, anything in ' ' it does not.


Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.