PHP 'illegal offset' [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I have a simple PHP blackjack script that I seem to be getting errors in.
The part of code causing the problem is this;
function evaluateHand($hand) {
global $faces;
$value = 0;
foreach ($hand as $card) {
if ($value > 11 && $card['face'] == 'a') {
$value = $value + 1;
}
else {
$value = intval($value) + intval($faces[$card['face']]); <----- error
}
}
return $value;
}
The error is "Warning: Illegal offset 'face'" on the line I've pointed to above.
What's causing this? Or how I could fix it?

Illegal offset means you are requesting an array key that doesn't exist. In this case, the array in $card has no key face when the error is thrown.

Related

Message: Undefined variable: beauty_detail in CODEIGNITER [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
I have a form where user can select either book or beauty radio button. Code is working fine on book but showing an error on beauty. Code is the same except data fetch from database is different. I have tried but still stuck.
ERROR
A PHP Error was encountered Severity: Notice
Message: Undefined variable: beauty_detail
Filename: controllers/Welcome.php
Line Number: 87
if($result == 0){
echo "no recommendation";
} else{
foreach($result as $key=>$value){
$q = $this->mymodel->fetchBeautydetail($key);
foreach($q as $val){
$beauty_detail[$val->user_id]['product_id'] = $val->product_id;
$beauty_detail[$val->user_id]['product_rating'] = $val->rating;
}
}
(line number: 87) $this->load->view('beauty_dashboard', ['beauty_detail'=>$beauty_detail]);
}
Problem is scope.
Try following. (Declaring beauty_detail out of foreach)
$beauty_detail;
foreach($result as $key=>$value){
$q = $this->mymodel->fetchBeautydetail($key);
foreach($q as $val){
$beauty_detail[$val->user_id]['product_id'] = $val->product_id;
$beauty_detail[$val->user_id]['product_rating'] = $val->rating;
}
}
$this->load->view('beauty_dashboard', ['beauty_detail'=>$beauty_detail]);
}

Undefined offset error in PHP at last line [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I'm getting an undefined offset error
<?php
function ara($one, $two, $three)
{
#preg_match_all('/' . preg_quote($one, '/') .
'(.*?)'. preg_quote($two, '/').'/i', $three, $m);
return #$m[1];
}
$link = "example.com";
$article = file_get_contents($link);
$sport = ara('data-bin="','"',$article);
$channel = ara('data-videobin="','"',$article);
for ($i=0;$i<50;$i++)
echo"<span class='linko'><a target='_blank' href='example.org/live1.php?id=".($channel[$i]."'>$sport[$i]</a>"."<br></span>"); // error is here
?>
How can I fix it? I'am waiting for your helps. Thanks.
These codes are for a live channel website.
The channel variable has less than 50 elements.
Usually in a loop, you should set the end condition to the size of the array.
for ($i=0;$i<sizeof($channel);$i++)

Index setted but undefined index [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I'm in a ambiguous situation.
Here's the concerned code:
http://prntscr.com/9edm4j
public function verifyReply($reponse)
{
$qr = $this->mStopBOT;
if(isset($_SESSION["stopBOT"]))
{
if($_SESSION["stopBot"] === false)
{
$_SESSION["stopBOT"] = true;
if($qr[$_SESSION["stopBOTq"]][1] == $reponse)
return true;
}
}
return false;
}
And here is the problem:
http://prntscr.com/9ednwm
PHP Notice: Undefined index: stopBot in /home/*************/public_html/inc/classes/Security.inc.php on line 92
The isset() function returns true, but when I use the function, it says that the index is undefined! ?
Regards and Thanks in Advance
You are checking if $_SESSION['stopBOT'] is set but then use $_SESSION['stopBot']
Note the case difference, stopBOT vs stopBot

Undefined index: topic in C:includes\topic_list.php on line 9 [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
<?php
$qr_topic = #mysql_query("SELECT * FROM topics");
while ($topic = #mysql_fetch_array($qr_topic)) {
$highlight = "";
**if ($topic['name'] == $_GET['topic'] || $post['topic_id'] == $topic['id']) {**
$highlight = "class='highlight'";
}
echo "<li ".$highlight."><a href='index.php?topic=".$topic['name']."'>".$topic['name']."<img src='img/".$topic['image']."' width='195' height='90' /></a></li>";
}
?>
Getting Undefined index error, not sure what is wrong ? This could be the line for error.
if ($topic['name'] == $_GET['topic'] || $post['topic_id'] == $topic['id']) {**
You try to use $_GET['topic'] without checking if it exists first, that's why you get that error. I'd recommend you to test if the variable exists first using is_set() or empty().

Undefined variable: total in [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I am using foreach over an array and adding each member and assigning total to a variable.
It works like i want, but I get the error:
Notice: Undefined variable: total in C:\xampp\htdocs\preg.php on line
10
I dont quite understand how/why this works and why I get the ^^ error
<?php
$bar = [1,2,3,5,36];
foreach($bar as $value) {
$total = ($total += $value);
}
echo $total;
?>
Before the foreach you need
$total = 0;
foreach($bar as $value){....
The $total variable wasn't initialised, so it couldn't be added to itself. Also, you can rewrite the whole thing like this:
$bar = [1,2,3,4,5];
$total = 0;
foreach($var as $value) {
$total += $value;
}
echo $total;

Categories