30 PHP built-in function you should know

Spread the love

Hello developers,

How are you doing? In this tutorial, I will discuss with you 30 PHP built-in functions that you should know.

explode()

The explode() function breaks a string into an array.

$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )

implode()

The implode() function returns a string from the elements of an array.

$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr)."<br>";
Hello World! Beautiful Day!

in_array()

The in_array() function searches an array for a specific value.

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

if (in_array("Glenn", $people))
  {
  echo "Match found";
  }
else
  {
  echo "Match not found";
  }
Match found

array_push()

Delete the last element of an array

$a=array("red","green","blue");
array_pop($a);
print_r($a);
Array ( [0] => red [1] => green )

 array_pop()

Delete the last element of an array

$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
Array ( [0] => red [1] => green [2] => blue [3] => yellow )

md5()

The md5() function calculates the MD5 hash of a string.

$str = "Hello";
echo md5($str);
8b1a9953c4611296a827abf8c47804d7

str_word_count()

The str_word_count() function counts the number of words in a string.

echo str_word_count("Hello world!");
2

strlen()

The strlen() function returns the length of a string.

echo strlen("Hello");
5

strpos() 

The strpos()  function returns the length of a string.

echo strpos("I love php, I love php too!","php");
7

gettype() 

ThThe gettype() function returns the type of a variable.

$a = 3;
echo gettype($a) . "<br>";

$b = 3.2;
echo gettype($b) . "<br>";

$c = "Hello";
echo gettype($c) . "<br>";

$d = array();
echo gettype($d) . "<br>";

$e = array("red", "green", "blue");
echo gettype($e) . "<br>";

$f = NULL;
echo gettype($f) . "<br>";

$g = false;
echo gettype($g) . "<br>";
integer
double
string
array
array
NULL
boolean

array_keys()

The array_keys() function returns an array containing the keys.

$a=array("Volvo"=>"XC90","BMW"=>"X5","Toyota"=>"Highlander");
print_r(array_keys($a));
Array ( [0] => Volvo [1] => BMW [2] => Toyota )

 array_merge()

The array_keys() function returns an array containing the keys.

$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2))
Array ( [0] => red [1] => green [2] => blue [3] => yellow )

 anonymous function

$multiply = function ($x, $y) {
	return $x * $y;
};
echo $multiply(10, 20);

arrow function

$add = fn ($x, $y) => $x + $y;
add(10,20)

Spread the love

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://devshahin.com/

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published. Required fields are marked *