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 trialA X
12,842 PointsWhy are we preserving the quotation marks around the alt name of the image of Mike the Frog? & What does echo mean?
Hi all, Taking the PHP intro with Hampton, and I noticed that he changed the formerly HTML document from this:
<body>
<section class = ''>
<div class = ''>
<img src = "img/avatar.png" alt = "Mike the Frog">
</div>
To this:
<body>
<section class = ''>
<div class = ''>
<img src = "img/avatar.png" alt = "<?php echo $name ?>">
</div>
But why are we preserving the quotation marks around the variable in the alt? Isn't that redundant because in the variable assignment there's quotation marks around the name of "Mike"?
Also on a separate note, I didn't feel like Hampton really explained what echo is...is it simply let's return whatever value we assign the variable to be? It seems like the server does some HTML conversion though...like when he looks at the console the full PHP code isn't present...so what's happening?
Thanks for your help!
1 Answer
Vince Brown
16,249 PointsHey Abby, To answer your question let's start with the variable assignment.
in php there are 7 data types, which means ways we can store our data in variables. These are
String.
Integer.
Float (floating point numbers - also called double)
Boolean.
Array.
Object.
NULL.
When you want to store a value like a name for an alt tag you would use the string Data type which to the computer just means a sequence of characters.
To set a variable in php that holds string data we need to wrap it in quotations.
<?php
$name = "Mike the Frog";
?>
These quotations are not actually apart of the variable , so whenever you use the variable your output will be
Mike the Frog
not
"Mike the Frog"
The echo function outputs a string so when you use it with the $name variable your result would be
<?php
$name = "Mike the Frog";
// call echo
echo $name;
// the output would be
Mike the Frog
?>
Since the Quotation marks are not apart of our output that is why you must retain them in your markup. If you left them off your markup would not output correctly
// without the quotation marks the code below
<img src="img/avatar.png" alt=<?php echo $name ?>>
// would output
<img src="img/avatar.png" alt=Mike the Frog>
// which is not valid so we retain the quotation marks
<img src="img/avatar.png" alt="<?php echo $name ?>">
// and the output is valid markup
<img src="img/avatar.png" alt="Mike the Frog">
A X
12,842 PointsA X
12,842 PointsI appreciate the time you took to really flesh out this answer. Thanks!