Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Basics (Retired) PHP Data & Structure PHP Variables

Why 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
Vince Brown
16,249 Points

Hey 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">

I appreciate the time you took to really flesh out this answer. Thanks!