phpSlash.org

PHP Syntax

PHP's syntax is similar to other languages like C, Java or Perl.

Variables

Unlike programming languages like C, you do not have to explicitly declare a variable in PHP. You only need assign a value to a variable and it is declared automatically. Variables in PHP are preceded by the $ symbol.

$integer_variable = 10;
$string_variable = "this is a string";
$decimal_variable = 3.14;
Operators

PHP supports the standard operators, including +, -, *, /, =, ++ and --. Boolean operators include == (equals), != (not equals), > (greater than), and < (less than).

Examples:

$x = 5 + 5;
$y = 10 * 11;
$z = $x / $y;
$x++;
$y--;

if ($x == $y) { }
if ($y != $z) { }
Conditions

Conditions in PHP are resolved using the if…then..else statement.

if ($variable == "something") {
  execute some code…
}
else {
  execute some different code…
}
Loops

The two main loops in PHP are the for…next loop and the while loop.

for ($count = 0; $count < 10; $count++) {
  execute some code…
}

while ($count < 10) {
  execute some code…
}

<-- Previous   Next -->