Functions in PHP basics. Code example open

Functions in PHP basics. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

Functions are a block of instructions that can be called multiple times in different parts of the program. Functions allow you to divide a program into smaller functional parts.

A function name must begin with an alphabetic character or underscore, followed by any number of alphanumeric characters or underscores.
function showText() {
    $text = "Hi";
}

To call a function:

showText();

function arguments:

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

We can now pass these arguments when the function is called:

myFunc(10,5);

We can also set a default value

function showText($a=10, $b=5) {
    echo $a + $b;
}

Return function

Returns data from the function and stops it:

function showText($a, $b) {
    return 100;
}

function returns 100;

pass the function as a string

function myFunc($a = 1, $b = 2)
{
    return $a + $b;
}

$func = 'myFunc';

echo $funct();

[readaslo id=”854″]

0

More

Leave a Reply

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

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation