// Starting clock time in seconds
$start_time = microtime(true);
$result = 0;
for( $i = 0; $i <=1000000; $i++ )
{
$result += $i;
}
// End clock time in seconds
$end_time = microtime(true);
// Calculate script execution time
$execution_time = ($end_time - $start_time);
echo " Execution time of script = ".$execution_time." sec";
$x = 10;
$y = 5;
function additional_first(){
return $x + $y;
}
echo additional_first();
// outputs : Undefined variables $x and $y
function additional_second(){
global $x,$y;
return $x + $y;
}
echo additional_second();
// outputs : 15
$int = 10;
echo "I. Value of \$int is: $int\n";
echo gettype("$int"); // outputs : string
• An integer is NOT converted to a string.
$int = 10;
echo "II. Value of \$int is: ".$int;
$alpha = range('A', 'Z');
for( $i = 0; $i < 10; $i++ ) {
for( $k = 10; $k > $i ; $k-- ){
echo $alpha[$i];
}
echo "\n";
}
Outputs : AAAAAAAAAA
BBBBBBBBB
CCCCCCCC
DDDDDDD
EEEEEE
FFFFF
GGGG
HHH
II
J
range(low, high, step)
// third parameter is optional, Specifies the increment used in the range. Default is 1
For instance:// Return an array of elements from "0" to "20".increment by 5.
$number = range(0,20,5);
print_r ($number);
// outputs : 0 5 10 15 20
// Using letters - return an array of elements from "a" to "k"
$letter = range("a","k");
print_r ($letter);
// outputs : a b c d e f g h i j k
$num = 0;
$n1 = 0;
$n2 = 1;
echo "Fibonacci series for first 20 numbers: \n";
echo $n1.' '.$n2.' ';
while ( $num < 18 )
{
$n3 = $n2 + $n1;
echo $n3.' ';
$n1 = $n2;
$n2 = $n3;
$num = $num + 1;
}
// 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
function print_string(&$string) {
$string = "Function string \n";
echo($string);
}
$string = "Global string \n";
print_string($string);
echo($string);
// outputs: Function string, Global string
► Scope of both global and function variable becomes global as both variables are defined by same reference.
Therefore, whenever global variable is change, variable inside function also gets changed. ◄
If we delete & (ampersand) symbol before variable, it outputs: Function string, Global string
function is_multi($array) {
$filter = array_filter($array, 'is_array');
print_r($filter);
if(count($filter) > 0) {
return true;
}
return false;
}
var_dump(is_multi([
"Valeri"=> [
"weight"=>77,
],
"Giorgi"=> [
"weight"=>87,
"parents"=> [
"father"=>"Daviti",
"mother"=>"Mariami",
]
],
]));
rand();
or rand(min,max);
For instance:
$x = rand(0,10);
echo $x;
// outputs : random integer between 0 and 10 (inclusive)
$x = 5;
$y = 10;
// Swapping Logic
$temp = $x;
$x = $y;
$y = $temp;
echo "After swapping: ";
echo "\n x = $x \n y = $y";
// outputs : x = 10 y = 5
• Swap two numbers without using a third variable
- Using arithmetic operation + and ?$x = 5;
$y = 10;
$x = $x + $y; // 15
$y = $x - $y; // 5
$x = $x - $y; // 10
echo "After swapping: ";
echo "\n x = $x \n y = $y";
// outputs : x = 10 y = 5
- Using arithmetic operation * and /
$x = 5;
$y = 10;
$x = $x*$y; // 50
$y = $x/$y; // 5
$x = $x/$y; // 10
echo "After swapping: ";
echo "\n x = $x \n y = $y";
// outputs : x = 10 y = 5