How to Call a PHP Function
Let's say we have a function func that
returns some text:
<?php
function func()
{
return 'text';
}
?>
To call our function, we need to
write its name and parentheses, like this:
func. We can either output the function's result
directly to the screen, or store it
in a variable and then do something with it.
Let's output the result of our function to the screen as an example:
<?php
function func()
{
return 'text';
}
echo func(); // outputs 'text'
?>
Now let's store the function's result in a variable:
<?php
function func()
{
return 'text';
}
$str = func();
?>
Now let's say we have a function that takes a number as a parameter and returns the square of that number:
<?php
function square($num)
{
return $num * $num;
}
?>
Let's find the square of the number
3 using this function:
<?php
function square($num)
{
return $num * $num;
}
echo square(3); // outputs 9
?>
Now let's not output the square of
three directly, but first store it in
a variable, then add the number 1 to this variable,
and output the result to the screen:
<?php
function square($num)
{
return $num * $num;
}
$res = square(3);
echo $res + 1; // outputs 10
?>