This question already has answers here:
What does the PHP error message "Notice: Use of undefined constant" mean?
(2 answers)
Closed 7 years ago.
Sorry I'm fairly new to PHP and I'm getting this error when trying to use an argument from a function. What am I doing wrong?
public function legislatorsByZip($zip = null) {
$url = "...";
$params = [
zip => $zip,
];
$data= $this->curl->simple_get($url, $params);
return $data;
}
error:
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant zip - assumed 'zip'
Filename: models/CongressAPI.php
Line Number: 11
(zip=> $zip is line 11 btw)
Please let me know if you need more info..
use "zip" instead of zip , the assoc. array should have its keys as strings
or int , words without the $ sign are constants
$params = [zip => $zip];
Change it into
$params = ["zip" => $zip];
Related
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
How to resolve this problem ?
if (strpos($scripts->item($i)->nodeValue, "test") !== false) { //line 127
return true
}
Notice: Trying to get property of non-object on line 127
Check if values exist at all
if(!empty($scripts->item($i)->nodeValue))
{
//your code
}
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]);
}
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
Not sure why I'm getting this error as the code is working and I can see the data I expected.
Code
$allSales = array();
foreach ($game['sales'] as $sale) {
foreach ($sale['values'] as $id => $values) {
$allSales[$id]+=$values['y'];
}
}
Error 1
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: player/game.php
Line Number: 81
Error 2 (Same Code)
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: player/game.php
Line Number: 81
The statement:
$allSales[$id] += $values['y'];
means to take the current value of $allSales[$id], add the value of $values['y'] to it, and store that result back in $allSales[$id]. But the first time that you encounter a particular $id, there is no $allSales[$id]. This results in the warning when you try to get its current value.
Change to:
if (isset($allSales[$id])) {
$allSales[$id] += $values['y'];
} else {
$allSales[$id] = $values['y'];
}
Or you could just prefix the line with # to suppress warnings (I know purists will cry over this):
#$allSales[$id] += $values['y'];
This question already has answers here:
How to validate an Email in PHP?
(7 answers)
Closed 8 years ago.
Hello I want to check if email is valid like blabla#blabla.com and not like just plain text 'BLbalbalba'.
I have this function:
function VerifyEmail($address)
{
$Syntax='#^[w.-]+#[w.-]+.[a-zA-Z]{2,5}$#';
if(preg_match($Syntaxe,$adrdess))
return true;
else
return false;
}
And checking it like this:
$email = htmlentities($_POST['email']);
if (!empty($email) && !empty($password) && !empty($message) && VerifyEmail($email) === true) {
Getting these errors:
Notice: Undefined variable: Syntaxe in C:\xampp\htdocs\recover\inc\functions.inc.php on line 20
Notice: Undefined variable: adrdess in C:\xampp\htdocs\recover\inc\functions.inc.php on line 20
Warning: preg_match() [function.preg-match]: Empty regular expression in C:\xampp\htdocs\recover\inc\functions.inc.php on line 20
Notice: Undefined variable: Syntaxe in C:\xampp\htdocs\recover\inc\functions.inc.php on line 20
Notice: Undefined variable: adrdess in C:\xampp\htdocs\recover\inc\functions.inc.php on line 20
Warning: preg_match() [function.preg-match]: Empty regular expression in C:\xampp\htdocs\recover\inc\functions.inc.php on line 20
Why is this happening? What did I do wrong? Is it a stable way of checking if email is valid?
Thanks!
No, it's not "stable", it's not "valid", and it will not do a good job. use
filter_var($email, FILTER_VALIDATE_EMAIL)
instead. relevant docs: http://www.php.net/manual/en/function.filter-var.php
some spelling mistakes here
should be
function VerifyEmail($address)
{
$Syntax='#^[w.-]+#[w.-]+.[a-zA-Z]{2,5}$#';
if(preg_match($Syntax,$address))