Jump to content

PHP and MySQL Programming/Syntax Overview

From Wikibooks, open books for an open world

PHP tags:

[edit | edit source]

<?php [code] ?>

[edit | edit source]
  • Enclose PHP code
  • Embedded in normal HTML code
  • Within PHP tags, statements are separated by a ; (generally also followed by a new line).

Example:

 <?php
 print "Hello World\n";
 $date = date("Y-m-d H:i:s");
 print "The date and time at the moment is $date";
 ?>

Commenting

[edit | edit source]

Comments out one line

Example:

echo "Hello"; // Everything from the hash is commented out

Example:

// This entire line is commented out

Same function as //

/* (text) */

[edit | edit source]

Comments out everything between the /* and the */

Example:

 /*All of this is
 commented out.
 Even this line!*/

Variables

[edit | edit source]

Variables in PHP are denoted by the $ prefix.

Example:

 $a = "Hello World"; # this assigns the string "Hello World" to $a.
 $b = "$a, I'm Ralfe"; # this assigns "Hello World, I'm Ralfe" to $b.
 $b = $a.", I'm Ralfe"; # exactly the same as the previous example.

PHP supports dynamic variables.

Example:

$c = “response”;

$$c = “Hello Ralfe”; # this assigns “Hello Ralfe” to $response.


PHP variables do not have to be declared ahead of time, nor do they require a type definition. PHP handles all data type conversions.

  • Example:
$a = 4;

$b = 12;

print “The value of a is $a.; # Uses a as a String.

$c = $a + $b; # $a is now used as an Integer again.

PHP supports Boolean variables, which can be assigned either a one or a zero, or a true or false.

Example:

$a = true;

$b = 1;  # $a and $b are equal, though not identical.

$c = false;

$d = 0; # $c and $d are equal, though not identical.

Helpful Definitions:

[edit | edit source]
Equal vs Identical
Equal
values are equal in value, but may be of differing types. Uses == comparison operator. E.g. false is equal to 0.
Identical
values are equal in value and of the same type. Uses === comparison operator. E.g. false is identical only to false, it is not identical to 0.

Operators

[edit | edit source]

Arithmetic Operators

[edit | edit source]

Example:

$a = 4;

$b = 2;

$a + $b = 6;  // Addition

$a - $b = 2;  // Subtraction

$a * $b = 8;  // Multiplication

$a / $b = 2;  // Division

$a % $b = 0;  // Modulus

$a++;  // Increment

$a--;  // Decrement

Assignment Operators

[edit | edit source]

Example:

$a = 4;

$b = $a;

// $b = 4;

Comparison Operators

[edit | edit source]

Example:

$a == $b // test if two values are equal

$a === $b // test if two values are identical

$a != $b // test if two values are not equal

$a !== $b // test if two values are not identical

$a < $b // test if the first value is less than the second

$a > $b // test if the first value is greater than the second

$a <= $b // test if the first value is less than or equal to the second

$a >= $b // test if the first value is greater than or equal to the second

Concatenation

[edit | edit source]

Example:

$a = "Fill the halls ";

$b = "with poisoned ivy...";

$c = $a . $b;  # the '.' operator concatenates two variables.

// $c = "Fill the halls with poisoned ivy...";

Arrays

[edit | edit source]

PHP supports both numerically indexed arrays as well as associative arrays.

Example:

$a = array(1, 2, 3, 4);

// $a[0] = 1;

// $a[1] = 2;

// $a[2] = 3;

// $a[3] = 4;

$b = array("name" => "Fred", "age" => 30);

// $b['name'] = "Fred";

// $b['age'] = 30;

Decision and Loop Statements

[edit | edit source]

If ... else statement

[edit | edit source]

Example:

 $a = 1;
 $b = 10;
 
 if ($a > $b) {
    echo "a is greater than b";
 }
 else if ($a == $b) {
    echo "a is equal to b";
 }
 else {
    echo "a is not greater than b";
 }
 
 // OUTPUT:
 // a is not greater than b

Switch statement

[edit | edit source]

Example:

 $a = 100;
 
 switch($a) {
    case(10):
       echo "The value is 10";
       break;
 
    case(100):
       echo "The value is 100";
       break;
 
    case(1000):
       echo "The value is 1000";
       break;
 
    default:
       echo "Are you sure you entered in a valid number?";
 }
 
 // OUTPUT:
 // The value is 100

For statement

[edit | edit source]

Example:

 for ($i = 0; $i < 10; $i++) {
    # initialize $i ; while condition ; increment statement
    echo $i;
 }
 
 // OUTPUT:
 // 0123456789

Foreach statement

[edit | edit source]

Example:

 $a = array(1, 2, 3, 4, 5);
 
 foreach ($a as $val){
    echo $val;
 }
 
 // OUTPUT:
 // 12345

While statement

[edit | edit source]

Example:

 while ($row = mysql_fetch_row($result)){
    print $row[0];
 }

Do ... while statement

[edit | edit source]

Example:

 $i = 0;
 # Note that it might seem that $i will
 do{
    # never be printed to the screen, but
    print $i;
    # a DO WHILE loop always executes at
 } while ($i >0);
 # least once!

Functions

[edit | edit source]

Example:

 function square_number($number) {
    return ($number * $number);
 }
 
 $answer = square_number(10);
 echo "The answer is {$answer}";
 
 // OUTPUT:
 // The answer is 100

Classes

[edit | edit source]

Example:

 class dog {
    var $name;
    
    function __construct($name){
       $this->name = $name;
    }
    
    function bark(){
       echo "Woof! Woof!";
    }
    
    function who_am_i() {
       echo "My name is {$this->name}, and I am a dog";
    }
 }
 
 $the_dog = new dog("John");
 $the_dog->who_am_i();
 
 // OUTPUT:
 // My name is John, and I am a dog

As of PHP 5.3.3, functions with the same name as the class will no longer act as a constructor. Versions prior to 5.3.3 the

function __construct($name)

line could be replaced with

function dog($name)

to achieve the same effect. [1]


  1. http://php.net/manual/en/language.oop5.decon.php