Results: 1580
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
➤ PHP allows you to define default argument values. In such case, if you don't pass any value to the function, it will use default argument value. For instance:
function addition( $x = 10 ){  
	echo $x + 10;  
}  
addition(40); // outputs :  50
addition(); // passing no value. outputs :  20
function addition( $x = 10, $y = 20 ){  
	echo $x + $y;  
}  
addition(5); // passing one argument. outputs :  25
addition(); // passing no value. outputs :  30
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments in function. To do so, you need to use 3 ellipses (dots) before the argument name. For instance:
function print_arr(...$numbers) {  
    foreach ($numbers as $element) {  
        echo $element."\n";  
    }  
}  
echo print_arr(1, 2, 3, 4);  
// outputs :   1 2 3 4
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ PHP also supports recursive function call. In such case, we call current function within function. It is also known as recursion. For instance:
function display( $number ) {    
    if( $number <= 10 ){    
     echo "$number";    
     display ($number+1 );    
    }
}    
display(0);  
// outputs :  012345678910
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ By using the is_null function, we can check whether the variable is NULL or not. returns true if var is null, otherwise false. • returns true if parameter is null, otherwise false.
$bool = TRUE;  
if (is_null($bool))  
{
	echo 'Variable is  NULL';  
}
else 
{
	echo 'Variable is not NULL';  
}  
// outputs :  Variable is not NULL
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ Two numbers can be swapped or interchanged. It means first number will become second and second number will become first. For instance: • Swapping Using Third Variable
$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
by Levani Makhareishvili
2 years ago
0
PHP
1
➤ The rand() function generates a random integer. If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Syntax:
rand();
or
rand(min,max);
For instance:
$x = rand(0,10);
echo $x;
// outputs :  random integer between 0 and 10 (inclusive)
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ Fibonacci series is the one in which you will get your next term by adding previous two numbers. For instance:
$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 
by Levani Makhareishvili
2 years ago
0
PHP
1
➤ The range() function creates an array containing a range of elements.This function returns an array of elements from low to high. If the low parameter is higher than the high parameter, the range array will be from high to low. Syntax:
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
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ Alphabet triangle pattern using range() function
$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
by Levani Makhareishvili
2 years ago
0
PHP
1
Concatenation using Double Quotes " " CODE
• An integer is converted to a string.
$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;
by Levani Makhareishvili
2 years ago
0
PHP
1
Results: 1580