PHP Basics

From TRCCompSci - AQA Computer Science
Revision as of 13:40, 24 December 2016 by Admin (talk | contribs) (PHP Selection)
Jump to: navigation, search

This section needs expansion.
You can help by adding to it.

PHP is a server side scripting language, which is run on the server before the page is served to the client. Essentially it dynamically creates pages on the fly. It can be written on it's own our within normal standard HTML etc. It must be stored on a running webserver and you can only access it via it's URL in a browser. The page the client will receive will have all of the server side elements replaced with the output of the code. If your viewed source you would only see standard HTML and text.

PHP Tags

PHP code must be contained within PHP tags, these tags can be placed anywhere in the page because they are all run before the page is served to the client.

1 <?php // the opening PHP tag
2 // your PHP code
3 ?> // the closing PHP tag

Within your PHP tags every line must be terminated with a semi colon.

PHP Comments

Like all languages PHP supports comments, these can greatly help the maintenance of a project because the developer / programmer may not be the person who maintains the project when it is live. The comments are exactly the same as in C#:

1 // This will comment a single line or include a comment at the end of that single line

Alternatively:

1 /*
2 This will comment all lines
3 between the start and
4 the end comment marks
5 */

Creating a Hello World in PHP

<syntaxhighlight lang="php" line> <?php echo "hello world"; ?>

hello world

echo is used to write text to the page, you could also echo html code or variables:

1 <?php echo "<p>Hello World<br>Hola Mundo</p>"; ?>

1 <p>Hello World<br>Hola Mundo</p>

Hello World
Hola Mundo

You could also do this:

 1 <html>
 2 <head> <title> Hello World </title> </head>
 3 <body>
 4 <p>
 5 <?php echo "Hello World"; ?>
 6 <br>
 7 <?php echo "Hola Mundo"; ?>
 8 </p>
 9 </body>
10 </html>

 1 <html>
 2 <head> <title> Hello World </title> </head>
 3 <body>
 4 <p>
 5 Hello World
 6 <br>
 7 Hola Mundo
 8 </p>
 9 </body>
10 </html>

Hello World
Hola Mundo

PHP Variables

In PHP you don't declare variables before they are used, instead you just assign it a value and PHP will automatically convert it to the correct data type in the background. All variables in PHP start with the dollar sign ($) followed by the name of the variable. The name must start with a letter, and can only contain alpha-numeric characters and underscore. Variables are also case sensitive.

1 <?php 
2 $name = "wayne";
3 $age = 21;
4 $alive = true; 
5 ?>

The above example creates 3 different variables, notice that you don't need to declare the data types.

1 <?php 
2 $message = "<p> Hello World <br> Hola Mundo </p>"; 
3 echo $message; 
4 ?>

You can join together text using a period . :

1 <?php 
2 $message = "Hello World"; 
3 echo "<p>".$message."</p>; 
4 ?>

Calculations can be performed as normal but PHP will dynamically alter data types to suit. so this:

1 <?php 
2 $number = 5;
3 $text = "5" 
4 echo $number + $text; 
5 ?>

will produce the output 10.

It is also important to note that variables inside double quotes will be displayed as their value, while variables in single quotes will only produce what you wrote. For example:

1 <?php 
2 $a = "Hello";
3 echo "$a<br>" . '$a';
4 ?>
5 //Output:
6 //Hello
7 //$a

PHP Constants

Constants are like variables however once they are declared they cannot be changes or undefined. The name of a constant must start with either an underscore or letter.

1 <?php 
2 define (Message, "<p> Hello World <br> Hola Mundo </p>"); 
3 echo Message; 
4 ?>

You can also include another parameter (true) to make the constant insensitive to the case:

1 <?php 
2 define (Message, "<p> Hello World <br> Hola Mundo </p>", true); 
3 echo mESSAGE; 
4 ?>

PHP Operators

PHP Selection

The if statement executes some code if one condition is true:

if (condition) {
    code to be executed if condition is true;
}


The example below will output "Have a good day!" if the current time (HOUR) is less than 20:

<?php
$t = date("H");

if ($t < "20") {
    echo "Have a good day!";
}
?>


The if....else statement executes some code if a condition is true and another code if that condition is false.

if (condition) {
    code to be executed if condition is true;
} else {
    code to be executed if condition is false;
}

The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:

<?php
$t = date("H");

if ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}
?>

PHP Repetition

PHP Functions

PHP & Forms

PHP Other Commands