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",
]
],
]));
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
$test = "Hello world";
echo str_replace("world", "Valer", $test);
// outputs: Hello Valer
► str_replace() is a more global approach to replacements, while strtr() simply translates the characters ONE BY ONE.
// therefore if we use that function instead of str_replace at that case, it outputs differently. ◄
$test2 = "Hello world";
echo strtr($test2, "world", "Valer");
// outputs: Heeea Valer
// because it translated l > e, o > a. (w > V, r > l, d > r)
function arrayKeys($array) {
print_r($array);
return array_keys($array);
}
print_r(arrayKeys(["House" => "White", "Car" => "Black"]));
// testing print_r outputs: ( [House] => White, [Car] => Black )
// The function returns: ( [0] => House, [1] => Car )function arrayFillKeys($array) {
print_r($array);
$array = array_fill_keys($array, 'value');
return ($array);
}
print_r(arrayFillKeys([4,3,2]));
// testing print_r outputs: ( [0] => 4, [1] => 3, [2] => 2 )
// The function returns: ( [4] => value, [3] => value, [2] => value )$string = "hello world";
$pattern = "/world/";
echo preg_match($pattern, $string);
// outputs 1
► As usual, we use this function together with Regex (Regular Expression) syntax and we adapt it to the different solutions.
for example, in regular expression "i" means case insensitive check. ◄
therefore if that case would be like that:
$string = "Hello wORLD";
$pattern = "/world/i";
echo preg_match($pattern, $string);
// it would still outputs 1
► Valeri has used this function in a task to check for the only uppercase letters,
only lowercase letters and first uppercase letter in the string via regex. ◄
For example: (via Regex - Regular Expression and preg_match)
(^[A-Z]*$) it means if there are only uppercase letters in a string.
(^[a-z]+$) it means if there are only lowercase letters in a string.
(^[A-Z]{1}[a-z]*$) it means if the first letter is uppercase in a string.
| - it means "Logical or" via regex as well.
<?php
function isPalindrome($s) {
//s = "A man, a plan, a canal: Panama"
$s= strtolower($s);
$filtered = "";
$len = strlen($s);
for ($i= 0; $i<$len; $i++){
$ascii = ord($s[$i]);
if (($ascii>=97 && $ascii<123) ||
($ascii>47 && $ascii<58)){
$filtered.=$s[$i];
}
}
return $filtered == strrev($filtered);
}
$s = "A man, a plan, a canal: Panama";
echo isPalindrome($s);
<?php
function space($sentence) {
$sentence= trim($sentence); //trim function-Breaks a sentence after spaces.
//str_contains-Checks if there is a blank in this sentence in this situation.
while (str_contains($sentence, ' ')) {
// str_replace-Replaces the first element with the second element in this sentence.
$sentence= str_replace(' ', ' ',$sentence);
}
return $sentence;
}
$sentence " hi i am gvanca";
echo space($sentence);
<?php
function gettingmarried($women, $men) {
$count=count($women); //count-counts the number of women.
// '!=' -If not equal.
if ($women != count($men)) {
echo "I don't have all pairs";
}
//array_combine-Pairs the data of the first element with the second.
return array_combine($women, $men);
}
print_r(gettingmarried(["elene","mariami"], ["dato", "vano"]));