$x = 10;
$y = 20;
$y = &$x;
echo " x = $x \n"; // outputs: x = 10
echo " y = $y \n\n"; // outputs: y = 10
$y = 20;
echo " x = $x \n"; // outputs: x = 20
echo " y = $y \n"; // outputs: y = 20
function print_string( &$str ) {
$str = " PHP \n";
print( $str );
}
$string = " JS \n";
print_string( $string ); // outputs: PHP
print( $string ); // outputs: PHP
isset(variable, ....); // second parameter is optional, another variable to check
For instance:
$x = 0;
// True because $x is set
if (isset($x)) {
echo "Variable 'x' is set\n";
}
$y = null; // or $y;
// False because $y is NULL
if (isset($y)) {
echo "Variable 'y' is set.";
// Declare an array
$array = array('PHP'=> true);
// Use isset function
echo isset($array['PHP']) ? 'Array is set.' :
'Array is not set.';
// outputs: Array is set.
}
unset(variable, ....); // second parameter is optional, another variable to check
For instance:
$x = 10;
unset($x);
// False because $x is NULL
if (isset($x)) {
echo "Variable 'x' is set\n";
}
// Declare an array
$array = array('PHP'=> true);
unset($array['PHP']);
echo isset($array['PHP']) ? 'Array is set.' :
'Array is not set.';
// outputs: Array is not set.
array_count_values(array)
For instance:
$animals = array(
"Cat",
"Cat",
"Dog",
"Rabbit",
"Rabbit",
"Dog",
"Dog",
"Dog",
"Cat",
);
print_r (array_count_values($animals));
/* outputs:
Array
(
[Cat] => 3
[Dog] => 4
[Rabbit] => 2
)
*/
$numbers = array(5, 1, 3, 4, 2);
sort($numbers);
print_r ($numbers); // outputs: 1 2 3 4 5
$cars = array("Ferrari", "BMW", "Mustang");
sort($cars);
print_r ($cars); // outputs: BMW Ferrari Mustang
// ASCII Code 66 - B
// ASCII Code 70 - F
// ASCII Code 77 - M
➤ rsort() - sort arrays in descending order.
$numbers = array(5, 1, 3, 4, 2);
rsort($numbers);
print_r ($numbers); // outputs: 5 4 3 2 1
➤ asort() - sort associative arrays in ascending order, according to the value.
$age = array("Keira Knightley"=>"37", "Mads Mikkelsen"=>"56", "Keanu Reeves"=>"58");
asort($age);
foreach($age as $key => $value) {
echo "Key = $key -> Value = $value \n";
}
// outputs:
// Key = Keira Knightley -> Value = 37
// Key = Mads Mikkelsen -> Value = 56
// Key = Keanu Reeves -> Value = 58
➤ ksort() - sort associative arrays in ascending order, according to the key.
$age = array("Keira Knightley"=>"37", "Mads Mikkelsen"=>"56", "Keanu Reeves"=>"58");
ksort($age);
foreach($age as $key => $value) {
echo "Key = $key -> Value = $value \n";
}
// outputs:
// Key = Keanu Reeves -> Value = 58
// Key = Keira Knightley -> Value = 37
// Key = Mads Mikkelsen -> Value = 56
• arsort() - sort associative arrays in descending order, according to the value.
• krsort() - sort associative arrays in descending order, according to the key.function bubbleSort(&$array)
{
if ($length = count($array)) {
for ($i = 0; $i < $length; $i++) {
for ($k = 0; $k < $length; $k++) {
if ($array[$i] < $array[$k]) { // > descending
$tmp = $array[$i];
$array[$i] = $array[$k];
$array[$k] = $tmp;
}
}
}
}
}
$arr = [2,99,15,72,14,1,26,923,34,66,7];
bubbleSort($arr);
print_r ($arr); // outputs: 1 2 7 14 15 26 34 66 72 99 923
➤ InsertSort Algorythm.
function insertSort(&$array) {
if ($length = count($array)) {
for ($i = 0; $i < ($length - 1); $i++) {
$key = $i + 1;
$tmp = $array[$key];
for ($k = ($i + 1); $k > 0; $k--) {
if ($tmp < $array[$k - 1]) { // > descending
$array[$k] = $array[$k - 1];
$key = $k - 1;
}
}
$array[$key] = $tmp;
}
}
}
$arr = [2,99,15,72,14,1,26,923,34,66,7];
insertSort($arr);
print_r ($arr); // 1 2 7 14 15 26 34 66 72 99 923
$x = 10;
$y = 5;
exit; // Terminates the execution of the script
echo $x + $y; // This line will not be executed
$x = 10;
$y = 5;
exit("terminate the current script"); // Output a message
echo $x + $y; // This line will not be executed
strval()
function.
• The strval() function is an inbuilt function in PHP and is used to convert
any scalar value (string, integer, or double) to a string. We cannot use
strval() on arrays or on object, if applied then this function
only returns the type name of the value being converted.
$number = 10;
// converts integer into string
$str = strval($number);
echo gettype($str); // outputs: string
➤ Using inline variable parsing.
• When you use Integer inside a string, then the Integer
is first converted into a string and then prints as a string.
$integer = 10;
$str = "$integer";
echo gettype($str); // outputs: string
➤ Using explicit Casting.
• Explicit Casting is the explicit conversion of data type because the
user explicitly defines the data type in which he wants to cast.
We will convert Integer into String.
$num = 5;
$str = (string)$num; // or String
echo gettype($str); // outputs: string
intval()
function.
• To convert string to integer using PHP built-in function, intval(),
provide the string as argument to the function. The function will
return the integer value corresponding to the string content.
$number_str = "10";
$int_number = intval($number_str);
echo gettype($int_number); // outputs: integer
➤ Using explicit Casting.
• To convert string to integer using Type Casting, provide the
literal (int) along with parenthesis before the string literal.
The expression returns integer value of the string.
$str = 10;
$number = (int)$str;
echo gettype($number); // outputs: integer