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?
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:
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.
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;
This question already has answers here:
Unsetting array values in a foreach loop [duplicate]
(9 answers)
Closed 9 years ago.
I think the code is obvious:
foreach ($programs as $program) {
if ($program->name == 'foo') {
unset($program);
}
}
But it's not working! Isn't it possible to unset current property? Where's the problem? Is there any alternatives?
foreach ($programs as $property => $program) {
// ^-----------^ added
if ($program->name == 'foo') {
unset($programs->$property);
// ^---------^ added
}
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
named PHP optional arguments?
I want to do this:
function they_said($alice = "", $bob = "", $charlie = "") {
echo "Alice said '$alice'";
echo "Bob said '$bob'";
echo "Charlie said '$charlie'";
}
they_said($charlie => "Where are the cookies!?");
That way I can ignore passing the first 2 arguments and simply pass the one I want.
Here's an example in python.
No, but you can pass an array:
function they_said($persons)
{
foreach ($persons as $person => $text)
{
echo "$person said '$text'";
}
}
they_said( array('charlie' => 'Where are the cookies!?') );