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)
php function variable scope
(3 answers)
Closed 2 years ago.
I need to check whether selected city ( $_GET['city'] ) behaves in $cities array or not. Right now i'm getting errors "Undefined variable: cities" and "Invalid argument supplied for foreach()". If i check $cities with is_array it returns true. How can i fix my code?
$cities = array ("London" => "name/name2/name3",
"Paris" => "name/name3/name4",
"Moscow" => "name/name5/name6",
"Paraguay" => "name/name4/name5");
function CityIsCorrect() { //IN WORK
if (empty($_GET['city'])) {return false;}
foreach ($cities as $citycheck){
if(($_GET['city'])==$citycheck) {return true;}
else {return false;}
}
}
To directly answer your question – add this line to your function:
global $cities
By default, all variables within a function are "local." If you want to refer to a global function (a variable declared outside of any function-block), you must use global.
Related
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)
php: cannot modify array in function?
(2 answers)
Closed 1 year ago.
I want to modify the array in the first function using the second function but it doesn't return the result where it should(first Function).
function first(){
$results = [
'programs' => [],
'events' => [],
];
second('program', $results);
print_r($results); // Don't get results here
}
function second($type,$arr){
$type .= 's';
if(array_key_exists($type,$arr)){
array_push($arr[$type],'title');
}
print_r($arr); // Get results here
}
first();
This question already has answers here:
Variable scope difference between PHP and C: block scope is not exactly the same?
(1 answer)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 1 year ago.
I'm learning PHP and I encounter this piece of code:
$args = [
"msg" => "hello world!"
];
foreach ($args as $key => $value) {
$$key = $value;
}
echo $msg; // prints hello world!
Why can I acces $msg if it was created inside the for loop? Shouldn't it be out of context?
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 3 years ago.
Could please help me understand what I am doing wrong here.
I am able to get the value of $income, but the issue is in the function ovIncome i get this error $income is undefined.
$income = $_REQUEST['income'];
//Gross Income Overview
function ovIncome() {
//Check if Less Than or More Than
if ($income == '0') {
$wageVal = 'Less than € 30.984,- ';
}
elseif($income == '1') {
$wageVal = 'More than € 30.984,- and same as € 61.200,-';
}
else {
$wageVal = 'More than € 30.984,-';
}
echo "$wageVal";
}
You need to pass variable in your function as a parameter.
$income = $_REQUEST['income'];
function ovIncome($income){//pass variable as parameter
And you will good to go.
Sample example: https://3v4l.org/dGYAj
Note: at the time of calling the function you need to pass that variable too( what I did in my code link in last line)
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 am trying to write a PHP function to write error messages to an array. Not sure what I'm doing wrong, still trying to get to grips with functions.
I can make it work without functions, so I guess its the way I'm writing the function that is wrong.
function writeerrors($arr_key, $arr_val){
$errors[$arr_key] = $arr_val;
return;
}
Then I call it here when I check if the form field is empty. If it is empty I want it to write to the $errors array.
//check if empty
if(empty($fname)){
//write to error array
writeerrors('fname', 'Empty field - error');
//Flag
$errors_detected = true;
}else {
Do something else ..}
This is the form... (ONLY TRYING TO VALIDATE FIRST NAME FIELD FOR NOW):
http://titan.dcs.bbk.ac.uk/~mgreen21/p1_prac/PHP_BBK/P1/hoe9/index.php
You just need to specify the global $errors variable, which you have created outside of your function.
function writeerrors($arr_key, $arr_val){
global $errors;
$errors[$arr_key] = $arr_val;
return;
}
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 9 years ago.
$count = 0;
$interpreter->addObserver(function(array $row) use (&$temperature) {
$count+=1;
if ($count < 3) <----- not liking this
{
return;
}
$temperature[] = array(
'column1' => $row[16],
'column2' => $row[18],
);
});
I am assuming it is a scope issue and I am unable to access the count from outside however I do need to count the rows in loop....thoughts?
You could refer to the global by adding the following as the first line of your function:
global $count;
However, does it need to be global? You might create a static variable, which will retain its value between your method calls:
static $count = 0;