PHP Introduction Part 2


Let’s write some code. The line below will go between your two PHP tags. You need a semicolon at the end of each line, except for the last line in a php script.

<?php // This is a comment echo "My first PHP script!"; ?>

Below is a variable declaration ($color) which must begin with a question mark. The period operator concatenates. The variables are case sensitive in PHP. The other thing to note is that you can include HTML elements within the quotes of the echo command.

<?php $color="blue"; echo "My car is " . $color . "&lt;br&gt;"; echo "My house is " . $COLOR . "&lt;br&gt;"; echo "My boat is " . $coLOR . "&lt;br&gt;"; ?>

So some of this information has been borrowed from the following web site:

http://www.w3schools.com/php/php_variables.asp

If you go there they will continue on and talk about SCOPE.

WordPress Functions

Have a look at the footer of this page. Right after the copyright notification is a short message that was made with php echo inside a function that I wrote and put into the footer.php template of the Child Theme for this web site. I wrote the function in the functions.php file of the Child Theme. I wrote it as follows, without the use of the bracket question mark php syntax. It is not needed in that file.

function haveagreatday() {
    echo "Have a great day!";
}

How do you get the message to display in the footer part of the web site? It’s easy, assuming that you already have a footer.php file in your Child Theme (or Theme). You just call the function as shown below, using the php delimiters.

 
<?php haveagreatday(); ?>

WordPress has a Codex on the web that you can use to get information about PHP functions. If you need to get the current logged in user check out wp_get_current_user() in the Codex.