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

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;
});

Related

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);
}

PHP Instance variable doesnt work [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
I'm new in OOP with PHP and I'm wondering why I can't declare an instance variable in my class so that I can use it properly. If declaring the variable like on the picture at the top, I get the error message from picture 3. If I add the "public" modifier to the variable, my PHP file says exactly nothing (No Error, just a white empty screen). It all works when I write the string directly into my function, but I wanted to try out using an instance variable.
I tried to solve this problem by myself and didn't find any solutions. So please don't be too mad about it.
Your return $name; searches for a variable $test in your function/method scope. To access the class property, you have to specify it:
class recipeapi
{
// add visibility keyword here
private $name = 'Felix';
// kind of standard is to use get...(), but return...() works the same way
public function getName()
{
// use $this->varname if you want to access a class property
return $this->name;
}
}

I am stuck with $this error using php classes [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
I am new to working with php classes, so bear with me.
I am getting this error:
"Fatal error: Using $this when not in object context in..."
I am attempting to call a function inside the same class. I am able to call the same function from another class so I am confused.
Here is the snippet from the class:
public function listLocations() {
// get all the locations
$listLocations = self::getLocations();
echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div><h2>Locations Class</h2></div>';
foreach ($listLocations as $data) {
echo 'Location - '.$data->location.'<br>';
}
}
public function getLocations(){
$Locations = $this->db->get_results("select location_id, location FROM {$this->soups_locations_db}");
return $Locations;
}
This is inside the Class Foo_Locations
I am calling this same function using this snippet from another class. I get the results that I am looking for without error so I am confused.
$myLocations = count(Foo_Locations::getLocations());
The error is pointing to this line in the Foo_Locations Class
$Locations = $this->db->get_results("select location_id, location FROM {$this->soups_locations_db}");
But I think its related to this line:
$listLocations = self::getLocations();
This community's help is always greatly appreciated.
Thanks in advance
The problem is that you are calling getLocations statically using self::getLocations();
$this->getLocations() allows you to use the instantiated class to call getLocations().

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 - Call one function in another, retrieve variable [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.
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.

Categories