Functions In PHP
WORKING WITH FUNCTIONS IN PHP (PART-1)
WORKING WITH FUNCTIONS IN PHP (PART-2)

PHP Functions

  • A function is a block of statements that can be used repeatedly in a program.
  • A function will not execute immediately when a page loads.
  • A function will be executed by a call to the function.
  • Are named section of a program
  • Are used to perform a specific task
  • Split program into modules
  • Are used to enable the developer to reuse the same piece of code
  • Can be easily modified in a program instead of going through entire code to make changes.
  • Function names are NOT case-sensitive.
  • We can set PHP Default Argument Value.
  • Statements are grouped into a single unit to perform a specific task
  • Enhance the logical flow in a program by dividing complicated code sequences into smaller modules
  • Enable to write a piece of code and assign a name to it
  • Include parameters that are:
    • Variables
    • Specified within the parenthesis after the name of a function
    • Used to add more functionality
  • Executed or invoked anywhere in the program using the assigned name

TYPES OF FUNCTIONS

There are 2 types of functions

  1. Built-in or pre-defined functions
  2. Custom or user-defined functions

The real power of PHP comes from its functions; it has more than 1000 built-in functions.

User-defined or custom Functions

  • Function can be defined or created
  • Function definition contains the code to be executed
  • The return expression statement within the body of the function is used to return a value from a function.
  • Note: A function name can start with a letter or underscore (not a number).
  • Tip: Give the function a name that reflects what the function does!

PARAMETERS VS ARGUMENTS

Built-in PHP Functions

  • Provides different built-in functions to be included in the PHP script
  • Built-in functions are grouped into following categories:
    • Mathematical functions
    • String functions
    • Date and time functions
    • Error handling functions
    • Database functions
    • Array functions
    • Mail functions

STRING FUNCTIONS

OPERATE ON CHARACTER TYPE OF DATA.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
    
    
    <?php
    
//    $str1 = "nasir"; // 78
//    $str2 = "nasir"; // 110
//    
//    if(strcmp($str1,$str2) == 0)
//    {
//        echo "Strings Are Same";
//    }
//    else
//    {
//        echo "Strings Are Not Same";
//    }
//    echo strcmp($str1, $str2);
    
    $str = "my name is adil ansari";
//    echo strrchr($str,"a");
    echo stristr($str, "a");
    
//    $name = "adil";
//    echo strrev($name);
//    echo strlen($name);
//    echo strtolower($name);
//    echo strtoupper($name);
    
//    echo bin2hex("A");
    
//    for($i = 97; $i <= 122; $i++)
//    {
//        echo chr($i) . "<br>";
//    }
    
    
    ?>
    
</body>
</html>

MATHEMATICAL FUNCTIONS

OPERATE ON NUMERICAL DATA.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
    
    <?php
    
//    $num1 = 10;
//    $num2 = 5;
//    $num3 = 45;
//    $num4 = 2;
    
//    echo rand(1,10000000);
//    echo rand()*50;
    
//    $a = 5.9; // 5 ----- 5.9 ----------- 6
//    echo floor($a);
//    echo ceil($a);
//    echo round($a);
    
//    echo pow(2, 3); // 2 X 2 X 2
//    echo sqrt(12);
    
//    echo min(22,33,44,55);
//    echo max(22,33,44,55);
    
    
//    $a = -5;
//    echo abs($a);
    
    
    ?>
    
    
</body>
</html>

DATE FUNCTION PHP

Some of the formats that can be used are as follows:

  • d — day of the month
  • D — textual representation of the day
  • l — full name of day
  • j — day of the month without leading zeros (1 to 31)
  • m — numeric representation of the month
  • M — textual representation of the month
  • y — a two digit representation of the year
  • Y — a four digit representation of the year
  • a — Lowercase am or pm
  • A — Uppercase am or pm
  • h — 12 hour format of an hour
  • H — 24 hour format of an hour
  • i — minutes with leading zero
  • s — seconds with leading zeros
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
    
    <?php
    date_default_timezone_set("Asia/Karachi");
    
//    echo date("d");
//    echo date("D");
//    echo date("l");
//    echo date("d");
//    echo date("m");
//    echo date("M");
//    echo date("y");
//    echo date("Y");
    
//    echo date("d-m-y");
//    echo date("l/M/Y");
    
//    echo date("h");
//    echo date("a");
//    echo date("A");
//    echo date("H");
//    echo date("i");
//    echo date("s");
    echo date("h:i:s A");
    
    
    
    ?>
    
</body>
</html>

Passing Arguments to Functions

  • PHP supports passing of arguments to a function
  • The three different ways of passing arguments to a function are as follows:
    • Passing arguments by value
    • Passing arguments by reference
    • Setting default values for arguments
  • The function definition determines the method of passing arguments to the function

Pass by value

  • Passes an argument by value.
  • In this, you are sending a copy of the data.
  • Changes does not affect the actual value.

Pass by reference

  • Passes an argument by reference.
  • In this, you are passing the memory address of the data that is stored.
  • Changes to the value effect the original data.

Source Code

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
    
    <?php
    
//    function Show()
//    {
//        echo "Statement No 1 <br>";
//        echo "Statement No 2 <br>";
//        echo "Statement No 3 <br>";
//    }
//    
//    show();
    
//function Add($num1, $num2)
//{
//    $add = $num1 + $num2;
//    echo $add;
//}
//    Add(10, 20);
    
//    function ShowName($name = "Anonymous")
//    {
//        echo "Your Name is $name <br>";
//    }
//    
//    ShowName();
    
//    function ShowAge($age = 18)
//    {
//        echo "Your Age is $age <br>";
//    }
//    
//    ShowAge();
    
    
    function Subtract($num1, $num2)
    {
        $sub = $num1 - $num2;
        return $sub;
    }
    
    $a = Subtract(20,5);
    $b = 10 + $a;
    echo $b;
    ?>
    
    
</body>
</html>

Download Notes Of This Blog From the link given below

https://www.mediafire.com/file/msv6z1ecciqhql4/FUNCTIONS+PHP.pptx/file

No responses yet

Leave a Reply

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