Notice: Undefined offset: 1 in /somepath/index.php on line 14 - php

I got a problem, I get an error with this code:
$totalpages = substr($totalpages[1],0,1);
The weird thing is, the code works?
This is the error i get:
Notice: Undefined offset: 1 /some/spath.php on line XX</code>

if (isset($totalpages[1]))
$totalpages = substr($totalpages[1],0,1);
BTW, you're getting a notice - not an error. That's the reason why your code still works.

u should check is_array($totalpages) ...
or ini_set('error_reporting', E_ALL & ~E_NOTICE);
more details on error reporting

before you set totalpages to whatever value you set it to, set it as a array $totalpages=array(); OR suppress the warning $totalpages = substr(#$totalpages[1],0,1);

Related

Better PHP Notices

Is there a way to make php notices more informative? For example, I'm getting notices such as
Notice: Undefined offset: 1 in /home/anon/public_html/MotE/lib/Bullet.php on line 18
while running file() to store each line in an array and then exploding each line in the array by \t. Is there a way to have the notice show me which line in the file I'm reading from is causing the issue?
I would like to know if there is a way to make php notices more informative?
Perhaps something like
Notice: Undefined offset: 1 in /home/anon/public_html/MotE/lib/Bullet.php on line 18 [File: /feed.csv]
If I do something like
echo 'line: ' . $arrKey . '<br />';
the notices all disappear but echoing that much information is undesirable and provides no useful information since the notices disappear.
You can look at Xdebug - it will print backtrace for you.
More info here: How can I get PHP to produce a backtrace upon errors?

xampp Notice: Undefined offset

Can anyone help me with this error? When running the site on my host i get no errors but when i run it with xampp on my pc i get this
Notice: Undefined offset: 1 in C:\xampp1\htdocs\ctcoun1kk\countrycheck.php on line 273
Notice: Undefined offset: 2 in C:\xampp1\htdocs\ctcoun1kk\countrycheck.php on line 273
Notice: Undefined offset: 3 in C:\xampp1\htdocs\ctcoun1kk\countrycheck.php on line 273
Line 273 is this one->
$decip = ($numbers[0]*16777216)+($numbers[1]*65536)+($numbers[2]*256)+($numbers[3]);
function x_dot2dec($dotip) {
$numbers = preg_split( "/./", $dotip);
$decip = ($numbers[0]*16777216)+($numbers[1]*65536)+($numbers[2]*256)+($numbers[3]);
return array ($decip, $numbers[0]);
}
Thank you for any help :)
It means that $numbers variable is not set properly. Before 273 line put:
var_dump($numbers);
and check if indexes 0,1,2 and 3 are set
Those are just notices. The script should work fine if you ignore thos. Set error reporting level using error_reporting (http://www.php.net/manual/en/function.error-reporting.php) and those will go away. This is set on your other server- that's why you don't see these "errors".
error_reporting(0); //disable all errors and notices

PHP primitive data type properties

Why is it that getting a property of any PHP primitive data type does not trigger an error or warning?
Tried this on php 5.3.28:
$num = 1;
$str = 'hello';
$arr = array(1,2,3);
$nada = null;
$num->key1;
$str->key2;
$arr->key3;
$nada->key4;
No errors or warnings were triggered.
You don't have any error reporting enabled. When I run your code I see this:
PHP Notice: Trying to get property of non-object in /home/5BSpSI/prog.php on line 8
PHP Notice: Trying to get property of non-object in /home/5BSpSI/prog.php on line 9
PHP Notice: Trying to get property of non-object in /home/5BSpSI/prog.php on line 10
PHP Notice: Trying to get property of non-object in /home/5BSpSI/prog.php on line 11
Try running the code again, but add error_reporting(E_ALL); as the first line

Undefined index: name in Dataset.php after upgrade to php 5.3

PHP Notice: Undefined index: parentid in /home/public_html/data/Dataset.php on line 319
PHP Notice: Undefined index: destinations in /home/public_html/data/Dataset.php on line 330
PHP Notice: Undefined index: radiogroup in /home/public_html/data/Dataset.php on line 340
PHP Notice: Undefined index: radiogroup in /home/public_html/data/Dataset.php on line 340
PHP Notice: Undefined index: radiogroup in /home/public_html/data/Dataset.php on line 340
PHP Notice: Undefined index: radiogroup in /home/public_html/data/Dataset.php on line 340
PHP Notice: Undefined index: name in /home/public_html/data/Dataset.php on line 220
PHP Notice: Undefined index: fieldhelp in /home/public_html/data/Dataset.php on line 236
My script refuses to work after upgrading to php 5.3 from 5.2. I am seeing many PHP Notice in the log.
at line 319: if( $this->aFields["parentid"] ) {
at line 340: if( $curField["radiogroup"] ) {
I suspect the problem is in another file which contains many such lines
if( isset( $this->request_vars[$name]["id"] ) ) {
how do i fix this? if it's that easy by judging from above.
It's not an error. It says that there's no element with index "radiogroup" etc. in array $curField.
You have to check if is it present first using isset, for example:
if(isset($curField['radiogroup']) and $curField['radiogroup']) {
Undefined index means that you try to access a key of an associative array that doesn't exist. This should be present in your old configuration but due to the error reporting level it never came up.
You should alter your code in order to first test if the variable is set and then use it.
For example:
Change occurrences of the form:
if( $this->aFields["parentid"] ) {
...
}
to
if( isset($this->aFields["parentid"]) ) {
...
}
From the PHP documentation (error_reporting):
<?php
// Turn off all error reporting
error_reporting(0);
?>
Other interesting options for that function:
<?php
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
?>
Hard to tell from the code, but I presume the error reporting level has changed, making it now display notices. However, if its possible a variable may not exist, you should use something like:
if( isset($this->aFields["parentid"]) ) {
In your case you could use empty, as that would check its both set and has a value and its not equal to 0/false (same as original line)
if( ! empty($this->aFields["parentid"]) ) {

Reece Calendar event

I have this problem with the script:
Notice: Undefined index: show_times in C:\wamp\www\ReeceCalendar_0.9\cal\gatekeeper.php on line 192
Notice: Undefined index: hours_24 in C:\wamp\www\ReeceCalendar_0.9\cal\gatekeeper.php on line 194
Notice: Undefined index: start_monday in C:\wamp\www\ReeceCalendar_0.9\cal\gatekeeper.php on line 196
Notice: Undefined index: anon_naming in C:\wamp\www\ReeceCalendar_0.9\cal\gatekeeper.php on line 198
and the codes for each line are:
192: if($d['show_times']=='y') $cal_options['show_times'] = TRUE;
194: if($d['hours_24']=='y') $cal_options['hours_24'] = TRUE;
196: if($d['start_monday']=='y') $cal_options['start_monday'] = TRUE;
198: if($d['anon_naming']=='y') $cal_options['anon_naming'] = TRUE;
Please help me!Thnx
It seems that this code was written with notices turned off. If you don't want to rewrite the code you can decrease errors level. Put following line on beginning of script:
error_reporting (E_ALL & ~E_NOTICE);
or change php.ini:
error_reporting = E_ALL & ~E_NOTICE but this is not recommended as it will turn off notices for all your scripts.
You can read about PHP errors reporting levels here.

Categories