PHP and MySQL Programming/Syntax Overview

From Wikibooks
Jump to: navigation, search

PHP is divided into blocks, which can be inserted into a standard HTML page. All PHP statements must inserted into one of these blocks.

<?php       //Standard of a PHP block
            //Contents of a PHP block
?>          //End of a PHP block

Statements

All PHP statements must be finished with a semi-colon:

echo "Hello World";      //Correct statement
echo "Nice knowing you"  //Syntax error

The semi-colon is a seperator, and enables instructions to be distinguished from one another so this program:

''
<?php
 
echo "Hello world"; // Print the words "Hello world to the screen"
 
/* Using a for loop to print*
  * 12 exclaimation marks  */
for($i=0; $i<12; $i++) //Until i is 11
{
      echo "!"; //Print an exclaimation mark
}
 
?>

is exactly the same as something like this:

echo "Hello world";for($i=0;$i<12;$i++){echo "!";}

Comments

You'll also have noticed that within the blocks of code there, there are pieces of text explaining what the code is doing - these are comments. A double-slash (//) indicates a single-line comment, and a slash-asterisk asterisk-slash can be used to extend over multiple lines. These are essential to letting you (and others) understand your code. You'll find some form of commenting in nearly every form of programming language.

Functions

Within PHP there are a number of functions. These are found in most programming languages and are designed to save time by automating complex operations. Say for example I was writing a script that needed to work out x ^{3} or x \times x \times x. My script might look something like this:

echo 2*2*2; // Outputs 8
 
echo 5*5*5; // Outputs 125
...

This is error prone and and a lot of typing. What if I typed 5*5*4 by mistake? It might take me ages to find the problem. To solve this, PHP uses functions. I'm going to rewrite that fragment using them.

function cube($x=0) // This is how to declare a function. The keyword function, followed by the function name and a pair of round brackets containing any arguments. I've added a default value in case I forget to pass a variable.
{
     return $x * $x * $x; // The return keyword tells PHP to send the information that follows back to the script. PHP does not have to send any information back.
}
 
echo cube(2); // Outputs 8
 
echo cube(5); // Outputs 125
 
echo cube();  // Outputs 0

If you call a function giving it the wrong number of arguments, or the wrong type of arguments, then PHP may deliver an error. If I modified the function so that the argument was just $x and called cube(), then because it has no default value, it delivers an error. Passing default values to functions can have it's uses when you're creating functions that require a lot of arguments.

Conditional Statments

Most programming languages also have some form of conditional statements. This controls the execution of sets of instructions, depending on whether certain conditions are met. To demonstrate how these might be useful, let's modify the function cube().

function cube($x=0)
{
    if(is_numeric($x)) {return x*x*x;}
    else {return "Not a supported value.";}
}
 
echo cube("Cassandra"); //Output: Not a supported value.

Without the if statement, that function would have attempted to multiply "Cassandra" by herself, and given some unpredictable results.