| Program Structure |
<html>
<head>
<title> Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
class Base
{
function takes($input)
for ($input = 1; $input <= 10; $input++) {
echo $input;
}
}
}
$b = new Base();
$b->takes();
?>
</body>
</html>
|
| Comments |
| Data Types |
boolean
integer
float
string
array
object
resource
NULL
mixed
number
callback
|
| Arrays |
<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo $arr["somearray"][6];
echo $arr["somearray"][13];
echo $arr["somearray"]["a"];
?>
<?php
array(5 => 43, 32, 56, "b" => 12);
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
<?php
$arr = array(5 => 1, 12 => 2);
$arr[] = 56;
$arr["x"] = 42;
unset($arr[5]);
unset($arr);
?>
<?php
$fruits = array ( "fruits" => array ( "a" => "orange","b" => "banana",
"c" => "apple"),
"numbers" => array ( 1,2,3,4,5,6),
"holes" => array ("first",
5 => "second",
"third")
);
echo $fruits["holes"][5];
echo $fruits["fruits"]["a"];
unset($fruits["holes"][0]);
$juices["apple"]["green"] = "good";
?>
|
| Operators |
* / % + -
= += -= *= /= .= %= &= |= ^= <<= >>=
& | ^ ~ << >>
< <= > >= <> == != === !==
++ --
&& || ! and or xor
?:
|
| Decisions |
<?php
if ($a > $b) {
echo "a is bigger than b";
} else {
echo "a is NOT bigger than b";
}
?>
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
<?php
if ($a == 5): ?>
A is equal to 5
<?php endif; ?>
<?php
if ($a == 5):
echo "a equals 5";
echo "...";
elseif ($a == 6):
echo "a equals 6";
echo "!!!";
else
echo "a is neither 5 nor 6";
endif;
?>
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
|
| Loops |
<?php
$i = 1;
while ($i <= 10):
echo $i;
$i++;
endwhile;
?>
<?php
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
?>
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
<?php
for($i = 1; $i <= 10; $i++ )
{
if($i == 2)
continue;
elseif($i==5)
break;
}
?>
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
?>
|
| Casting & Conversion |
<?php
$foo = "0";
$foo += 2;
$foo = $foo + 1.3;
$foo = 5 + "10 Little Piggies";
$foo = 5 + "10 Small Pigs";
?>
<?php
$foo = 10;
$str = "$foo";
$fst = (string) $foo;
?>
|
| Enumeration |
<?php
enum MyEnum {a,b,c,d,e};
$enum = new OrbitEnum ("MyEnum");
echo $enum->a; /* write 0 */
echo $enum->c; /* write 2 */
echo $enum->e; /* write 4 */
?>
|
| Namespaces |
|
| Classes & Structs |
public
protected
private
static
<?php
class Base
{
}
class Child extends Base
{
}
?>
|
| Interfaces |
<?php
interface iTemplate
{
public function setVariable($name, $var);
}
class Template implements iTemplate
{
private $vars = array();
public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
}
?>
|
| Functions |
<?php
function takes($input, &$string, $type = "cappuccino")
{
function body
}
?>
|
| Exception Handling |
<?php
try {
$error = 'Always throw this error';
throw new Exception($error);
echo 'Never executed';
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo 'Hello World';
?>
|
| Constructors/Desctructors |
<?php
class MyClass {
function __construct() {
print "In constructor\n";
$this->name = "MyClass";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
}
?>
|
| Properties |
public $my_static = 'foo';
public function getValue() {
return self::$my_static;
}
|