PHP Review

The PHP Tag

Basic Syntax

Simple Example

<!DOCTYPE html>
<html>
<head>
  <title>Hello World</title>
</head>
<body>

<?php
$message = "Hello world";
echo "<p>$message</p>";
?>

</body>
</html>

Variable Naming Rules

PHP Types

The String Type

Multi-line strings

Heredoc Syntax Rules

Arithmetic Operators

Operator Description Example
+ Addition $a + 3
- Subtraction $a - 3
* Multiplication $a * 3
/ Division $a / 3
% Modulus $a % 3
++ Increment ++$a
Decrement –$a

Assignment Operators

Operator Example Equivalent to
= $a = 3 $a = 3
+= $a += 3 $a = $a + 3
-= $a -= 3 $a = $a - 3
*= $a *= 3 $a = $a * 3
/= $a /= 3 $a = $a / 3
%= $a %= 3 $a = $a % 3
.= $a .= $b $a = $a . $b

PHP Implicit Type Coercion

Explicit Type Casting

PHP Constants

Checking Syntax

Control Flow

The PHP Boolean Type

Equality & Comparison Operators

Operator Description Example
== equal to $a == 3
=== identical to $a === 3
!= not equal to $a != 3
!== not identical to $a !== 3
> greater than $a > 3
< less than $a < 3
>= greater than or equal to $a >= 3
<= less than or equal to $a <= 3

Logical Operators

Operator Description Example
&& and $a == 3 && $b == 0
and low-precedence and $a == 3 and $b == 0
|| or $a == 3 || $b == 0
or low-precedence or $a == 3 or $b == 0
! not !($a == $b)
xor exclusive or $a xor $b

The Equality and Identity Operators

Selection

Iteration

PHP Arrays

Constructing Arrays with Integer Keys

Associative Arrays

Key Casts

Printing Arrays

The array keyword

The foreach loop

Multidimensional Arrays

Some Useful Array Functions

Superglobals

Defining a PHP Function

function function_name([parameter [, ...]])
{
    // Statements
}

The return keyword

Variable Scope

Assigning Variables in Global Scope

Static Variable Example

<?php
counter();
counter();

function counter() {
    static $count;
    echo "<p>$count</p>";
    $count += 1;
}
?>

Including and Requiring Files