The code from the official example:
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
Result: 2
If $a
and $b
are declared global
outside the function then it works.
<?php
global $a, $b; // ← added this
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
Result: 3
PHP 5.6.4
Why so?
UPDATE: Thanks for the comments. Apparently, the problem is in the CMS. I will look for the reason in it.
My Answer:
A PHP function is a block of code that performs a specific task and can be reused throughout a PHP script. Functions in PHP are defined using the `function` keyword followed by the function name and any parameters that the function may accept. Here is an example of a simple PHP function that adds two numbers together:
php
function addNumbers($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}
$result = addNumbers(5, 10);
echo $result; // Output: 15
In this example, the `addNumbers` function takes two parameters `$num1` and `$num2`, adds them together, and returns the result. The function is then called with the values `5` and `10`, and the result is stored in the variable `$result` and printed to the screen.
Rate this post
3 of 5 based on 9046 votesComments