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 months ago.
I have the following error in PHP error.log
[Tue Dec 19 12:08:22.887574 2017] [:error] [pid 32196] [client xx.xx.xx.x:20560] PHP Notice: Undefined offset: 8 in /var/www/html/page.php on line 55, referer: view.php?1x=8
and the php code that i think causes this is:
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
// echo $_SESSION['websites'][$i];
$website = explode(";", $_SESSION['websites'][$i]);
// echo $website[0];
$i++;
$_SESSION['i'] = $i;
I dont really know what $i = isset($_SESSION['i']) ? $_SESSION['i'] : 0; does
thank you
session_start();
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
if ($_SESSION['i'] < $sesioni1x) {
// echo $_SESSION['websites'][$i];
$website = explode(";", $_SESSION['websites'][$i]);
// echo $website[0];
$i++;
$_SESSION['i'] = $i;
header("Location: $website[0]"); //redirect
die();
// echo $website[0];
// echo $sesioni1x;
// echo $website[0]." Frame-URL<br>";
// $_SESSION['actual_website'] = $website[0];
}
if ($_SESSION['i'] == $sesioni1x) {
$handle = fopen($list1x, "a"); //open file to append content to csv file
fputcsv($handle, $_SESSION['addwebsite'], ";"); //insert line in opened file
fclose($handle); //close file
header("Location: index.php"); //redirect
die();
// echo "session = var";
}
this is the full code where i get this warning, i must say that script is doing his job, but i want to get rid of the error
the $_SESSION['i'] in the source is 0
this is ternary called ternary operator. Basically it is a simple if/else.
$i = (isset($_SESSION['i']) ? $_SESSION['i'] : 0);
To help you understand it's this code in 1 line
if(isset($_SESSION['i'])){
$i=$_SESSION['i'];
}
else{
$i=0;
}
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
The above code is just an if/else nothing else
this will check for the $_SESSION['i'] is it is set then assign it's value to $i otherwise it assigns 0 to $i variable.
Now looking to your problem, it looks like your $_SESSION['i'] is already set and its value is 8 so $i = 8,
Now, $website = explode(";", $_SESSION['websites'][$i]); this like is checking for your $_SESSION['website'] array and trying to find 8th element from the array which has not been set so it gives an error its undefined.
(e) ? r1 : r2 is a conditional statement.
If the expression e is true the value is r1, if e is false the value is r2. So
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
means "i is $_SESSION['i'], if $_SESSION['i'] is set, otherwise it is 0".
Related
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 found the problem on my old apps, PHP Notice: Undefined offset: 1
This is the code:
$uri = "sub.examples.com";
$pageurl = explode("/",$uri);
if($uri=='/') {
$homeurl = "https://".$_SERVER['HTTP_HOST'];
(isset($pageurl[1])) ? $pg = $pageurl[1] : $pg = '';
(isset($pageurl[2])) ? $ac = $pageurl[2] : $ac = '';
(isset($pageurl[3])) ? $id = $pageurl[3] : $id = 0;
} else {
$homeurl = "https://".$_SERVER['HTTP_HOST'].$pageurl[1];
(isset($pageurl[2])) ? $pg = $pageurl[2] : $pg = '';
(isset($pageurl[3])) ? $ac = $pageurl[3] : $ac = '';
(isset($pageurl[4])) ? $id = $pageurl[4] : $id = 0;
}
The errors in the line
$homeurl = "https://".$_SERVER['HTTP_HOST'].$pageurl[1];
Can anyone provide a solution?
Thanks,
Regards.
$homeurl = "https://".$_SERVER['HTTP_HOST'].$pageurl[0];
replace this line.
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.
So i've got a rather complex bit of code that i'm trying to make work (actually; it works fine on my test server but not so much my live server. so i'm trying to make it rework) -- it keeps telling me that a variable i'm asking it to check with isset is not a defined variable. I'm probably over thinking this whole method and there's probably a simplier way. but i need you guys to bash me over the head with it apparently
include('bootstra.php');
include('includes/parts/head.php');
if(!isset($_SESSION['user']['uid']))
{
echo $html;
include('includes/parts/login.php');
if(isset($_GET['mode']))
{
$file = $_GET['mode'];
$path = "includes/parts/{$file}.php";
if (file_exists($path)) {
require_once($path);
} else {
die("The Page REquested could not be found!");
}
}
else
{
include('includes/parts/frontpage.php');
}
}
else
{
if(!isset($_SESSION['x']) || !isset($_SESSION['y']))
{
if(isset($_GET['x']) && isset($_GET['y']))
{
$_SESSION['x'] = $_GET['x'];
$_SESSION['y'] = $_GET['y'];
}
else
{
$_SESSION['x'] = 0;
$_SESSION['y'] = 0;
}
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
if($_SESSION['x'] != $_GET['x'] || $_SESSION['y'] != $_GET['y'] )
{
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
echo $html;
echo $base->displayMap($_GET['x'], $_GET['y']);
include('includes/context_menu.php');
}
}
}
Here is the exact error:
Notice: Undefined index: x in /home/****/public_html/reddactgame/index.php on line 27
Change it
<pre>
if( !isset($_SESSION['x']) || !isset($_SESSION['y']) )
</pre>
to
<pre>
if( !( isset($_SESSION['x']) && isset($_SESSION['y']) ) )
</pre>
There is no error, that is a Warning...
It is saying that $_SESSION['x'] was never setted, so, when you do that isset it tells you that it was never declared.
Example:
This gives you the same warning
empty($x);
This does not give you warning
$x = 0;
empty($x);
EDIT
I see this is a common question, so here is a better explanation.
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.
<?php
function getLeeftijdsCategorie($leeftijd){
if($leeftijd<18){
$categorie="kind";
}
elseif($leeftijd>=18&&$leeftijd<65){
$categorie="volwassen";
}else{
$categorie="bejaard";
}
return $categorie;
}
//globale array met leeftijden
$aLeeftijden = array(16,17,18,14,22,34,67,58,8,4,55,22,34,45,35);
$aantalKind = 0;
$aantalBejaard = 0;
$aantalVolwassen = 0;
for ($x=0; $x <= count($aLeeftijden); $x++) {
if (getLeeftijdsCategorie($aLeeftijden[$x]) == 'kind') {
$aantalKind;
}
if (getLeeftijdsCategorie($aLeeftijden[$x]) == 'volwassen') {
$aantalVolwassen++;
}
if (getLeeftijdsCategorie($aLeeftijden[$x]) == 'bejaard') {
$aantalBejaard++;
}
}
echo "Aantal kinderen : ".$aantalKind;
echo "<br>Aantal volwassen personen : ".$aantalVolwassen;
echo "<br>Aantal bejaarden : ".$aantalBejaard;
?>
Hi, im getting 5 error messages can someone please help me i need to get how many people are children etcetra.
I already tried over an hour but i really cant find it.
The error message is:
PHP Notice: Undefined offset: 15 in D:\ICT Opleiding\Applicatieontwikkeling\phpsemester27\PHPPage.php on line 33
PHP Notice: Undefined offset: 15 in D:\ICT Opleiding\Applicatieontwikkeling\phpsemester27\PHPPage.php on line 37
PHP Notice: Undefined offset: 15 in D:\ICT Opleiding\Applicatieontwikkeling\phpsemester27\PHPPage.php on line 41
Thanks
You “function return value in write context” is related to this line:
if (getLeeftijdsCategorie($aLeeftijden[$x]) = 'bejaard') {
You have to change = in ==.
Then, there is also a Parse Error:
echo "<br>Aantal bejaarden : "$aantalBejaard;
must be:
echo "<br>Aantal bejaarden : " . $aantalBejaard;
# ↑
The undefined offset error is due to your for loop construction:
for ($x=0; $x <= count($aLeeftijden); $x++) {
must be:
for ($x=0; $x < count($aLeeftijden); $x++) {
$aLeeftijden count is 15, but last index is 14.
Give the following a try:
// Improved readability
function getLeeftijdsCategorie( $leeftijd ) {
if( $leeftijd < 18 ) {
$categorie = "kind";
} else if( $leeftijd >= 18 && $leeftijd < 65 ){
$categorie = "volwassen";
} else {
$categorie = "bejaard";
}
return $categorie;
}
//globale array met leeftijden
$aLeeftijden = array(16, 17, 18, 14, 22, 34, 67, 58, 8, 4, 55, 22, 34, 45, 35);
$aantalKind = 0;
$aantalBejaard = 0;
$aantalVolwassen = 0;
for( $x = 0; $x < count( $aLeeftijden ); $x++ ) {
if( getLeeftijdsCategorie( $aLeeftijden[$x] ) == 'kind') {
$aantalKind++; // Forgot ++
}
if( getLeeftijdsCategorie( $aLeeftijden[$x] ) == 'volwassen') {
$aantalVolwassen++;
}
// Forgot =
if( getLeeftijdsCategorie( $aLeeftijden[$x] ) == 'bejaard') {
$aantalBejaard++;
}
}
// Writing strings like this is much less prone to errors
echo "Aantal kinderen : {$aantalKind}";
echo "<br>Aantal volwassen personen : {$aantalVolwassen}";
echo "<br>Aantal bejaarden : {$aantalBejaard}";
don't close php if you include it in some other file this may lead to other errors if you have spaces after the closing php tag.
I want paging is a content of a news blog. Everything works correctly, the page content is successful. But I get an error screen displays PHP:
Notice: Undefined index: page in C:\wamp\www\index.php on line 146
code with the line that gives the error:
$maxreg = 1;
$pag = $_GET['page'];
if (!isset($pag) || empty($pag)){
$min = 0;
$pag = 1;
}else{
if($pag == 1){
$min = 0;
}else{
$min = $maxreg * $pag;
$min = $min - $maxreg;
}
}
include("js/class.AutoPagination.php");
$obj = new AutoPagination(contar_contenido(), $pag);
mostrar_contenido($min,$maxreg);
echo $obj->_paginateDetails();
The line gives the error is this:
$ page = $ _GET ['page'];
The first page by default index.php and contains no var in the url.
I do not understand why if fails below by a conditional var I determine if that has content or not.
Should not show any error php, could someone give me a solution? Thanks
You don't need to do
$pag = $_GET['page'];
Check if $_GET['page'] is set first:
if(!isset($_GET['page']) || empty($_GET['page'])) {
$min = 0;
$pag = 1;
}else{
if($pag == 1){
$min = 0;
}else{
$min = $maxreg * $pag;
$min = $min - $maxreg;
}
}
I quote;
Declare your variables. Or use isset() to check if they are declared before referencing them;
PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"
I found the solution, I've built so, assigning a value to the variable empty get in if it is indefinite.
Maybe it's a very elegant solution, but it works perfect paging.
if(!isset($_GET['page']) || empty($_GET['page'])) {
$_GET['page']="";
}
$maxreg = 2;
$min = 0;
$pag = $_GET['page'];
I think the second conditional left over.
I leave it here in case anyone has the same problem. This way you can fix it.
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 have made this for loop to get a variable amount of columns selected from the database. As I'm running this, I get an error saying :
Notice: Undefined variable: kolom_1
Notice: Undefined variable: kolom_2
Notice: Undefined variable: kolom_3
Notice: Undefined variable: kolom_4
Notice: Undefined variable: kolom_5
Notice: Undefined variable: kolom_6
But I have it all placed in a for loop, why does he not recognize them? I am not getting what I'm doing wrong.
function lijst_ophalen($data, $from){
$totaal = count($data);
for ($i=1; $i<$totaal; $i++){
$kolom_[$i] = $this->mysqli->real_escape_string($data['kolom_' . $i . '']);
if($kolom_[$i]!="") $kolom_[$i] = "{$kolom_[$i]},"; else $kolom_[$i]="";
if($kolom_[$i]==$totaal) $kolom_[$i] = "{$kolom_[$i]}";
}
$from_table = "";
$categorie = "";
if($from == "bv"){
$from_table = "klanten_algemene_gegevens_bv";
$categorie = "";
}
if(($from == "1manszaak") || ($from == "vof")){
$from_table = "klanten_algemene_gegevens_vof_1manszaak";
if($from == "1manszaak"){
$categorie = "1manszaak";
}
if($from == "vof"){
$categorie = "vof";
}
$categorie = "WHERE soort_onderneming = '{$categorie}'";
}
if($from == "ib"){
$from_table = "klanten_ib";
$categorie = "";
}
$result = $this->mysqli->query(
<<<EOT
SELECT
{$kolom_1}
{$kolom_2}
{$kolom_3}
{$kolom_4}
{$kolom_5}
{$kolom_6}
FROM {$from_table}
{$categorie}
EOT
);
if($result){
$waardes = array();
while ($row = $result->fetch_assoc()) {
$waardes[]=$row;
}
return $waardes;
}
}
When you are initializing / filling data into the $kolom_ you are creating an' array instead of your (properly) intended series of different variables.
$kolom_[$i] <--- ARRAY
And lower down you are calling a series of variables $kolom_1, which doesn't exist because you created an' array and not a series of variables.
To avoid the error you can simply change your
$kolom_1
$kolom_2 (and so on)
calls into
$kolom_[1]
$kolom_[2] (and so on)
and you should be set to go.