Results: 1580
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
➤ In PHP, References enable accessing the same variable content by different names. A reference variable is created by prefixing & (ampersand) sign to original variable. For instance:
$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 
by Levani Makhareishvili
2 years ago
0
PHP
References
1
➤ By default, PHP variables are passed by value as the function arguments in PHP. When variables in PHP is passed by value, the scope of the variable defined at function level bound within the scope of function. Changing either of the variables doesn’t have any effect on either of the variables. ➤ When variables are passed by reference, use & (ampersand) symbol need to be added before variable parameter. For example: function( &$x ). 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. For instance:
function print_string( &$str ) {
    $str = " PHP \n";
    print( $str );
}
$string = " JS \n";
print_string( $string ); // outputs:  PHP
print( $string ); // outputs:  PHP
by Levani Makhareishvili
2 years ago
0
PHP
References
1
➤ The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false. Syntax:
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.
}
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ The unset() function unsets a variable. Syntax:
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.
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ The array_count_values() function counts all the values of an array. Syntax:
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
)
*/
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ sort() - sort arrays in ascending order.
$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.
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ BubbleSort Algorythm.
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
by Levani Makhareishvili
2 years ago
0
PHP
Algorithms
Arrays
1
➤ The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. • The exit() function returns nothing. • The exit() function only terminates the execution of the script. For instance:
$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
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ Using
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
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ Using
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
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
Results: 1580