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;
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)
How can I get useful error messages in PHP?
(41 answers)
Closed 3 years ago.
I'm trying to check if a value is in a array but it's always returning false. I've tried to fix this in quite a few different ways but none worked.
I have this file which I used require_once '../path/'; to add it to my current script. It is a JSON that was converted to PHP nested arrays.
This is the function that is always returning false. I did a lot of testing using echo and everything looks fine with $states_json and the array $cities.
If anyone could help me with this situation I would be apprciated.
EDIT: I'm calling this function with validateInstCity("RS", "Porto Alegre") so it was supposed to return true. After some more testing, I found out that the problem is that $states_json is NULL within the function. The strange part is that I used it inside others functions before without any problems. As you may see on the file, when using validateInstCity("RS", "Porto Alegre") $idx should be 22 and the function should return true.
function validateInstCity($inst_province = null, $inst_city = null) {
if (empty($inst_province) ||
empty($inst_city)) {
}
$idx;
for ($i=0; $i < count($states_json); $i++) {
if ($states_json[$i]['sigla'] == $inst_province) {
$idx = $i;
break;
}
}
$cities= array();
for ($i=0; $i < count($states_json[$idx]['cidades']); $i++) {
array_push($cities, $states_json[$idx]['cidades'][$i]);
}
if (in_array($inst_city, $cities, false)) {
return true;
} else {
return false;
}
}
This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 5 years ago.
usort($qus_with_ans, "qus_sort");
$ary_val = max(array_column($qus_with_ans, 'updated_at'));
$ary_key = array_search($ary_val, array_column($qus_with_ans, 'updated_at'));
$k = $qus_with_ans[$ary_key];
$curnt_sub_id = $k['subject_id'];
$curnt_sub_name = $k['s_name'];
$last_question_key = end(array_keys($qus_with_ans));
We have a error Strict standards: Only variables should be passed by reference on last line of code i can't understand why error comes Please fix my issue
line no. 138 are $last_question_key = end(array_keys($qus_with_ans));
You can't use function return in end function and should convert it to the variable.
$keys=array_keys($qus_with_ans);
$last_question_key = end($keys);
However, use arrya_pop which is pushing-out last element from the array
$last_question_key = array_pop(array_keys($qus_with_ans));
If you anyway don't have intension to use keys elsewhere from performance point of view.
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I've got this code.
$rollcount=0;
$rollcounts=array(); //I define $rollcounts here
$number_of_tries = 100;
foreach(range(0,$number_of_tries-1) as $i){
do{
$roll=rand(1,6);
$rollcount++;
}while($roll!=6);
array_push($rollcounts, $rollcount);
$rollcount = 0;
}
$freqs = array();
while (!empty($rollcounts)){
$freq = count(array_filter($rollcounts,function($a) use ($rollcounts)
{return $a == $rollcounts[0];}
));
$freqs[$rollcounts[0]] = $freq;
for($i=0;$i<count($rollcounts);$i++){
if(rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}
}
} // redo until $rollcounts is empty
That generates this error message (line 40 has been commented in the code)
Notice: Use of undefined constant rollcounts - assumed 'rollcounts'
in /Applications/XAMPP/xamppfiles/htdocs/learningphp/myfirstfile.php
on line 40
Clearly, $rollcounts has already been defined in the code. So what is the problem here?
You forgot a $
Old code
if(rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}
New code
if($rollcounts[$i] == $rollcounts[0]){ // THIS IS LINE 40
unset($rollcounts[$i]);
}