Raspberry Pi Day 5 - Programming php

In the previous day you explored how to install PHP and use a simple function within an HTML page to display some information about the server,

<?php phpinfo(); ?>

The <? is called an open delimiter and this is where you will place the PHP code.  When you are finished creating the PHP code you will need to let PHP that you are finishing this code with a closing delimiter which is a ?>.

Notice that the function phpinfo(); ends with a semi colon. This is letting PHP know that we have finished with the line of code.  This is very similar to a sentence and ending it with a full stop or period.  The PHP will not be delivered to the web browser because the server will run the PHP code and return html or java code.

PHP code can become quite long and a complex spanning a number of pages.  To keep track of what is going on it is always a good idea to use comments.  Comments allow you to make notes about the code to remind yourself and others of what is going on without going into the detail of the code.

 

<?php

                /* This is a comment and the function below displays php information */
phpinfo();

?>

All comments are ignored by PHP and are only displayed in the code.  This type of comment is known as a multiline comment because it can span multiple lines.

<?php

                /* This is a commen and

                The function below

                Displays php information */

                phpinfo();

?>

Another type of comment is the single line comment.  Two forward slashes // are used for this.

<?php 

                // this is a single line comment
phpinfo();
?>

 

We will create a php page that will display the current date.  Create a new page by enter the following.

$ cd /var/www

$ sudo nano datepage.php

Enter the following code into the datepage.php document

 

<html>

<head>
<title>The date</title>

</head>

<body>The date is

<? php

            //this will display the date

            echo date(“y/m/d”);

?>

</body>

</html>

 

Save the file.  CTRL-X and confirm that you want to save it.

Open a web browser and in the address bar enter the following.

http://localhost/datepage.php

The output will display the current date.

The date is 2014/04/12.

Beginning raspberry pi programming tutorials

Next Raspberry Pi Beginning Programming Week 7 >>