Undefined offset error in PHP at last line [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 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++)

Related

Undefined Index in array from xml [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 1 year ago.
I am getting "Undefined Index FruitOrder" in an if statement for an array which is created from XML/Soap Response
Code:
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $resp);
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//soapBody ')[0];
$array = json_decode(json_encode($body), TRUE);
if($array['FruitOrder']['FruitCode'] == 'FruitSuccess' || $array['FruitOrder']['FruitCode'] == 'FruitSuccessWarnings')
{
To avoid the Undefined Index error you should check if the item ['FruitOrder']['FruitCode'] exists in the array with isset.
if(isset($array['FruitOrder']['FruitCode'])
&& ($array['FruitOrder']['FruitCode'] === 'FruitSuccess'
|| $array['FruitOrder']['FruitCode'] === 'FruitSuccessWarnings'))
{
echo 'process the data';
}

Very strange situation with 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 5 years ago.
php code:
1. if (isset($data['city_id']))
2. {
3. $city_id = "city_id='". $data['city_id']. "', ";
4. }
And I get:
Notice: Undefined index: city_id on line 3
How can this be?
Just ran your code sample and it works perfectly, I do not get 'undefined index' error - taking us to the big apple
<?php
$data['city_id']='New York';
if (isset($data['city_id']))
{
$city_id = "city_id='". $data['city_id']. "', ";
echo $city_id;
}
?>
output: city_id='New York',
Surely, without the $data['city_index']='New York'; I just get a blank screen, as the if condition is not met - no errors.

Notice: Undefined offset: 1 in while loop [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 6 years ago.
I need to make a blog using a TXT file for a school project.
In the text file $blog [0] is the title of the message, $blog1 is the username and $blog[2] is the message itself.
$file = fopen('blogs.txt', 'r');
while(!feof($file)) {
$blog = fgets($file);
$blog = explode("*", $blog);
echo "
<p><strong>". $blog[0]. "</strong>
<br>By: ". $blog[1].
"<br>". $blog[2];
}
The page shows all the messages. But at the bottom I have a couple of 'Undefined Offsets: 1' and 'Undefined offset: 2's. It also says 'By: ' (as shown in the echo) a couple of times.
This is what the page looks like
Check if explode returns more than one element:
$blog = explode("*", $blog);
id (count($blog) >= 3) {
echo "
<p><strong>". $blog[0]. "</strong>
<br>By: ". $blog[1].
"<br>". $blog[2];
} else {
//do some other stuff
}

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

Notice: Undefined index, why? [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 9 years ago.
I am trying to make a library system, everything is going well, but I faced with such a error
Notice: Undefined index: user_log in
C:\xampp\htdocs\e_library\top.php on line 23
and line 23 is here:
<?php
$user_log = $_SESSION['user_log'];
if (isset($_SESSION['user_log'])){
echo "<a href='#' style='color:#FFC'>Welcome $_SESSION[username] </a> || <a href='logout.php' style='color:#FFC'>Logout</a>";
}
else{
echo "<a href='login.php' style='color:#FFC'>Sign In</a>";
}
?>
You should put $user_log = $_SESSION['user_log']; inside your if (isset($_SESSION['user_log'])) block, instead of before.
Please check first:
if (isset($_SESSION['user_log'])){ // Because session array does not contain this yet.
//welcome message and $user_log = ..
}else{
}

Categories