Page 1 of 1

Why the extra quotes in PHP

Posted: Mon Nov 17, 2014 1:51 am
by gautamz07
check out the below syntex :

Code: Select all

echo '<img src="' .$product['img'] .'" alt=" ' .$product['name']. ' ">';
after src , wht the additional ' ' ??

could't it just be :

Code: Select all

echo '<img src=" .$product['img'] ." alt="  .$product['name'].  ">';

Re: Why the extra quotes in PHP

Posted: Mon Nov 17, 2014 2:06 am
by requinix
You should always put quotes around HTML attribute values. Without the "extra" quotes you would produce HTML like

Code: Select all

<img src=/path/to/image.png alt=Name of the product>
One set of quotes is for the PHP, the other set is for the HTML.

Re: Why the extra quotes in PHP

Posted: Mon Nov 17, 2014 6:47 am
by Celauran
gautamz07 wrote:could't it just be :

Code: Select all

echo '<img src=" .$product['img'] ." alt="  .$product['name'].  ">';
No. You've opened a single quote and not closed it, so the . will be echoed as a literal period rather than treated as a concatenation operator. Moreover, variables are not parsed inside single quotes.

Re: Why the extra quotes in PHP

Posted: Mon Nov 17, 2014 10:35 am
by Benjamin
I'd use:

Code: Select all

echo "<img src='{$product['img']}' alt='{$product['name']}'>";

Re: Why the extra quotes in PHP

Posted: Mon Nov 17, 2014 1:30 pm
by Christopher
HTML typically uses double quotes, so you can also do this:

Code: Select all

echo "<img src=\"{$product['img']}\" alt=\"{$product['name']}\">";

Re: Why the extra quotes in PHP

Posted: Mon Nov 17, 2014 2:20 pm
by Benjamin
It used to drive me nuts when people would use single quotes in HTML, but I guess the browsers don't care, so I shouldn't either.

Re: Why the extra quotes in PHP

Posted: Tue Nov 18, 2014 1:12 am
by gautamz07
Wow Thanks Guys :) Thanks very much

Re: Why the extra quotes in PHP

Posted: Tue Nov 18, 2014 1:28 am
by gautamz07
So @Requinix you mean to say :

echo '<img src=" " >';

the above part is for the html and inside that the :

' .$product['img'] .'

is the PHP ???

Re: Why the extra quotes in PHP

Posted: Tue Nov 18, 2014 12:32 pm
by Christopher
To clarify, in this code:

Code: Select all

echo '<img src="' .$product['img'] .'" alt=" ' .$product['name']. ' ">';
the strings being concatenated are:
'<img src="'
$product['img']
'" alt=" '
$product['name']
' ">'

It will echo:

<img src="foo.jpg" alt="Foo">


In this code:

Code: Select all

echo '<img src=" .$product['img'] ." alt="  .$product['name'].  ">';
There is just one string. Every thing between the first ' and end ' will be included literally in the string. It will echo:

<img src=" .$product['img'] ." alt=" .$product['name']. ">

Re: Why the extra quotes in PHP

Posted: Tue Nov 18, 2014 2:16 pm
by requinix
gautamz07 wrote:So @Requinix you mean to say :

echo '<img src=" " >';

the above part is for the html and inside that the :

' .$product['img'] .'

is the PHP ???
Yes. As Christopher just said, the " quotes were for the HTML inside the strings and the ' quotes were for the PHP strings themselves.


While I'm here, I answered the first question on the assumption that in
gautamz07 wrote:could't it just be :

Code: Select all

echo '<img src=" .$product['img'] ." alt="  .$product['name'].  ">';
the mismatched quotes were accidental and it was supposed to read like

Code: Select all

echo "<img src=" .$product['img'] ." alt="  .$product['name'].  ">";
(ie, using " quotes at the beginning and end)

Re: Why the extra quotes in PHP

Posted: Wed Nov 19, 2014 12:57 am
by gautamz07
Ok Ty guys ! :)