PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Define Function

In PHP, you can create your own 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, but it will execute by a call to the function.

Here's the basic syntax for defining a function in PHP:

function functionName() {
  // Code to be executed
}
  • function: It is a keyword in PHP which is used to declare a function.
  • functionName: It represents the name of the function. The function name should start with a letter or underscore (not a number).

Here's a simple example of a PHP function:

function sayHello() {
  echo "Hello, world!";
}

sayHello();  // Call the function

In this example, we have a function called sayHello() that outputs "Hello, world!". We then call the function using its name followed by parentheses.

Functions with parameters:

You can also define functions with parameters. Parameters are like variables that are used to pass data into functions. Here's an example:

function sayHelloTo($name) {
  echo "Hello, " . $name;
}

sayHelloTo("Alice");  // Outputs: Hello, Alice

In this example, the sayHelloTo() function takes one parameter, $name. When we call the function, we pass the string "Alice" as an argument, which is used as the value of the $name parameter inside the function.

Return values:

Functions can also return values, which can be used as the value of expressions. You can specify a return value using the return statement. Here's an example:

function add($x, $y) {
  return $x + $y;
}

echo add(5, 3);  // Outputs: 8

In this example, the add() function takes two parameters and returns their sum. When we call the function with the arguments 5 and 3, it returns 8, which is then outputted by the echo statement.

  1. How to Create Functions in PHP:

    Create a basic function in PHP.

    <?php
    function greet() {
        echo "Hello, World!";
    }
    
    greet();
    
  2. Defining Custom Functions in PHP:

    Define custom functions for specific tasks.

    <?php
    function addNumbers($a, $b) {
        return $a + $b;
    }
    
    $result = addNumbers(5, 7);
    echo "Sum: $result";
    
  3. PHP Function Parameters and Arguments:

    Utilize parameters and arguments in functions.

    <?php
    function greetUser($name) {
        echo "Hello, $name!";
    }
    
    greetUser("John");
    
  4. Returning Values from Functions in PHP:

    Return values from functions.

    <?php
    function square($number) {
        return $number * $number;
    }
    
    $result = square(4);
    echo "Square: $result";
    
  5. PHP Function Naming Conventions:

    Follow naming conventions for functions (camelCase).

    <?php
    function calculateSum($a, $b) {
        return $a + $b;
    }
    
  6. Using Global Variables in PHP Functions:

    Access global variables within functions.

    <?php
    $globalVar = 42;
    
    function printGlobalVar() {
        global $globalVar;
        echo "Global variable value: $globalVar";
    }
    
    printGlobalVar();
    
  7. PHP Anonymous Functions and Closures:

    Define anonymous functions and use closures.

    <?php
    $add = function ($a, $b) {
        return $a + $b;
    };
    
    $result = $add(3, 5);
    echo "Sum: $result";
    
  8. Recursive Functions in PHP:

    Implement recursive functions.

    <?php
    function factorial($n) {
        if ($n <= 1) {
            return 1;
        } else {
            return $n * factorial($n - 1);
        }
    }
    
    $result = factorial(5);
    echo "Factorial: $result";
    
  9. PHP Function Scope and Visibility:

    Understand function scope and visibility.

    <?php
    $globalVar = 42;
    
    function printGlobalVar() {
        echo "Global variable value: $globalVar"; // Error: $globalVar is not in scope
    }
    
    printGlobalVar();
    
  10. PHP Function Overloading and Default Values:

    Achieve function overloading using default values.

    <?php
    function greetUser($name = "Guest") {
        echo "Hello, $name!";
    }
    
    greetUser(); // Hello, Guest
    greetUser("John"); // Hello, John
    
  11. Variable-Length Argument Lists in PHP Functions:

    Use variable-length argument lists in functions.

    <?php
    function sum(...$numbers) {
        return array_sum($numbers);
    }
    
    $result = sum(1, 2, 3, 4, 5);
    echo "Sum: $result";
    
  12. Declaring Functions in PHP Namespaces:

    Declare functions within namespaces.

    <?php
    namespace MyNamespace;
    
    function myFunction() {
        echo "Function in MyNamespace";
    }
    
    myFunction();