1

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>
6
  • 1
    What are you trying to calculate? Nov 9, 2010 at 18:09
  • I'm not calculating anything, there's a string with the first character being a integer, I want this one in a variable.
    – seymar
    Nov 9, 2010 at 18:27
  • You seem to be under the impression that an E_NOTICE denotes unclean code. This isn't necessarily the case.
    – ceejayoz
    Nov 9, 2010 at 18:28
  • I want it uberbugfree :P
    – seymar
    Nov 9, 2010 at 18:30
  • An E_NOTICE is not a bug. It's a notice.
    – ceejayoz
    Nov 9, 2010 at 18:33

3 Answers 3

3
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.

1
  • Ah I was executing the code two times, it was in an for-loop. I didn't notice that. The first loop it went well, second didn't work. Fixed! Thnx guys.
    – seymar
    Nov 9, 2010 at 18:50
2

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

3
  • My purpose isn't getting rid of the notice, but preventing it. I want clean code.
    – seymar
    Nov 9, 2010 at 18:18
  • Tested <code>is_array</code>, and yes it's an array. It returns true.
    – seymar
    Nov 9, 2010 at 18:24
  • further check isset($totalpages[1]) as other users suggested
    – ajreal
    Nov 9, 2010 at 18:25
1

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);

1
  • It's an array, coming out an explode function :) I'm going to try to set it as an array first. My purpose isn't getting rid of the notice, but preventing it. I want clean code.
    – seymar
    Nov 9, 2010 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.