if
has only one section: Condition
if (Condition) {
// Code to be executed
}
for
loop has three sections divided by ;
for (Initialization; Condition; Increment/Decrement) {
// Code to be executed
}
The loop prints numbers from 0 to 10
for ($i = 0; $i < 10; $i ++) {
echo $i . "\n";
}
function printGreeting() {
print "Prints: Hello world!\n";
}
printGreeting();
Function that returns something
function returnGreeting() {
return "Returns: Hello world!\n";
}
echo returnGreeting();
Parameterized function that doubles the number
function doubleMe($number) {
$result = "$number times 2 is: " . $number * 2 . "\n";
return $result;
}
echo doubleMe(4);
Function calculates the sum of two numbers
function add($num1, $num2) {
$sum = $num1 + $num2;
$result = "Sum of $num1 and $num2 is: $sum";
return $result;
}
echo add(4, 5);
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} elseif($d == "Sun"){
echo "Have a nice Sunday!";
} else{
echo "Have a nice day!";
}
echo "\n";
Outputs Have a good morning!
if the current time is less than 10, and Have a good day!
if the current time is less than 20. Otherwise it will output Have a good night!
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
Prints Adult
If $age
is more than 18
, and Teenager
if the variable is less than 12. Otherwise it prints Child
$age = 20;
if ($age > 18) {
echo 'Adult';
} else if ($age > 12) {
echo 'Teenager';
} else {
echo 'Child';
}
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} else{
echo "Have a nice day!";
}
echo "\n";
Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
If $age
is more than 18
, prints Adult
otherwise prints Child
$age = 20;
if ($age > 18) {
echo 'Adult';
} else {
echo 'Child';
}
$d = date("D");
if ($d == "Fri"){
echo "Have a nice weekend!";
}
If the current hour is less than 20, the code will output "Have a good day!"
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
If $age
is more than 18
, prints Adult
$age = 20;
if ($age > 18) {
echo 'Adult';
}
$colors = ["Red", "Green", "Blue"];
$colors[] = "Yellow";
Associative arrays
$student = [
"name"=>"George",
"weight"=>77,
"height"=>1.8,
"male"=>true
];
$student["male"] = true;
echo 'George is ' . $student['weight'] . 'kg'."\n";
Multidimensional arrays
$students = [
"George"=> [
"name"=>"George",
"weight"=>77,
"height"=>1.8,
"male"=>true
],
"Alex"=> [
"name"=>"Alex",
"weight"=>87,
"height"=>2.8,
"male"=>true,
"parents"=> [
"father"=>"David",
"mother"=>"Marta",
]
],
];
echo 'Alex weighs ' . $students['Alex']['weight'] . 'kg'."\n";
echo "Alex's father is: " . $students['Alex']['parents']['father'];
min()
finds the lowest value from its arguments
echo min(10, 15, 7, 9, 17)."\n"; // 7
Function max()
finds the highest value from its arguments
echo max(10, 15, 7, 9, 17)."\n"; // 17
Function count()
counts all elements in the array
echo count([10, 15, 7, 9, 17])."\n";
Function round()
rounds a floating-point number to its nearest integer
echo(round(0.75))."\n"; // returns 1
echo(round(0.5))."\n"; // returns 1
echo(round(0.49))."\n"; // returns 0
Function floor()
rounds the floating-point number down
echo floor(3.3)."\n";// 3
echo floor(7.84)."\n";// 7
echo floor(-4.8)."\n";// -5
Function ceil()
rounds the floating-point number up
echo ceil(3.3)."\n";// 4
echo ceil(7.84)."\n";// 8
echo ceil(-4.8)."\n";// -4
Function rand()
generates a random number
echo rand()."\n";
// Generates a random number from the range
echo rand(10, 100)."\n";
Function sqrt()
returns the square root of a number
echo sqrt(64)."\n"; // 8
Function pow()
raises 2 to the power of 3
echo pow(2, 3)."\n"; // 8
Function pi()
Returns PI value
echo pi()."\n";// 3.1415926535898
Function abs()
returns the positive (absolute) value of a number
echo abs(-12)."\n";
Function decbin()
converts decimal number into binary
echo decbin(2)."\n";// 10
strlen()
returns the length of the string
echo strlen("Hello world!")."\n"; // 12
Function strrev()
returns the Reverse of the string
echo strrev("Hello world!")."\n"; // !dlrow olleH
Function strpos()
tries to find the second parameter and returns its index
echo strpos("Hello world!", "world")."\n"; // 6
Function str_replace()
replaces the word "Hello" with "Hi"
echo str_replace("Hello", "Hi", "Hello there")."\n"; // Hi there
Function substr()
returns the substring between given indexes
echo substr("Hello world", 6)."\n"; // world
echo substr("Hello world", 6, 3)."\n"; // wor
echo substr("Hello world", -5)."\n"; // world
echo substr("Hello world", -4, 2)."\n"; // or
Function trim()
removes whitespaces from sides of the string
echo trim(" Hello World ").".\n"; // Hello World.
// Removes whitespaces from the right end
echo rtrim(" Hello World ")."\n"; // Hello World
// Removes Exclamation mark from the right end
echo rtrim("Hello World!", "!")."\n"; // Hello World
Function strtoupper()
converts the string to upper case
echo strtoupper("Hello World")."\n";
Function strtolower()
converts the string to lower case
echo strtolower("Hello World")."\n";
Function explode()
breaks the string by its delimiter
print_r (explode(" ", "It's the first day of the course!"));
Function implode()
joins the array element based on the delimiter and returns the string
echo implode(" ", ['Hello', 'World']);
$variable
, $Variable
and $VARIABLE
are completely different// lowercase
$variable = "Value 1";
// PascalCase
$Variable = "Value 2";
// UPPERCASE
$VARIABLE = "Value 3";
echo $variable . "\n";
echo $Variable . "\n";
echo $VARIABLE . "\n";
Function names are case-insensitive
function printGreeting() {
print "Prints: Hello world!\n";
}
printGreeting();
Calling the function with uppercase will still work$name = "George";
Integer
$weight = 85; //kg
Float
$height = 1.9; //m
Boolean
$male = true;
Array
$colors = ["Red", "Green", "Blue"];
// Instead of storing them in separate variables like this:
$color1 = "Red";
$color2 = "Green";
$color3 = "Blue";
gettype()
returns a type of a given variableecho gettype($my_name); // string