Jump to content

Switch from single to double quoted and reversed the entity, and now it doesn't


Recommended Posts

I have:

{$content['size']}

inside

echo "";

 

I had to switch the quotes around because I added something else that required me to do so, so it now sits at this:

{$content["size"]}

inside

echo '';

 

Before I made the quote switch, it was echoing "Large", which is correct. Now after the switch, it echo's "{$content["id"]} ". Why?!

I've tried 567568758654 different combinations of quotes and removing {} and stuff but non of them fix it. I don't even see why its suddenly not even echoing! All I did was quote reverse!

Ok, the original post was written shit house. I will start again

 

 

I had:

echo "{$content['size']}";

... and this echoed "Large", which is correct.

 

I then switched the quotes around, so its now like this:

echo '{$content["size"]}';

 

The problem now is that instead of echoing "Large", it echo's "{$content["id"]} ".

Why?!

You cannot simply "reverse the quotes"

PHP has a wonderful thing called interpolation.

Interpolation can only happen inside a double quoted string, for single quoted strings you will have to concatenate.

That answers the why :)

$cool = 'Something';
echo 'This is $cool'; // Output: This is $cool
echo "This is $cool"; // Output: This is Something

So the short answer would be, dont put variables into single quoted strings.

for single quoted strings you will have to concatenate.

continuing from Buddski's example...

$cool = 'Something';
echo 'This is' . $cool; // Output: This is Something (concatenated)

I suck so hard its unbelievable!

 

I tried braking out with:

.size.

".size."

"'.size.'"

"size"

'size'

'.size.'

"'size'"

'"size'"

Fail.

 

I then tried

$content['size'] = $size;
echo $size;

... and tried every combination again as listed above.

 

Total failure!

Yeah, I tried it as just $echo also.

 

Heres the code:

echo '
<IMG alt="" src="../products/'.$row['fordir'].'/'.$row['categorydir'].'/'.$row['id'].'/thumbnail.png">
<BR>
<B>Size:</B> {$content["size"]}
<BR>
<BR>
';

This is it:

 

echo '
<IMG alt="" src="../products/'.$row['fordir'].'/'.$row['categorydir'].'/'.$row['id'].'/thumbnail.png">
<B>Product ID:</B> {$content["id"]}
<BR>
<B>Size:</B> {$content["size"]}
<BR>
<B>Price: $</B> $row[price] AUD
<BR>
<BR>
';

As previously said, you CANNOT parse variables in a single quoted string unless you concatenate it.

echo '
<IMG alt="" src="../products/'.$row['fordir'].'/'.$row['categorydir'].'/'.$row['id'].'/thumbnail.png">
<BR>
<B>Size:</B> '.$content["size"].'
<BR>
<B>Price: $</B> '.$row['price'].' AUD
<BR>
<BR>';

Yeah, you said:

$cool = 'Something';
echo 'This is $cool'; // Output: This is $cool
echo "This is $cool"; // Output: This is Something

 

I then tried to copy the logic and failed.

I then tried

$content['size'] = $size;
echo $size;

... and tried every combination again as listed above.

 

Total failure!

From looking at the supplied code (the last revision) you didnt copy the logic at all.

If your script is not echoing anything at all, perhaps you have an error somewhere else in your script that is halting the execution.

Do you still have error_reporting enabled?

Like I said in my original post, everything working fine, except the size not longer echo's "Large" since swapping the quotes, it now echo's "{$content["id"]}".

 

http://i55.tinypic.com/34f01f6.png

Miss this one? It shows you exactly what you missed.

To further clarify, wrapping {} around a variable does nothing in a single quoted string either, this is (basically) for doing more complex things in double quoted or HEREDOC strings.

As previously said, you CANNOT parse variables in a single quoted string unless you concatenate it.

echo '
<IMG alt="" src="../products/'.$row['fordir'].'/'.$row['categorydir'].'/'.$row['id'].'/thumbnail.png">
<BR>
<B>Size:</B> '.$content["size"].'
<BR>
<B>Price: $</B> '.$row['price'].' AUD
<BR>
<BR>';

Ah shit you just beat me to it I was four attempts away from trying that combination of '..'.

I looked at it from another perspective. I said to myself stop focussing on the fact I reversed the strings, and yeah, things started making sense. lol

Thanks but! :)

 

Hey, I noticed you did this yeah..:

<B>Size:</B> '.$content["size"].'
<BR>
<B>Price: $</B> '.$row['price'].'

Size has the double while price has the single. I have both mine single. Is there any difference?

 

EDIT: In fact, it works with no quotes.. but I am going to keep the single quotes in to keep consistency amongst my code. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.