Custom Function and Built-in-Function in PHP

Function play an important role in programming language as it organise our code in a proper manner. We can use our code over and over by naming it only. The function works very similar to variable, but they are more powerful than that.

Function Syntax

<?php
function functionName() {

// Enter your code

}
?>

Example 1

<html>
   <head>
      <title> This is test </title>
   </head>
   <body>
      <?php 
         function student_Details() { 
              echo "Hi my name is john. I study in class third. I live in delhi";
         }
         student_Details();
      ?>
   </body>
</html>

Output

Hi my name is john and study in class third. I live in delhi

Example 2

//Function inside Function
<html>
   <head>
      <title> This is test </title>
   </head>
   <body>
      <?php 
         function init() { 
             student_Details();
             echo "<br>";
         	   calculate();
         }
         
         function calculate() {
             echo 89 + 78 ; 
         }
         
         function student_Details() {
              echo "Hi my name is john and study in class third. I live in delhi";
         }
         init();
      ?>
   </body>
</html>

Output

Hi my name is john and study in class third. I live in delhi
167

 

Function Parameters

Parameters are used to refer to the variable as found in the function while defining the arguments it makes the function more flexible we can do many things.

Example 1

<html>
   <head>
      <title> This is test </title>
   </head>
   <body>
      <?php
         function student($message) {
              echo $message;
         }
         student("Hi my name is John and I am a student");
      ?>
   </body>
</html>

Output

Hi my name is John and I am a student

Example 2

<html>
   <head>
      <title> This is test </title>
   </head>
   <body>
      <?php
         function multiplyNumbers ($number1, $number2){
         	 $multiply= $number1 * $number2;
             echo $multiply;
         }
         multiplyNumbers(28,25);
      ?>
   </body>
</html>

Output 

700

 

Return value from function

<html>
   <head>
      <title> This is test </title>
   </head>
   <body>
      <?php
         function multiplyNumber($number1,$number2){
            $multiply = $number1 * $number2;
         	return $multiply;
         }
         $result =  multiplyNumber(25,25);
         echo $result;
         echo "<br>";
         $result =  multiplyNumber(25,$result );
         echo $result;
      ?>
   </body>
</html>

Output

625
15625

 

Global variables in PHP

A global variables have a Global scope. In php variable start with $ sign followed by the name of that variable $GLOBAL. In PHP global variables are define outside a function and local variable are define inside a function.

<html>
   <head>
      <title> This is test </title>
   </head>
   <body>
      <?php
         $x = "This is Global variable"; // Global variable
         
         function convert() {
         	global $x;
         	$x = "This is Local variable"; // Local variable
         }
         echo $x;
         echo "<br>";
         convert(); //function name is used for define Local variable
         echo $x;
         ?>
   </body>
</html>

Output

This is Global variable
This is Local variable

 

Built-in-Function in PHP

  • Math Functions
<html>
   <head>
      <title> This is test </title>
   </head>
   <body>
      <?php
         echo pow(4,8);
         echo "<br>";
         echo rand();
         echo "<br>";
         echo rand(1, 100); //To execute random number between 1 to 100
         echo "<br>";
         echo sqrt(20);
         echo "<br>";
         echo ceil(20.4);
         echo "<br>";
         echo floor(20.4);
         echo "<br>";
         echo round(4.5);
      ?>
   </body>
</html>

Output

65536
1100905722
86
4.4721359549996
21
20
5

Read all other math function here

 

  • String Functions
<html>
   <head>
      <title> This is test </title>
   </head>
   <body>
      <?php
         $string = "Hi everyone my name is John Doe";
         echo strlen($string); // To find length of String
         echo"<br>";
         echo strtoupper($string); // Convert String in Uppercase
         echo"<br>";
         echo strtolower($string); // Convert String in Lowercase
      ?>
   </body>
</html>

Output

31
HI EVERYONE MY NAME IS JOHN DOE
hi everyone my name is john doe

Read all other string function here

  • Array Functions
<html>
   <head>
      <title> This is test </title>
   </head>
   <body>
      <?php
         $list = [89,78,78,74,373,999,242];
         sort($list); // For sorting the array
         print_r($list); // For Print the array
         echo"<br>";
         echo max($list); // For find maximum value 
         echo"<br>";
         echo min($list); // For find maximum value 
         echo"<br>";
         // For Combine array 
         $x = array('Car', 'Bike', 'Truck');
         $y = array('Datsun', 'Honda', 'Tank Truck');
         $z = array_combine($x, $y);
         print_r($z);
         echo"<br>";
         //Split an array into chunks
         $input_array = array('Datsun', 'Audi', 'Ford', 'Tata', 'Honda');
         print_r(array_chunk($input_array, 3));
         print_r(array_chunk($input_array,3, true));
         echo"<br>";
         //Returning the values from a single column in the input array
         $records = array(
             array(
                 'id' => 100,
                 'first_name' => 'Tata',
                 'last_name' => 'Motors',
             ),
             array(
                 'id' => 101,
                 'first_name' => 'Rolls',
                 'last_name' => 'Royce',
             ),
             array(
                 'id' => 102,
                 'first_name' => 'Land',
                 'last_name' => 'Rover',
             ),
             array(
                 'id' => 103,
                 'first_name' => 'Aston',
                 'last_name' => 'Martin',
             )
         );
          
         $first_names = array_column($records, 'first_name');
         print_r($first_names);
         //Get the column of last names by using id
         $last_names = array_column($records, 'last_name', 'id');
         print_r($last_names);
         ?>
   </body>
</html>

Output

Array ( [0] => 74 [1] => 78 [2] => 78 [3] => 89 [4] => 242 [5] => 373 [6] => 999 )
999
74
Array ( [Car] => Datsun [Bike] => Honda [Truck] => Tank Truck ) 
Array ( [0] => Array ( [0] => Datsun [1] => Audi [2] => Ford ) [1] => Array ( [0] => Tata [1] => Honda ) ) Array ( [0] => Array ( [0] => Datsun [1] => Audi [2] => Ford ) [1] => Array ( [3] => Tata [4] => Honda ) ) 
Array ( [0] => Tata [1] => Rolls [2] => Land [3] => Aston ) Array ( [100] => Motors [101] => Royce [102] => Rover [103] => Martin )

Read all other array function here


Keywords: