Why the extra quotes in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Why the extra quotes in PHP

Post 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'].  ">';
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Why the extra quotes in PHP

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why the extra quotes in PHP

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Why the extra quotes in PHP

Post by Benjamin »

I'd use:

Code: Select all

echo "<img src='{$product['img']}' alt='{$product['name']}'>";
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Why the extra quotes in PHP

Post by Christopher »

HTML typically uses double quotes, so you can also do this:

Code: Select all

echo "<img src=\"{$product['img']}\" alt=\"{$product['name']}\">";
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Why the extra quotes in PHP

Post 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.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Why the extra quotes in PHP

Post by gautamz07 »

Wow Thanks Guys :) Thanks very much
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Why the extra quotes in PHP

Post 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 ???
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Why the extra quotes in PHP

Post 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']. ">
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Why the extra quotes in PHP

Post 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)
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: Why the extra quotes in PHP

Post by gautamz07 »

Ok Ty guys ! :)
Post Reply