Statements
Control flow
if, else, elseif/else if
$number = 10;
if ($number > 0){
print 'greater than zero';
} elseif ($number < 0) {
print 'less than zero';
} else {
print 'equal to zero';
}
//=> greater than zero
switch
$number1 = 10;
$number2 = 10;
$operator = '+'; // (+, -, *, /)
$result = 0;
switch ($operator) {
case 'add':
case '+':
$result = $number1 + $number2;
break;
case '-':
$result = $number1 - $number2;
break;
case '*':
$result = $number1 * $number2;
break;
case '/':
$result = $number1 / $number2;
break;
default:
$result = 0;
}
var_dump($result); //=> int(20)
Iterations
for
$names = ['Alice', 'Bob', 'Charlie'];
for($index = 0; $index < sizeof($names); $index++){
print($names[$index]."\n");
}
//=>
// Alice
// Bob
// Charlie
foreach
foreach (range(0, 9) as $number) {
echo $number;
}
//=> 0123456789
$names = [
0 => 'Alice',
'admin' => 'Bob',
'client' => 'Charlie'
];
foreach($names as $name){
print($name."\n");
}
//=>
// Alice
// Bob
// Charlie
foreach($names as $key=>$name){
print("$key => $name\n");
}
//=>
// 0 => Alice
// admin => Bob
// client => Charlie
while
$result = '';
while (strlen($result) < 4) {
$result .= 'x';
print "$result\n";
}
//=>
// x
// xx
// xxx
// xxxx
do…while
$result = '';
do {
$result .= 'x';
print "$result\n";
} while(strlen($result) < 4);
//=>
// x
// xx
// xxx
// xxxx
Alternative syntax
endif;
, endwhile;
, endfor;
, endforeach;
, or endswitch;
:
$number = 10;
if ($number > 0):
print 'greater than zero';
elseif ($number < 0):
print 'less than zero';
else:
print 'equal to zero';
endif;
//=> greater than zero
Includes
require()
Example 1
require
├── test.php
└── util.php
<?php
function sum($a, $b) {
return $a + $b;
}
?>
<?php
include("util.php");
echo sum(1, 1);
?>
Example 2
require-path-child
├── lib
│ └── util.php
└── test.php
codes/require-path-child/lib/util.php:
<?php
function sum($a, $b) {
return $a + $b;
}
?>
codes/require-path-child/test.php:
<?php
require("lib/util.php");
echo sum(1, 1);
?>
Example 3
require-path-parent
├── lib
│ └── util.php
└── src
└── test.php
codes/require-path-parent/lib/util.php:
<?php
function sum($a, $b) {
return $a + $b;
}
?>
codes/require-path-parent/src/test.php:
<?php
require(__DIR__."/../lib/util.php");
echo sum(1, 1);
?>
include()
include
├── test.php
└── util.php
<?php
require("util.php");
echo sum(1, 1);
?>
<?php
function sum($a, $b) {
return $a + $b;
}
?>
require_once()
Error
require-error
├── lib
│ ├── math.php
│ └── util.php
└── test.php
codes/require-error/lib/math.php:
<?php
function sum($a, $b) {
return $a + $b;
}
?>
codes/require-error/lib/util.php:
<?php
require("math.php");
function minus($a, $b) {
return $a - $b;
}
?>
<?php
require("lib/util.php");
require("lib/math.php");
echo sum(1, 1);
// =>
// PHP Fatal error: Cannot redeclare sum() (previously declared in
// codes/require-error/lib/math.php:2) in
// codes/require-error/lib/math.php on line 4
?>
Valid
require_once
├── lib
│ ├── math.php
│ └── util.php
└── test.php
codes/require_once/lib/math.php:
<?php
function sum($a, $b) {
return $a + $b;
}
?>
codes/require_once/lib/util.php:
<?php
require_once("math.php");
function minus($a, $b) {
return $a - $b;
}
?>
<?php
require_once("lib/util.php");
require_once("lib/math.php");
echo sum(1, 1);
?>
include_once()
include_once
├── lib
│ ├── math.php
│ └── util.php
└── test.php
codes/include_once/lib/math.php:
<?php
function sum($a, $b) {
return $a + $b;
}
?>
codes/include_once/lib/util.php:
<?php
require_once("math.php");
function minus($a, $b) {
return $a - $b;
}
?>
<?php
require_once("lib/util.php");
require_once("lib/math.php");
echo sum(1, 1);
?>