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]);
}
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:
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:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
im still new to php , and i got this error .. pls help me out here , thx .
Notice: Undefined index: sid in C:\wamp\www\test\inc\template.php on line 205
so , here is my code :
<?php
}
elseif($_SESSION['sid'] != '')
{
?>
$_SESSION is an array so you set it like so $_SESSION['sid'] = 'some sid'; if you try to use $_SESSION['sid'] without setting it then PHP throws a notice.
If you want to avoid the notice then one of these options will work:
Option 1
<?php
// check if the variable isset before using it
if(isset($_SESSION['sid']) && $_SESSION['sid'] != '')
{
// do something
}
?>
Option 2
<?php
// turn off error reporting
// NEVER DO THIS IN A DEVELOPMENT ENVIRONMENT
// error_reporting(E_ALL) should be used in development
error_reporting(0);
if($_SESSION['sid'] != '')
{
// do something
}
?>
If you want to see if a session variable is set (or any variable for that matter), use isset(). If you just want to see if it is empty, use empty() after using isset()
elseif(isset($_SESSION['sid']) && !empty($_SESSION['sid']))
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";
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;