606

Understanding the Basics
of PHP

Just as JavaScript is one of the most popular client-side programming languages, PHP is one of the most popular server-side languages. As open-source software, it is used in countless Web applications, including blogging applications like WordPress. If you log into WordPress as administrator and select “Appearance,” then “Editor,” and then “Page template” (page.php), you’ll notice a few significant details about PHP:

  • .php in the file name tells the server to process the file as PHP code rather than straight HTML.
  • <?php indicates the beginning of a section of PHP code in a file.
  • ?> indicates the end of a section of PHP code in a file.

You’ll also notice that sometimes PHP is used to insert information within an HTML tag:

<h1 class="entry-title"><?php the_title(); ?></h1>

Sometimes it outputs an HTML tag, as in the case of this span tag:

<?php edit_post_link(__(ˈEditˈ, ˈsomestyleˈ),ˈ<span class="edit-link">ˈ,ˈ</span>ˈ) ?>

And sometimes it imports another document entirely:

<?php get_header() ?>

PHP for Templates

Server-side code is often used to create a new HTML document from separate files on the spot. Rather than save every blog page as an individual file, PHP can draw a post from a database—with author name, creation date, and so on—and output it all within an HTML template. Shopping sites do a similar thing with products’ names, descriptions, images, and current prices.

PHP for Security

Because its work is done on the server, PHP code is not visible to the end user. This helps to safeguard information such as details of database access, visitor addresses, credit card numbers, and so on. To discover just how different a PHP file is from the code it delivers to the browser, visit any page on your blog, choose “View Source” from the browser menu bar, and compare what you see there with what you see as administrator in the blog dashboard.

Your Turn

  1. In your WordPress blog (or other PHP application), choose a place in the footer to insert a current-year copyright statement: Copyright &copy; <?php echo date("Y")?> Explain what each part of that code does.
  2. Choose blog elements that you wish to hide (perhaps the RSS block or the archives section) and use PHP comment tags to do so (<?php /* to start hiding and */ ?> to end). Visit your blog with your browser to check the results.
 

Additional Resources