Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialammarkhan
Front End Web Development Techdegree Student 21,661 Pointsconcatenate in php confusion
I am trying to get my head around the following statement, but i am unable to understand of why so many quotes like ("") and then (") in the following example <br>
'<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
can someone break it down for me?
1 Answer
thomascawthorn
22,986 PointsAn html element has double quotes surrounding attribute values.
<p class="class-attribute-value" id="id-attribute-value"></p>
The key thing to remember is that in this case, php is outputting html to the browser. Because valid html elements require these quotes (it's just their syntax), you have to make sure you're printing them to the screen in the correct format.
It's confusing because you need quotes within strings, as well as quotes around strings.
<?php
// in php, you're using single quotes to wrap your string;
$string = '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
// remember that concatination is just the joining of stuff,
// so you could represent it like this:
$image1 = '<img src="';
$iamge2 = $product["img"];
$image3 = '" alt="';
$image4 = $product["name"];
$image5 = '">';
// The single quotes above are in exactly the same position as your example,
// we're just joining them slightly differently.
$element = $image1 . $image2 . $image3 . $image4 . $image5;
I too hate concatenation similar to the example you gave because it can make code incredibly unreadable! * Not to say that it's a bad way of doing it - just personal preference.
There is not a lot you can do other than really work through it a couple of times and work out at which quote the string is starting and stopping, and which quotes are within strings.
NOW. Enough of this untidy concatenation. My OCD is going haywire.
In the real world, I would do this.
<?php
// Write the element and use php specifically for the
// tags: (with and without short echo codes) ?>
<img src="<?php echo $product["img"] ?>" alt="<?php echo $product["name"] ?>">
<img src="<?= $product["img"] ?>" alt="<?= $product["name"] ?>">
<?php
// Use double quotes. When using double quotes,
// variables are parsed by php inside the string
// (just remember to use curly braces around the variable!)
$name = 'Tom';
$string = 'My name is $name'; // My name is $name
$string = "My name is {$name}" // My name is Tom ?>
When using double quotes, you need to escape the html double quote - otherwise php thinks you're ending the string. You 'escape' something by adding a backslash before the character. It essentially says to php 'ignore me! I'm part of this string!'. It will only be required if the character is meaningful to php - like a quote. The backslashes will not show up in your final code.
<?php
$string = "<img src=\"{$product["img"]}\" alt=\"{$product["name"]}\">";
Pick one that seems most sensible and readable to you :-)
ammarkhan
Front End Web Development Techdegree Student 21,661 Pointsammarkhan
Front End Web Development Techdegree Student 21,661 PointsTom Cawthorn u explained it perfectly. Thank u for taking my weeks of frustration away.