TOP 50 PHP FUNCTIONS

top 50 php functions


1. Ceil- This function is used to round up fractions of a given
variable .


Example
";
echo ceil($y);

output//
45
10

2. Floor- This function is used to round down fractions of a given variable.
Example

   
$x=5.8;
$y=9.2;
echo floor($x) ."
";
echo floor($y);
//output
5	
9
 

3. Count- This function is a means to count every element in an array.
Example

 

$names=array(“bola”, “kemi”, “tunde”, “fola”);
Echo count($names);

//output:4

• COUNT_RECURSIVE:is used to count all the elements in multidimensional array.

 
Example
$names=array(array("bola", "kemi", "tunde", "fola"),
array("bola", "femi", "tunde", "folu"));

Echo count($names, COUNT_RECURSIVE);

//output:10

4. Current- This function is used to call or return elements in an array.

 
$names=array("bola", "kemi", "tunde", "fola");
echo current($names);
//output:bola

5. Extract- This function serves to import variables into the current symbol table from an array.

 
Example
$style=array("color" => "blue", "size" => 45, "shape" => "square");
		echo extract($style) ."
";
		echo "\$color is $color\n\$size is $size\n\$shape is $shape";
//output: 3
$color is blue $size is 45 $shape is square

6.End- This function is used to call out the last element of an array.

 
Example
$style=array("color", "size" ,  "square");
echo end($style) ;
//output: square

7. Echo- This function is a means to print out information of a given variable likewise “print_r & var_dump” .

8. Usleep- This function is used to delay execution of an action in microseconds

 
Example
echo  date('h/i/s')."
";
//delay 	
	usleep(3000000);
	echo  date('h/i/s');	
//output: 02/48/04
02/48/07

9. Reset- This function is used to call out the first element in an array

             
    Example:
$people = array ("Peter","Joe","Glenn","Cleveland");
 echo reset ($people);
//Outputs: Peter

10.Die- This function is used to terminate or exit a given command or action.

 
Example
         $hello = "https://www.solarexinsitute.com/";
