PHP - Call one function in another, retrieve variable [duplicate] - php

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
Why does echoing the variable from test1() in test2() in the following code not work? I thought if I execute a function in another one it is jsut as if the code was placed in there.
And how can I do it to make it work?
function test1() {
$var = "Hallo";
}
function test2() {
var();
global $var;
echo $var;
}
test2() ;

The $var variable isn't in the scope of the test2() function.
See: this post for details.

Related

undefined global variable in routes/web.php laravel [duplicate]

This question already has answers here:
PHP Anonymous functions: undefined variable
(2 answers)
Closed 6 days ago.
I am trying to define a global variable to use in multiple Laravel routes. However, I am getting "undefined variable $title" as error message. What am I doing wrong? The code on the bottom, throws a similar error.
$title = "YAAA";
Route::get('/insertORM', function(){
$a = $title;
});
This piece of code exists in a standard laravel application inside the routes/web.php file.
The function needs to inherit the variable from the parent scope by using use()
Route::get('/insertORM', function() use ($title, $body) {
$a = $title;
$b = $body;
});

Why can't I access the variables set in a php file [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I have two php files, say
action.php
require_once 'action_helper.php';
storeDataToDb($data); //function from action_helper.php
logPersistIsPerformed(); //function from action_helper
echo $success; //variable set in action_helper.php
action_helper.php
$success = "success";
function storeDataToDB($data) {
// persist data
}
function logPersistIsPerformed() {
insertToDB($success);
}
I'm not sure if this is just a scope issue but what I encounter is when action.php calls the functions and variables declared in action_helper.php there are no issues.
but when I call a function in action_helper.php from action.php, which calls a variable declared in action_helper.php, it doesn't seem to see this success variable.
during debugging, once I loaded the page, I get to see all the variables both from action and action_helper. but when I get to step into the function from action_helper, I'm not able to see the variables declared in action_helper but just the variables passed into that function.
You need to use the global keyword to let PHP know that $success is a global variable.
function logPersistIsPerformed() {
global $success;
insertToDB($success);
}

Why is my defined variable undefined [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
I declared a new variable $var at the top of my document. Afterwards, I called a function which should output this variable.
Unfortunately, I get the following message:
Notice: Undefined variable: var
This is my code:
$var = "abc";
func ();
function func() {
echo $var;
}
In PHP, functions cannot access variables that are within the global scope unless the keyword global is used to 'import' the variable into the function's scope.
You would fix it by doing this:
function func() {
global $var;
echo $var;
}
Read more about scoping here: http://php.net/manual/en/language.variables.scope.php
Any variable used inside a function is by default limited to the local function scope.
In PHP global variables must be declared global inside a function if they are going to be used in that function.
Global variables can also be accessed using $GLOBALS although I would avoid using that unless absolutely necessary.
A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.
Worth mentioning:
It's worth linking to this discussion on globals and why you may not want to use them: Globals are evil
. I'd say there is a preference to use classes instead, or simply pass in the variable as an argument to the function. I won't say not to use globals but at the very least be mindful of its use.
This is because the scope of the variable, either you define it as global, or you pass it as a parameter.
Check the comments under this post:
Variables Scope
The scoping of your variable is wrong. You will need to either pass it as a function parameter or declare it as Global in the function. Please review function scoping.
You could do something like this:
$var = "abc";
func ();
function func() {
global var;
echo $var;
}

call function variable inside function variable [duplicate]

This question already has answers here:
Anonymous recursive PHP functions
(6 answers)
Closed 7 years ago.
$function = function($parameter) use ($variable)
{
//Do some stuff
$function($something); //When I do this i get variable undefined '$function'
}
How can I call the function?
I've already tried to call the parent or redefine the variable but i'd like a more compact solution than just redefining a whole new function and using that.
The general trick here is to use () the Closure holding variable by reference:
$function = function($parameter) use ($variable, &$function) {
//Do some stuff
$function($something);
}

PHP: static variable outside of class [duplicate]

This question already has answers here:
`static` keyword inside function?
(7 answers)
Closed 9 years ago.
I have seen in 3rd party code a variable declared as static, but outside of any class, simply in a "normal" function.
<?php
function doStuff(){
static $something = null;
}
?>
I have never seen static used this way, and I cannot find anything it in the PHP documentation.
Is this legal PHP code? Is this effectively the same as a global variable? If not, what is the purpose?
From the manual:
Another important feature of variable scoping is the static variable.
A static variable exists only in a local function scope, but it does
not lose its value when program execution leaves this scope.
<?php
function test()
{
static $a = 0;
echo $a;
$a++;
}
?>
Now, $a is initialized only in first call of function and every time
the test() function is called it will print the value of $a and
increment it.

Categories