John Davidson

arrays - included php file function global var problem

0 comments
Message:


I have a index.php file, i include functions.php


functions.php


function myfunction($var1, &$array_work) {

$array_work = array();
global $array_work;

array_push($array_work, "Test");
}


$var1 = 2;
myfunction($var1, $array_work);

print "<pre>";
print_r($array_work);
print "</pre>";

Everthing works, when i call the file direct (functions.php), but when i include this file into the index.php it doesnt work?


Warning: array_push() expects parameter 1 to be array, null given in

this works now for me:


function myfunction($var1, &$array_work) {
array_push($array_work, "Test");
}


$var1 = 2;
$array_work = array();
myfunction($var1, $array_work);

print "<pre>";
print_r($array_work);
print "</pre>";


My Answer:
$var2) {
global $myGlobalVar;
$result = $var1 + $var2 + $myGlobalVar;
return $result;
}

$myGlobalVar = 10;
$var1 = 5;
$var2 = 3;

echo myfunction($var1, $var2);
?>

In this code, we have a function called myfunction that takes two parameters $var1 and $var2, and also uses a global variable $myGlobalVar. The function calculates the sum of $var1, $var2, and $myGlobalVar and returns the result.

Before calling the function, we set the value of $myGlobalVar to 10, $var1 to 5, and $var2 to 3. Then we call the myfunction with $var1 and $var2 as arguments.

When we run this code, it will output the result of the sum of $var1, $var2, and $myGlobalVar, which is 18.

Rate this post

3 of 5 based on 8963 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us