fopen  ($hello, "r") 
or die("Unable to connect to $hello);

11. Exit- This function is used as a means to output a message and terminate the command.

 
Example
         $hello = "https://www.solarexinsitute.com/";
fopen  ($hello, "r") 
or  exit("Unable to connect to $hello);

12. Uniquid- This function is used to generate a unique id in a given script when needed.
Example:

 

echo uniqid ();
//Outputs 5f43155e2c50d

13. Compact- This is used to create or output an array and its values.

 
$firstname = "Peter";
$lastname= "Ayomide";
$age = "30";
$result = compact("firstname","lastname","age");
//Output
Array ( [firstname] => Peter [lastname] => Ayomide [age] => 30 ).

14.Shuffle- This function is used to shuffle an array<

 
$hello = array("red","green" ,"blue","yellow");
shuffle($hello);
print_r($hello);
//Outputs 

Array ( [0] => yellow [1] => red [2] => blue [3] => green )
Note: Refresh the page to see how shuffle() works.

14. Array_Reverse- This function is used to return an array elements in reverse.

 
$a = array("a"=> "Volvo", "b"=> "BMW" ,"c"=>"Toyota");
print_r(array_reverse($a));
//Output
Array ( [c] => Toyota [b] => BMW [a] => Volvo )

15. Array_fill- This function is used to fill an array with values
Example

 

$styles=array_fill(20,5, "books");
print_r( $styles);
//output: Array ( [20] => books [21] => books [22] => books [23] => books [24] => books )

16. Isset- This function is used to determine/know if a variable is declared.
Example

 

$styles=65;

if (isset($styles)){
echo "variable styles is set";
}
else{
echo "not set";
}
//output: variable styles is set.

17. Ksort- This function is used to sort through an array using key.
Example

 

$a = array("a"=>"Volvo", "b"=>"BMW","c"=>"Toyota");
ksort($a);
foreach ($a as $cars => $val){
echo "$cars = $val\n";
}
//output: a = Volvo b = BMW c = Toyota

18. Usort- This function will sort through an array using a comparison
19. Empty- This function is used to determine if a variable is empty.
Example

 

$b=" ";
if(empty($b)) {
echo "variable b is empty";
}

else { echo "not empty";
}
//output: variable b is empty.

20. Sleep- This function is used to delay an execution by seconds.
Example

 

echo  date('h/i/s')."
";
	
	sleep(30);
	echo  date('h/i/s');	
//output: 04/37/16
04/37/46

21. Get_browser- This function is used to know what a user browser ia able to do.
22. Defined- This function is used to check if a given constant exists.
Example

 

define("msg" , "i love designing");
if(defined ("msg")){
echo "msg is defined";
}
else{ echo
"not defined";
}
//output: msg is defined.

23. Unset- This function is used to change a given value into a modified one.
Example

 
$msg ="i love designing";
echo "the value of variable 'msg' before unset: " . $msg ."
";
unset ($msg);
echo "the value of variable 'msg' after unset: ". $msg;
//output: the value of variable 'msg' before unset: i love designing
the value of variable 'msg' after unset:

24.Intval- This function is used to get the integer of a variable.
Example

 

$a=5;
echo intval($a)."
";

$b=5*2;
echo intval ($b). "
";

$hello = array("red","green" ,"blue","yellow");
echo intval ($hello);
//output: 5
10
1

25. Array_shift- This function is used to echo the first element in an array, likewise “reset”.
Example

 

$a = array("a"=>"Volvo", "b"=>"BMW","c"=>"Toyota");
echo array_shift($a) ."
";
print_r ($a);
//output: Volvo
Array ( [b] => BMW [c] => Toyota )

26. Array_pop- This function is used to echo the last element in an array, likewise “end”
Example

 

$a = array("a"=>"Volvo", "b"=>"BMW","c"=>"Toyota");
echo array_pop($a) ."
";
print_r ($a);
//output: Toyota

27.Array_push- This function is used to add a value at the end an array.
Example

 

$a = array("a"=>"Volvo", "b"=>"BMW","c"=>"Toyota");
echo array_push($a, "lexus","camry" ) ."
";
print_r ($a);
//output: Array ( [a] => Volvo [b] => BMW [c] => Toyota [0] => lexus [1] => camry )

28.Strval- This function is used to echo the string value of a variable.
Example

 

$a="hi";
echo strval($a)."
";

$b="Solarex";
echo strval ($b). "
";

$hello = 2020;
echo strval ($hello);
//output: hi
Solarex
2020

29. Print_r- This function is used to execute a given action or command ,likewise “echo & var_dump” .
Example

 

$a = array("a"=>"Volvo", "b"=>"BMW","c"=>"Toyota");
print_r ($a);
//output: Array ( [a] => Volvo [b] => BMW [c] => Toyota )
 
$consonants=array("b","g","n","n","v");
$text="i love designing";
echo str_replace($consonants," ",$text);
//output: i lo e desi i 

31. Eval- This function is used to evaluate a string as a php code.
32. Strlen- This function is used to calculate the length of a string
Example

 

$names="I love playing basketball";


Echo strlen($names);
//output:25

33. List- This function serves as a means to assign variables as a list.
Example

 

$names=array("bola", "kemi", "tunde", "fola");
list($A, $b, $C, $d)=$names;
echo" my name is $b and my friends names are $A , $d and $C.";

//output: my name is kemi and my friends names are bola , fola and tunde.

34.Define- This function is used to define a named or declared constant.

Example

 

define(“MSG”, “I love dsigning”);
echo MSG;
//output: I love designing.

35.Print_r- This function is used to echo out information ,likewise “echo & var_dump”.
Example

 


$names=array("bola", "kemi", "tunde", "fola");
echo print_r($names);

//output: Array ( [0] => bola [1] => kemi [2] => tunde [3] => fola )

36.Next- This function is used to echo out the next value in an array after the current one.
Example

 
$names=array("bola", "kemi", "tunde", "fola");



echo current($names)."
";
echo next($names)."
";
echo next($names);
//output
Bola
Kemi
Tunde

37.Var_export- This function is used as a means to output or echo out a split string representing a variable.
Example

 

$d=32;
$e=5;
$names=array("bola", "kemi", "tunde", "fola");


echo var_export($d)."
";
echo var_export($names);
//output: 32
array ( 0 => 'bola', 1 => 'kemi', 2 => 'tunde', 3 => 'fola', )
?>

38.Settype- This function is used to check what type a variable is.
Example

 

$d=32;
$e=5;
echo settype($d, "string"); //$d is now strings.
echo settype($e, "integer"); //$e is now integer.

39. Constant- This function is used to get the value of a constant or declares it as null.
Example



define(“MSG”, “I love designing”);
echo constant(“MSG”);

//output: I love designing.

40.Unserialize- This creates a php value from stored data.

41.Pos- This function is an alias of “current” and work exactly like it.
Example

 

$names=array("bola", "kemi", "tunde", "fola");
Echo pos($names);
//output:bola

42. Base_convert- This function is used to convert a variable between arbitrary bases(this is a mathematical function.
Example

 
       $first =7826786;
       echo base_convert($first,10,2);
       //Outputs:
       11101110110110101100010

43.Fmod- This function aids to return the remainder of a division in arguments (this is a mathematical function).
Example



$d=32;
$e=5;
echo fmod($d, $e);

//output:2

44. Max- This function helps to find the highest value of a given variable (this is a mathematical function)
Example

 

$d=array(4,5,7,2,1);
echo min($d);
//output:7

45. Min- This function helps to find the lowest value of a given variable(this is a mathematical function).
Example

 

$d=array(4,5,7,2,1);

echo min($d);
//output:1

46.substr- This function is used to return a part of a strings.
Example

 

$text="i love designing";
$extract=substr($text,0);
echo $extract ."
";
echo substr($text,3,6)."
";
echo substr($text,3);
//output: i love designing
ove de
ove designing

47.In_array- This function is used to check if the value is inside the array.
Example

 

$cars=array("camry", "volvo" ,"toyota" );
if(in_array("camry",$cars)){
echo "the value is found";
}
else{
echo "not found";
}
//output: the value is found

48.Explode- This function returns array of string.
Example

 

$cars="camry, volvo ,toyota";
print_r(explode(",",$cars));
echo "
";
print_r (explode(",",$cars,0));
echo"
";
 print_r (explode(",",1));
//output: Array ( [0] => camry [1] => volvo [2] => toyota ) 
Array ( [0] => camry, volvo ,toyota ) 
Array ( [0] => 1 )

49.Sqrt- This function is used to get the square root of a given value/variable(this is a mathematical function)
Example

 

$x=36;
$y=64;

echo sqrt($x) ."
";
echo sqrt($y);

//output:
6
4

50.Implode: This function is used to join array element with strings.
Example


$people = array (12.5,"Peter","Joe","Glenn","Cleveland");

$glued=implode(",",$people);
echo $glued;
//output: 12.5,Peter,Joe,Glenn,Cleveland