PHP Notes
PHP Notes
Reverse – strrev()
$x = "Hello World!";
print_r($y);
/*
Result:
Slicing
$x = "Hello World!";
$x = "Hello World!";
Negative Length
From the string "Hi, how are you?", get the characters starting from index 5,
and continue until you reach the 3. character from the end (index -3).
Function Description
hebrevc() Converts Hebrew text to visual text and new lines (\n
………………………………………………………………………..
PHP Numbers
Integer - is_int(), is_integer(), is_long()
……………………………………………………………………………………
(int)
(float)
(bool): If a value is 0, NULL, false, or empty, the (bool) converts it into false, -
1 = true
(array) : When converting into arrays, most data types converts into an
indexed array with one element.
Objects converts into associative arrays where the property names becomes
the keys and the property values becomes the values:
(object)
…………………………………………………………………………………………………………………………………………….
MATH
pi() function returns the value of PI:
min(), max()
abs(-6.7)
sqrt(64) -
rand(10,100),
pow(25,1/2)
………………………………………………………………………………………………………………………………………………….
Magic Constants
Constant Description
__METHOD__ If used inside a function that belongs to a class, both class and funct
ClassName::class Returns the name of the specified class and the name of the namesp
PHP Operators
Arithmetic operators –
Modulus : % - Remainder of $x divided by $y
Exponentiation - ** Result of raising $x to the $y'th power
Assignment operators
Comparison operators
!== Not $x !== Returns true if $x is not equal to $y, or they are not of the
identical $y
<= Spaceship $x <=> Returns an integer less than, equal to, or greater than zer
> $y equal to, or greater than $y. Introduced in PHP 7.
Increment/Decrement operators
Logical operators
Array operators
Conditional assignment operators
FOR EACH LOOP
$colors = array("red", "green", "blue", "yellow");
Key Value
$members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
class Car {
public $color;
public $model;
$this->color = $color;
$this->model = $model;
#Result
color: red
model: Volvo
Foreach Byref
By default, changing an array item will not affect the original array:
var_dump($colors);
By assigning the array items by reference (&$x), changes will affect the
original array:
var_dump($colors);
endforeach;
Functions :
Variable Number of Arguments
function sumMyNumbers(...$x) {
$n = 0;
$len = count($x);
$n += $x[$i];
return $n;
$a = sumMyNumbers(5, 2, 6, 2, 7, 7);
echo $a;
Strict Type
<?php declare(strict_types=1); // strict requirement
return $a + $b;
?>
PHP Array Types
Indexed arrays - Arrays with a numeric index
Associative arrays - Arrays with named keys
Multidimensional arrays - Arrays containing one or more arrays
Associative Arrays
Another Method
$myCar = [
];
Types of Array :
Indexed Array :
Associative Arrays :
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
$cars = [
0 => "Volvo",
1 => "BMW",
2 =>"Toyota"
];
$cars = [];
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars["bmw"] = "cherries";
$cars["color"] = "Red";
unset($cars[0], $cars[1]);
Sorting Arrays:
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the
value
ksort() - sort associative arrays in ascending order, according to the
key
arsort() - sort associative arrays in descending order, according to the
value
krsort() - sort associative arrays in descending order, according to the
key
Multidimensional Arrays:
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo "<ul>";
echo "<li>".$cars[$row][$col]."</li>";
echo "</ul>";
Function Description
array() Creates an array
$_SERVER
echo $_SERVER['PHP_SELF'];
Element/Code Description
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_REQUEST['fname']);
if (empty($name)) {
} else {
echo $name;
?>
$_REQUEST
$_POST$_POST['fname'];
$_GET
$_GET['web']
$_FILES
$_ENV
$_COOKIE
$_SESSION
preg_last_error() Returns an error code indicating the reason that the mos
expression call failed
Expression Description
[abc] Find one character from the options between the brackets
Metacharacters
Metacharacters are characters with a special meaning:
Metacharacter Description
| Find a match for any one of the patterns separated by | as in: cat
\d Find a digit
Quantifiers
Quantifiers define quantities:
Quantifier Description
n+ Matches any string that contains at least one n
Note: If your expression needs to search for one of the special characters
you can use a backslash ( \ ) to escape them. For example, to search for one
or more question marks you can use the following expression: $pattern = '/\?
+/';
Grouping
You can use parentheses ( ) to apply quantifiers to entire patterns. They
also can be used to select parts of the pattern to be used as a match.
Example
Use grouping to search for the word "banana" by looking for ba followed by
two instances of na:
$pattern = "/ba(na){2}/i";