Top 50 PHP function example You Need to Know (2021 Update)

PHP function example : The PHP string functions are part of the PHP core. here learn to PHP implode, php isset, php foreach, php switch, php switch case, php break, php unset, php sleep, php random number

php print_r, php echo, php comment, php comments, php new line, php empty, php file_put_contents, php file_get_contents, php number format

PHP Built-in Functions – PHP function example

php preg_match, php strtotime, php round, php json_encode, php count, php $_server, php var_dump, php list, php exit

php for each, php else if, php memory limit, php delete file, php parsing url, session_encode php, include php, php htmlspecialchars, php excell or many more.

php implode

PHP function example

<?php
$arr = array('Welcome','Website!','Beautiful','Day!');
echo implode(" ",$arr);
?>

php foreach

<?php
$players = array("kohali", "rohit", "sachin", "yuvraj");

foreach ($players as $value) {
  echo "$value <br>";
}
?>

php switch

<?php
$favplayer = "kohali";

switch ($favplayer) {
  case "kohali":
    echo "Your favorite player is kohali!";
    break;
  case "sachin":
    echo "Your favorite player is sachin!";
    break;
  case "rohit":
    echo "Your favorite player is rohit!";
    break;
  default:
    echo "Your favorite player is neither kohali, sachin, nor rohit!";
}
?>

php switch case

<?php
$today = date("D");
switch($today){
    case "Mon":
        echo "Player is Monday. Clean your IPL.";
        break;
    case "Tue":
        echo "Player is Tuesday. Buy some ODI.";
        break;
    case "Wed":
        echo "Player is Wednesday. Visit a Test.";
        break;
    case "Thu":
        echo "Player is Thursday. Repair your t20.";
        break;
    case "Fri":
        echo "Player is Friday. Party Worlcup.";
        break;
    case "Sat":
        echo "Player is Saturday. Its movie Ranji.";
        break;
    case "Sun":
        echo "Player is Sunday. Do some Champion.";
        break;
    default:
        echo "No details available for that sorry.";
        break;
}
?>

php break

<?php
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br>";
}
?>

php sleep

<?php
echo date('h:i:s') . "<br>";


sleep(3);


echo date('h:i:s');
?>

PHP: isset(), unset() and empty()

PHP function example

Read Also:  PHP mysqli insert update delete for Dynemic query

php isset

<?php
	$Welcome = 'Welcome';
	if(isset($Welcome)) {
		echo $Welcome. ' is set.';
	}
?>

$empty = '';
if(isset($empty)) {
	echo $empty. ' is also set.'; 
}

$untrue = false;
if(isset($untrue)) {
	echo $untrue. ' is also set.'; 
}

php unset

<?php
	$Welcome = 'Welcome';
	unset($Welcome);
	if(isset($Welcome)) { 
		echo $Welcome. ' is set.';
	}
?>

<?php
	$Welcome = 'Welcome';
	$n = 1;
	$bool = false;
	unset($Welcome, $n, $bool);
?>

php empty

<?php
	$Welcome = '';
	$menu = 0;
	$player_name = false;
	$counter = null;

	if(empty($Welcome)) { 
		echo 'true';
	}

	if(empty($menu)) { 
		echo 'true';
	}

	if(empty($player_name)) { 
		echo 'true';
	}

	if(empty($counter)) { 
		echo 'true';
	}
?>

<?php
	$player_name = ' ';
	if(empty($player_name)) { 
		echo $player_name. ' is empty.';
	}
?>

php random number

<?php
echo(rand() . "<br>");
echo(rand() . "<br>");
echo(rand(10,100));
?>

php print_r

<?php
$a = array("kohali", "rohit", "sachin");
print_r($a);

echo "<br>";

$b = array("Mithun"=>"35", "Bharat"=>"37", "jayesh"=>"43");
print_r($b);
?>

php echo

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Welcome Website!<br>";
echo "We are about to learn Angularjs, vuejs, jquery, javascript or PHP!<br>";
echo "I ", "Love ", "you ", "pakainfo ", "with infinityknow website.";
?>

php comment

single-line comments:

<!DOCTYPE html>
<html>
<body>

<?php
// This is a single-line comment

# PHP is an acronym for "PHP: Hypertext Preprocessor" ·
?>

</body>
</html>

php comments

Syntax for multiple-line comments:

<!DOCTYPE html>
<html>
<body>

<?php
/*
What is PHP?
Why to Learn PHP?
PHP is an acronym for "PHP: Hypertext Preprocessor" ·
*/
?>

</body>
</html>

php new line

<?php  
    echo nl2br("Welcome To Pakainfo.com free download source code \n in this best\r\n on the great website");  
?>  

php file_put_contents

<?php
echo file_put_contents("welcome.txt","Welcome Website. Greatting!");
?>

php file_get_contents

