Some notes on PHP syntax.
<?php
?>
-
Greater than
- < Less than
- <= Less than or equal to
-
= Greater than or equal to
- == Equal to
- != Not equal to
if (True) {
echo “hello”;
}
else if (False) {
echo “this is False”;
}
else {
echo “test”;
}
switch (2) {
case 0:
echo ‘The value is 0’;
break;
case 1:
echo ‘The value is 1’;
break;
case 2:
echo ‘The value is 2’;
break;
default:
echo “The value isn’t 0, 1 or 2”;
}
}>
The switch statement does have an alternative syntax for better readability.
switch ($variable):
case 0:
echo “The value is 0”;
break;
endswitch;
<?php
$array = array(“Egg”, “Tomato”, “Beans”);
?>
Accessing elements by offset:
$tens = array(10, 20, 30, 40, 50);
echo $tens[2];
Values can be deleted with unset
:
unset($languages[0]);
associative array:
$assocCar = array(‘year’ => 2012,
‘colour’ => ‘blue’,
‘doors’ => 5,
‘make’ => “BMW”);
// Echoes the first five even numbers
for ($i = 2; $i < 11; $i = $i + 2 ){
echo $i;
}
$langs = array(“JavaScript”,
“HTML/CSS”, “PHP”,
“Python”, “Ruby”);
foreach ($langs as $lang) {
echo “<li>$lang</li>”;
}
unset($lang);
while(condition) {
do_something();
}
alternate syntax for while:
while(condition):
endwhile;
do-while syntax:
do {
somecommand;
} while(condition);
String functions:
$length = strlen(“Olivier”);
print $length;
$uppercase = strtoupper($myname);
$lowercase = strtolower($myname);
Array functions:
sort($array);
rsort($array);
join(“, “, $array)
strpos()
: find dhe position of the forst occurence of a substring in a string.
Function syntax:
function name(paramenters){
statement;
return “somestring”;
}
Simple class:
class Person {
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
}
$teacher = new Person();
$student = new Person();
# Accessing public elements of the class.
echo $teacher->$isAlive;
}
Same class with constructor:
class Person {
public function __construct($firstname, $lastname, $age){
$this->$firstname = $fistname;
$this->lastname = $lastname;
$this->age = $age;
}
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
}
$teacher = new Person(“boring”,”12345”,12345);
$student = new Person(“Bla”, “Blub”, 30);
echo $teacher->$isAlive;
}
Dog class:
class Dog {
public $numLegs = 4;
public $name;
public function __construct($name){
$this->name = $name;
}
public function bark(){
return “Woof!”;
}
public function greet(){
return “Hi, I am $this-name”;
}
}
$dog1 = new Dog(“Barker”);
$dog2 = new Dog(“Amigo”);
echo $dog1.bark();
echo $dog2.greet();
class Shape {
public $hasSides = true;
}
class Square extends Shape {
}
if (property_exists($square, “hasSides”)){
echo “I have sides!”;
}
Overriding Parent Methods:
class Shape {
$sides = true;
}
class Square extends Shape {
$sides = 4;
}
- Just overwrite the property in the child class. The child class has precedence over the parent class.
- Methods that start with the
final
keyword can not be overwriten by child classes.
Constants can not be overwriten:
class Person {
}
class Ninja extends Person {
const stealth = “MAXIMUM”;
}
// ...and here!
echo Ninja::stealth;
}
With the static
keyword a class’s property can be accessed without having to create an instance of the class.
class King {
// Modify the code on line 10...
public static function proclaim() {
echo “A kingly proclamation!”;
}
}
// ...and call the method below!
echo King::proclaim();