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 trial

PHP Build a Simple PHP Application Listing Inventory Items Even More Excitement With Arrays

Randall Zepeda
Randall Zepeda
1,979 Points

php html mixture

can someone explain the code to combine php and html more thoroughly like this code: <?php

$movie["title"] = "Star Wars";

echo "<h1 class=\"title\">" . $movie["title"] . "</h1>";

?>

like what do the first and second quotations exactly do as well as the \ dividers not sure if they are comments and also the last set of quotations on the last h1 and what instances you would use the . to combine things, like whats the special rule for that, .

Thanks,Randy

saranyamoellers
saranyamoellers
31,639 Points

I think the \ is for escape quotations so the brower won't be confused between quotations from echo " "; or you can do this without escape

echo "<h1 class='title'>" . $movie["title"] . "</h1>";

and the dot . is for concat the variable and string together

4 Answers

Emmanuel Salom
PLUS
Emmanuel Salom
Courses Plus Student 8,320 Points

The "\" are used to scape characters. If you want to insert php code to your HTML you do it by placing something like this:

<?php echo $phpvariable ; ?> 

anywhere in your html

Randall Zepeda
Randall Zepeda
1,979 Points

like what ? lol its not showing up in the comment

Don McCurdy
Don McCurdy
2,345 Points

I think this is what Emmanuel was getting at, but \ dividers and . can be used with any strings (text) at all — doesn't need to be HTML.

  • \ Backslashes escape (or protect) characters that would otherwise end the string. If your string is wrapped in single quotes, you'll need to escape single quotes inside the string so that PHP knows they're part of it, not the end. If you use double quotes, you don't need to escape single quotes anymore but do need to escape double quotes. Examples:
<?php

// Single quotes
$string = 'Hello, I\'m glad to meet you.';

// Double quotes
$string = "I said, \"Hello, I'm glad to meet you.\"";

// Bad! (forgot to escape a quote)
$string = 'This won't work, because the first quote ends the string.';
  • . Dot-operators are PHP's way of joining strings together. Similar to + in other languages. Example:
<?php

echo "Hello, " . "how are you?"; // prints: Hello, how are you?

All of these rules work whether or not the text is HTML – PHP doesn't even know the difference. Since HTML can often get fairly complicated on a real website, you'll often want to use templates instead of messing with all of this string escaping and concatenation. Building a Simple PHP Application is a good place to start with that.

Randall Zepeda
Randall Zepeda
1,979 Points

Thanks, Im on simple php application now actually and thats where the concatenation and escaping is being used. Still confused on the escape. So for the double quotes... why are there two sets of quotes. why cant it just be "I said,Hello,Im glad to meet you."; what is the point of the other set of quotes and the bad string.. why cant it just be 'this wont work,because the...string'; im confused as why these extra quotes are put in place.

Hugo Paz
Hugo Paz
15,622 Points

Hi Randall,

The examples Don McCurdy gave you are possible situation yo might find. You use double quotes to create a string. But if your string has quotes inside it, like this "I am quoting someone so i put this sentence in quotes", you need to escape the quotes that will be shown or else the string breaks, like in his last example.

This:

$string = "I said, \"Hello, I'm glad to meet you.\"";

prints this : I said, "Hello, I'm glad to meet you."

see the quotes inside the sentence?

while this :

$string = "I said, Hello, I'm glad to meet you.";

prints this: I said, Hello, I'm glad to meet you.

Randall Zepeda
Randall Zepeda
1,979 Points

so you have to put it after \" and before \" for it to work? and is that its only use? to put quotes inside a print and it doesn't get mixed up as html php languages?

Hugo Paz
Hugo Paz
15,622 Points

In this case yes. There are other characters that need to be escaped, like for instance the / character.

Every programming language has it's special characters - characters that mean something special such as identifying a variable, the end of a line or a break in some data.

We need to escape them so the program will run the way we intend.

Emmanuel Salom
PLUS
Emmanuel Salom
Courses Plus Student 8,320 Points

Oops sorry I replied using my mobile here you go: <h1 class="title"> <? echo $movie["title"]; ?> </h1>