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 4 years ago.
Hi I am new to php and I just run into a problem. I would like to get local variable data outside the function after it was called.
function MyFunction() {
$x = set
}
if($x == "set"){
code...
}
You can't do that. Instead return the value from the function.
function MyFunction() {
$x = "set";
return $x
}
if(MyFunction() == "set"){
code...
}
Related
This question already has answers here:
PHP: how to access variables inside a function that have been declared outside of it?
(3 answers)
Closed 1 year ago.
I am trying to access this variable into function but its give me an error Undefined variable: ERROR.
<?php
$ERROR["emptyEmail"] = "empyt email";
$ERROR["emptyPassword"] = "empty password";
function validateLogin($data) {
if (empty($data["email"])) {
return $ERROR["emptyEmail"];
} else if (empty($data["password"])) {
return $ERROR["emptyPassword"];
} else {
return "valid";
}
}
?>
Insert this inside the function:
global $ERROR;
So, the variable can be accessed inside the function scope (see global keyword).
function validateLogin($data) {
global $ERROR;
...
}
Alternatively you can access to all variables that are outside the function using $GLOBALS:
$GLOBALS['ERROR']
This question already has answers here:
Giving my function access to outside variable
(6 answers)
Closed 6 years ago.
How do I set a variable from inside a function?
$test is supposed to be an object, I don't know if that makes a difference.
<?php
$test;
function hi() {
$test = new obj();
}
?>
Whenever I check if $test is set, I get false. This is not a class so I can't do $this->test
<?php
function hi() {
$test = new obj();
return $test;
}
$test=hi();
?>
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'm not new to programming but I am very new to PHP. I just can't figure out why this variable is not being recognised. I have a file called utils.php in directory utils like this:
<?php
$the_var = 'A'
function foo($bar) {
echo $bar;
}
?>
...and another file called work.php in a parent directory of utils like this:
<?php
include('utils/utils.php');
function doIt() {
echo $the_var; // is always empty
foo('bar'); // no problem
}
?>
Why can't the variable $the_var be accessed?
Variable inside function is not global. If you can access to variable $the_var use
function doIt($the_var) {
echo $the_var;
foo('bar'); // no problem
}
or
function doIt() {
echo $GLOBALS['the_var'];
foo('bar'); // no problem
}
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'm trying to add to, and print the contents of a global array that is being accessed within an individual function.
PHP
<?php
// Globals for data cache
$city_array = [];
/* printArray
* print the value of global array
*/
function printArray() {
print_r($city_array);
}
printArray();
?>
This is returning an error:
Notice: Undefined variable: city_array in /Applications/XAMPP/xamppfiles/htdocs/donorsearch/process.php on line 6
How can I get access to this global array within this local function?
To access global variable in function you must use global to tell PHP you want that:
function printArray() {
global $city_array;
....
}
Either use global:
$city_array = [];
function printArray() {
global $city_array
print_r($city_array);
}
printArray();
Pass via function:
function printArray($array) {
print_r($array);
}
$city_array = [];
printArray($city_array);
This question already has answers here:
How do I get the function name inside a function in PHP?
(4 answers)
Closed 8 years ago.
Is there a function that can return the name of the current function a program is executing?
Yes, you can get the function's name with the magic constant __FUNCTION__
class foo
{
function print_func()
{
echo __FUNCTION__;
}
function print_method()
{
echo __METHOD__;
}
}
$obj = new foo();
$obj->print_func(); // Returns: print_func
$obj->print_method(); // Returns: foo::print_method
Maybe via debug_backtrace http://www.php.net/manual/en/function.debug-backtrace.php