PHP Functions

A function in PHP is a block of reusable code that performs a specific task. Functions help reduce repetition and make code more organized.

Basic Function Example:
<?php
    function sayHello() {
        echo "Hello, World!";
    }

    sayHello(); // Calling the function
?>

👉 Explanation:

  • Functions are declared using the function keyword.
  • You call a function by using its name followed by parentheses.
  • Functions help write cleaner and reusable code.

Functions with Parameters

Parameters allow you to pass values into a function.

Example:
<?php
    function greet($name) {
        echo "Hello, " . $name;
    }

    greet("Samir");
?>

👉 Key Points:

  • Parameters act like variables inside the function.
  • Values are passed when calling the function.

Functions with Default Parameters

If no value is passed, default values are used.

Example:
<?php
    function welcome($name = "Guest") {
        echo "Welcome, " . $name;
    }

    welcome();          // Output: Welcome, Guest
    welcome("Samir");   // Output: Welcome, Samir
?>

Functions with Return Values

Functions can return data to the place where they are called.

Example:
<?php
    function add($a, $b) {
        return $a + $b;
    }

    $result = add(10, 20);
    echo $result; // 30
?>

👉 Explanation:

  • return sends a value back from the function.
  • The returned value can be stored in a variable.

Function with Type Declaration

You can specify the data type for parameters and return values.

Example:
<?php
    function multiply(int $a, int $b): int {
        return $a * $b;
    }

    echo multiply(5, 3);
?>

Variable Scope in Functions

Variables inside a function are local and cannot be accessed outside.

Example:
<?php
    $x = 100;

    function test() {
        $y = 50;
        echo $y;
        // echo $x; // Error: cannot access global variable directly
    }

    test();
?>

Using Global Variables Inside Functions

Example:
<?php
    $a = 10;
    $b = 20;

    function sum() {
        global $a, $b;
        echo $a + $b;
    }

    sum();
?>

Static Variables in Functions

Static variables retain their value even after the function ends.

Example:
<?php
    function counter() {
        static $count = 0;
        $count++;
        echo $count . "<br>";
    }

    counter(); // 1
    counter(); // 2
    counter(); // 3
?>

👉 Summary:

  • Functions help reuse code and organize logic.
  • They can accept parameters and return values.
  • Local, global, and static scopes work inside functions.
  • Type declarations improve code quality.

PHP Inbuilt (Built-in) Functions

PHP offers hundreds of built-in functions that make development easier. You do not need to create them — just call them directly.

Common Built-in Functions:
<?php
    echo strlen("Hello");      // String length
    echo strrev("Hello");      // Reverse a string
    echo strtoupper("hello");  // Convert to uppercase
    echo rand(1, 100);         // Generate random number
    echo round(3.7);           // Round a number
    echo date("Y-m-d");        // Current date
?>

👉 Popular Categories of Built-in Functions:

  • String functions: strlen(), str_replace(), strpos()
  • Array functions: count(), array_merge(), in_array()
  • Math functions: abs(), round(), rand()
  • Date functions: date(), time()
  • File functions: file_get_contents(), fopen()
  • Variable handling: isset(), empty(), var_dump()

👉 Notes:

  • Built-in functions save time — no need to write the logic yourself.
  • You can mix user-defined and built-in functions in your script.
  • PHP documentation contains full list of built-in functions.
PHP Tutorial