In this article Functions in PHP we give the information about functions are an important part of programming in PHP. This makes code reusable and keeps the program organized and modular.

Functions in PHP

Function is a code block which is given a name. It can be used again and again. Whenever a function is called, it executes the work.

How to create a function in PHP?

Syntax of creating a function

function functionName(parameters) {

    // code block

}

Calling a Function

functionName(arguments);

Types of Function

  1. Built-in Functions: Functions already available in PHP.

Like: strlen(), strpos(), array_push().

  1. User-defined Functions: Functions created by the user.

User-defined Functions (Examples)

Example 1: Function without parameters

<?php

function greet() {

    echo “Hello, this is an example of a PHP Function!<br>”;

}

// call the function

greet();

?>

Output:

Hello, this is an example of PHP Function!

Example 2: Function with parameters

<?php

function greetUser($name) {

    echo “Hello, $name!<br>”;

}

// call the function

greetUser(“Rahul”);

greetUser(“Sonia”);

?>

Output:

Hello, Rahul!

Hello, Sonia!

Example 3: Function with Return Value

<?php

function addNumbers($a, $b) {

    return $a + $b;

?>

// Call the function and print the result

$result = addNumbers(10, 20);

echo “Addition result: $result<br>”;

?>

Output:

Result of addition: 30

Example 4: Default Parameters

<?php

function greet($name = “Guest”) {

    echo “Hello, $name!<br>”;

}

//use default value

greet();

greet(“Ravi”);

?>

Output:

Hello, guest!

Hello, Ravi!

Arguments and Parameters in Function

  1. Pass by Value:

By default, arguments are passed by value in PHP.

<?php

function changeValue($num) {

    $num += 10;

    echo “Inside: $num<br>”;

}

$value = 20;

changeValue($value);

echo “Outside: $value<br>”;

?>

Output:

Inside: 30

Out: 20

  1. Pass by Reference:

If you want to pass a reference to a variable, use &.

<?php

function changeValue(&$num) {

    $num += 10;

    echo “Inside: $num<br>”;

}

$value = 20;

changeValue($value);

echo “Outside: $value<br>”;

?>

Output:

Inside: 30

Out: 30

Variable Functions

In PHP, you can call a variable like a function.

<?php

function sayHello() {

    echo “Hello World!<br>”;

}

$func = “sayHello”;

$func(); // call variable function

?>

Output:

Hello World!

Anonymous Functions (Lambda Functions)

In PHP you can create functions without names.

<?php

$greet = function($name) {

    return “Hello, $name!”;

}

echo $greet(“Ajay”);

?>

Output:

Hello, Ajay!

Arrow Functions (available since PHP 7.4)

Arrow Functions are used for small code blocks.

<?php

$multiply = fn($a, $b) => $a * $b;

echo $multiply(5, 6); //30

?>

Recursive Functions (functions that call themselves)

Example: Using Factorial

<?php

function factorial($n) {

    if ($n <= 1) {

        return 1;

    }

    return $n * factorial($n – 1);

}

echo “Factorial of 5: ” .factorial(5);

?>

Output:

Factorial of 5: 120

Function Scope (Scope Levels)

  1. Local Scope:

Variables created inside the function cannot be accessed outside.

<?php

function test() {

    $x = 10; // Local Scope

    echo $x;

}

test();

// echo $x; // will give error

?>

  1. Global Scope:

Variables declared outside the function can be accessed using $GLOBALS.

<?php

$x = 10;

function test() {

    global $x; // or $GLOBALS[‘x’]

    echo $x;

}

test();

?>

  1. Static Scope:

Static variables are used to retain values ​​between function calls.

<?php

function counter() {

    static $count = 0;

    $count++;

    echo $count . “<br>”;

}

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

?>

Conclusion

  1. Functions make code reusable.
  2. Various methods of passing parameters and values ​​are available.
  3. Built-in, user-defined, and anonymous functions are available in PHP.
  4. Use functions to create modular and scalable code.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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