gettype(variable);
For instance:
$int = 10;
echo gettype($int)."\n"; // outputs: integer
$str = "PHP";
echo gettype($str)."\n"; // outputs: string
$flt = 1.2;
echo gettype($flt)."\n"; // outputs: double (also called float)
$bool = true;
echo gettype($bool)."\n"; // outputs: boolean
$arr = [1,2,3,4,5,6,7,8,9,10];
echo gettype($arr)."\n"; // outputs: array
// with built-in function
function reverseString($s) {
$s = array_reverse($s);
return $s;
}
print_r(reverseString(["h", "e" , "l", "l", "o"]));
// with loop
function reverseString($s) {
$count = count($s);
$array = [];
for ($i = 0; $i < $count; $i++) {
array_unshift($array, $s[$i]); // inserts element at the beginning of the array
}
$s = $array;
return $s;
}
print_r(reverseString(["h", "e" , "l", "l", "o"]));
function reverseBits($n) {
$n = bindec(strrev($n));
return $n;
}
echo reverseBits("00000010100101000001111010011100");
// P.S onlinephp returns correct answer: 964176192, but
// leet code returns 1 at the same case,
// can someone explain to me in the comments ?
in_array(search, array, type) // third parameter is optional
For instance:
$colours = ["Red", "Black", "White"];
if (in_array("Black", $colours))
echo "Match found!";
else
echo "Match not found!";
// outputs: Match found!
array_unique(array, sorttype) // second parameter is optional
For instance:
$colours = [
"Red",
"Black",
"White",
"Red",
"Black",
"Blue",
"white"
];
print_r (array_unique($colours));
// outputs:
/*Array
(
[0] => Red
[1] => Black
[2] => White
[5] => Blue
[6] => white
)*/
print_r(variable, return); // second parameter is optional
For instance:
$cars = array("BMW", "Ferrari", "Mustang");
print_r($cars);
$names = array("Keira Knightley"=>"37", "Mads Mikkelsen"=>"56", "Keanu Reeves"=>"58");
print_r($names);
/* outputs:
Array
(
[0] => BMW
[1] => Ferrari
[2] => Mustang
)
Array
(
[Keira Knightley] => 37
[Mads Mikkelsen] => 56
[Keanu Reeves] => 58
)
*/
ord(string)
For instance:
echo ord("A")." - A\n"; // outputs: 65
echo ord("a")." - a\n"; // outputs: 97
echo ord("z")." - z\n"; // outputs: 122
echo ord(7)." - 7\n"; // outputs: 55
echo ord("7")." - '7'\n"; // outputs: 55