<?php
$results = file_get_contents('pakainfo.txt',
				FALSE, NULL, 0, 36);
echo $results;

?>

php number format

<?php
echo number_format("855665568")."<br>";
echo number_format("568956659",2)."<br>";
echo number_format("568985645",2,",",".");
?>

php preg_match

PHP preg_match() Function

<?php
$str = "Visit pakainfo";
$pattern = "/pakainfo/i";
echo preg_match($pattern, $str);
?>

php strtotime

<?php
echo(strtotime("now") . "<br>");
echo(strtotime("9 October 2022") . "<br>");
echo(strtotime("+4 hours") . "<br>");
echo(strtotime("+2 week") . "<br>");
echo(strtotime("+3 week 9 days 6 hours 7 seconds") . "<br>");
echo(strtotime("next Monday") . "<br>");
echo(strtotime("last Sunday"));
?>

php round

<?php
echo(round(0.70) . "<br>");
echo(round(0.55) . "<br>");
echo(round(0.59) . "<br>");
echo(round(-4.39) . "<br>");
echo(round(-4.48));
?>

php json_encode

<?php
$age = array("Mithun"=>56, "Bharat"=>85, "jayesh"=>65);

echo json_encode($age);
?>

php count

<?php
$players=array("Rohit","Sachin","Polard");
echo count($players);
?>

Don’t Miss : PHP Array Functions

Read Also:  PHP Convert multi-dimentional array into single array

php $_server

<?php
echo $_SERVER['PHP_SELF'];
echo "<hr>";
echo $_SERVER['SERVER_NAME'];
echo "<hr>";
echo $_SERVER['HTTP_HOST'];
echo "<hr>";
echo $_SERVER['HTTP_REFERER'];
echo "<hr>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<hr>";
echo $_SERVER['SCRIPT_NAME'];
?>

php var_dump

<?php
$a = 32;
echo var_dump($a) . "<hr>";

$b = "Welcome Website!";
echo var_dump($b) . "<hr>";

$c = 32.5;
echo var_dump($c) . "<hr>";

$d = array("kohali", "rohit", "sachin");
echo var_dump($d) . "<hr>";

$e = array(32, "Welcome Website!", 32.5, array("kohali", "rohit", "sachin"));
echo var_dump($e) . "<hr>";

echo var_dump($a, $b) . "<hr>";
?>

php list

<?php
$players = array("chahal","Hardik","Krunal");

list($a, $b, $c) = $players;
echo "We have differnt players, a $a, a $b and a $c.";
?>

php exit

<?php
$link = "https://www.pakainfo.com";

fopen($link, "r")

or exit("Unable to establish a connection to $link");
?>

php for each

<?php
$player = array("Mithun"=>"35", "Bharat"=>"37", "jayesh"=>"43");

foreach($player as $pl => $val) {
  echo "$pl = $val<br>";
}
?>

php else if

<?php
$team = date("H");

if ($team < "25") {
  echo "Have a best team!";
} else {
  echo "Have a great team!";
}
?>

php memory limit

Extra step for WordPress sites

define( 'WP_MEMORY_LIMIT', '300M' );
define( 'WP_MAX_MEMORY_LIMIT', '300M' );

require_once(ABSPATH . 'wp-settings.php');

php delete file

PHP unlink() Function

<?php
$file = fopen("products.txt","w");
echo fwrite($file,"Welcome Website. Pakainfo.com!");
fclose($file);

unlink("products.txt");
?>

php parsing url

<?php

$url = 'http://pakainfo.com/php/#products';

var_dump(parse_url($url));
var_dump(parse_url($url, PHP_URL_SCHEME));

?>

session_encode php

<html>   
   <head>
      <title>Setting up a PHP session- www.pakainfo.com</title>
   </head>   
   <body>
      <?php 
         session_start();   
         $_SESSION['data'] = "free Download PHP Projects";
         $res = session_encode();
         echo "Encoded Data: ". $res;
      ?>
   </body>   
</html>

include php

<?php include("navigation.html"); ?>  
<h2>Welcome to the world</h2>  

php htmlspecialchars

<?php
$user_content = "Welcome To Pakainfo.com <b>free download source code</b> and demo.";
echo htmlspecialchars($user_content);
?>

php excell

require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator("Pakainfo")
        ->setLastModifiedBy("Jagruti Pandya")
        ->setTitle("MS Office 2011 XLSX Welcome Document")
        ->setSubject("MS Office 2011 XLSX Welcome Document")
        ->setDescription("Welcome doc for MS Office 2011 XLSX, generated by PHPExcel.")
        ->setKeywords("MS office 2011 openxml php")
        ->setCategory("Welcome result file");
$objPHPExcel->getActiveSheet()->setTitle('infinityknow dot com');

I hope you get an idea about PHP function example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.