Solution: Embedding PHP in HTML - Ayush Shrestha || UI/UX || Front-end || Angular || React || Wordpress

Solution: Embedding PHP in HTML

– The challenge was to use the arrays in the PHP block at the top of embed.php to create the content for the web page. There’s no single correct solution, but this is how I would do it. The main heading on line 23 simply says ‘Book’ and the first paragraph simply says ‘Description’.And we can get both of those from this $book associative array. But because we’re dealing with an associative array, we need to work carefully. So let’s just delete that ‘Book’ placeholder between the H1 tags.

And we need to embed a PHP block in there. I’m going to use the shorthand for ‘echo’,because there’s no need for any other PHP code in this block. So that’s shorthand and then we’ll have the closing tag as well. And then between that we need to have the values that we’re going to display. Now rather than just the ‘Book’ title, I want to include the name of the author, so I’m going to use a double quoted string. So a pair of quotes in there.

But the thing is that both the book title and the author come from an associative array. So it will embed them in a double quoted string. We need to have curly braces. So a pair of curly braces. We’ll have a closing one there and then we’ll type in between there $book and the element that we want is ‘title’ and after that closing curly brace, some literal text, ‘by’. And then another pair of curly braces and between those curly braces we’ll have $book and the ‘author’ element.

So that’s dealt with the title, the main heading. So let’s take a look at the ‘Description’. We’ll just delete that and then we can also use the shorthand for ‘echo’. So, put that in there quickly. And then the closing tag and then we need to go between those two tags. And let’s scroll up and take a look at description. It’s not a complete sentence, so we need to embed that either in the double quoted string or we need to use the concatenation operator.

I’m going to use the concatenation operator. Scroll back down again. And I want the book title to be in quotes. So we need a pair of single quotes and then between them, we’ll have a single, double quote. Need to get rid of that second one that’s been put in by my editor. And then we have the concatenation operator. Then we’ll have $book and the ‘title’ element. And we need the concatenation operator again and another pair of single quotes.

Then we need to have the closing quote for around the title. Put that in there. Again, get rid of that extra one that has been put in by my editing program. Then a space, the word ‘is’, and another space. And we’ve got that closing quote there. After the closing quote, the concatenation operator again. And then we have, from the $book array, the ‘description’. And that looks fine to me, but we need to have a period or a full stop at the end of the sentence.

I’m gonna put that outside of the PHP block. Now you might have noticed that I haven’t used a semi-colon either here or here. And when you’ve got only one command inside a PHP block, that’s perfectly legal. But I still prefer to have the semi-colon in there, so I’m going to put them in there. And then we need to deal with this main characters. That needs to be an unordered list of the main characters. So we need some HTML.

So that will be a UL tag. And then between the UL tag, we’ll have a list item. And inside that one list item, we need a PHP block. So, again, use the shorthand for ‘echo’. And then between the two, $characters is an indexed array. So we need to start from 0. So we’ll have $characters and then in square brackets, 0. And then I’ll put in that semi-colon again.

And I can now duplicate that four times, so one, two, three, four. And then just change the index here to 1, 2, 3 and 4. And that’s all we need to do. So let’s just save that and then load the page into a browser and see what we get. “The Hitchhiker’s Guide to the Galaxy”, by Douglas Adams. “The Hitchhiker’s Guide to the Galaxy” and there is the description. And we’ve got the period with the full stop at the end.

Then the main characters, an unordered list. So congratulations, if you got a result similar to mine. If you got error messages, it probably means that you have mismatched quotes,brackets or braces. Check your code carefully against my completed version in embed_end.php in the ch02, 02_11 folder. We’ll look at troubleshooting errors in Chapter 8.But let’s get back to the code to review what we’ve done.

I think that the tricky aspects of this challenge were handling the associative array elements.Particularly with the double quoted string here on line 23 and then using the concatenation operator on line 24. Something else that might have caught you out is remembering that index arrays start at 0. To be honest, I found using the concatenation operator here on line 24 trickier than embedding associative array elements in double quotes.

But it doesn’t matter which style you use, there’s often more than one way to do something in PHP. And as you’ll discover later in this course, we could have used a loop to generate the unordered list of characters. It involves less code and is quicker. But let’s not get ahead of ourselves. Next up on the agenda is using conditional statements to make decisions.

Leave a Reply