Using PHP Code on Our Site3:09 with Alena Holligan
In this video, we’ll convert the home page HTML file into a PHP file and start adding some PHP code.
Update to html
index.php unordered list for our random items should have a class of "items".
Welcome back. 0:00 We're ready to start building our media library. 0:01 We're going to start by setting up a separate file for 0:04 the header that we can include in all the pages of our library. 0:06 This will make our site easier to maintain 0:10 by giving us one place to update shared elements, 0:13 like navigation links, instead of needing to update each page individually. 0:16 Go ahead and launch the workspace that is on this page or 0:20 download the project files to work locally. 0:23 Most web servers, including the one in Workspaces, are configured to display 0:26 a file named index.html or index.php as the main file by default. 0:30 If you click the preview icon, a new window opens. 0:37 In the index.html file is displayed. 0:40 Go back to your workspace and open index.html. 0:44 You can see that the file references an external style sheet here, 0:46 and down on the content, we're referencing some external images for the suggestions. 0:51 This page also has a number of links. 0:58 Both here in the suggestions. 1:02 And up here in the navigation. 1:06 We haven't built these pages yet, but we will. 1:08 Instead of actual links, you'll see hash signs as placeholders for now, 1:11 and we'll update those links as we build out the pages. 1:14 Before we start adding those new pages, 1:18 let's explore how PHP files work on a server. 1:20 Let's start by changing the title tag in the copyright date to use PHP. 1:23 We'll start with a simple echo command. 1:26 And we'll echo out the title as a string. 1:29 This isn't actually very useful. 1:38 But we'll be extending this in a later video to use a variable instead of 1:40 a string. 1:44 Using a variable for the title will allow us to use the same HTML on all the pages, 1:44 but still have individual titles for each page. 1:49 Now let's go to the footer and echo out the year. 1:53 We'll use the built in function date with y for our year. 1:59 Let's save this page and go back to the browser. 2:06 When I refresh this page, 2:09 we'll see something that you might not have expected to see. 2:11 The PHP code here in the title tag, and nothing here for the date. 2:14 Our Web server did not execute the PHP code in this file. 2:19 Why not? 2:23 Because our file has an extension of .html. 2:24 Our server will only execute the PHP code in a PHP file. 2:28 If that file has an extension, it identifies as PHP. 2:32 This extension is usually .php. 2:36 Let's go back to our workspaces and rename the index file. 2:38 We'll rename this from index.html to index.php. 2:43 Now when we preview this file in the browser, the Web server recognizes 2:51 the extension, executes the PHP code and displays the title and the year correctly. 2:55 So now we have the home page working as a PHP file. 3:01 It's processing a little bit of code to handle the title and the header, 3:04 and the date and the footer. 3:07
You need to sign up for Treehouse in order to download course files.Sign up