PHP - Offset Error Encountered [duplicate] - php

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'];

Related

PHP/CODEIGNITER:Trying to get property of non-object [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I got this error and cant fix it help me please
Message: Trying to get property of non-object
Filename: controllers/auth.php
Line Number: 20
and this is my code:
The line 20 starts at if ($user->mail){
if ($user->mail) {
$this->session->set_flashdata("Success","You Are now logged in");
$_SESSION['user_logged'] = TRUE;
$_SESSION['username']=$user->username;
}
else {
$this->session->set_flashdata("error", "No Account exists in Database");
}
}
The most likely cause of the error message is that $user is null.
Well as the error is self explanatory, you are using:
$user->mail
$user->username
But according to the error $user is not an object and as such you can not get property of non-object. So try to figure out what exactly are you doing with $user, returning an object or an array.

Notice: Use of undefined constant for already defined variable [duplicate]

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]);
}

php Use of undefined constant zip - assumed 'zip' [duplicate]

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];

php switch menu undefined variable [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
The following code has been moved to a new server and is throwing this error:
Notice: Undefined variable: menu in * on line 128
Notice: Undefined variable: menu in * on line 160
Notice: Undefined variable: menu in * on line 170
Here is the code:
Profile
Regisztráció
Kapcsolat
<?php switch($menu)
{
case "profile":
{
echo("profil");
}
case "regisztracio":
{
echo("regisztráció");
}
case "kapcsolat":
{
echo("kapcsolat");
}
default:
{
echo("Home page");
}
}
?>
I didn't understand your lang but the problrem is you are not using $_GET['menu'] to retrieve the GET parameter.
$menu = $_GET['menu'];
switch($menu) {
....
}
Profile
Regisztráció
Kapcsolat
here "menu " is not a php variable. You should pass value as $menu to switch ( $menu = $_GET['menu']; ). Not "menu" to switch.
$menu is undefined.
It is not set anywhere, e.g.
$menu = "profile";

Checking if email is valid using Pregmatch error? [duplicate]

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))

Categories