php basics

MMP 460 > Week 5

Now that we have looked at the structure of header.php let's take a closer look at how php works

php is a programming language that can be embedded into an HTML template using special <?php ?> tags.

This allows developers to infuse dynamic content into a static template. Instead of hard coding everything that appears on the page, the php scripts can be used to load content specific to various parts of your website.

<?php PHP_code_goes_here ?>

A simple line of php looks like this. All of the code for this line must go between the open and close brackets. However, you can add multiple lines of php inside the brackets, and multiple php tags in your page.

php supports basic programming functions and operations and has special commands:

// echo will write text into the html document
<?php echo "hello world" ?>
<?php echo 2 + 2 ?>

php supports variables with the $ declaration.

<?php $message = "hello world!" ?>
<?php echo $message ?>

Multiple lines of php inside one tag must be separated by a semi-colon ; like most programming languages.

<?php 
$message = "hello world!";
echo $message; 
?>

php has to be run on a server so in order to test we can use a php console.

Let's look at some of the lines of php in our theme starter.

<meta charset="<?php bloginfo( 'charset' ); ?>">

In this line, bloginfo() is a php function that takes a parameter or argument, which is 'charset'. It uses this argument to return some data saved in WordPress.

It's used again in the site branding section with different arguments:

<?php bloginfo( 'name' ); ?>