Kingy Posted January 19, 2008 Share Posted January 19, 2008 I havn't noticed this error before but when i ran the script through cmd prompt i keep receiving this... PHP Notice: Undefined offset: 3 in C:\Inetpub\wwwroot\bot.php on line 113 LINE 113: $cmd = str_replace(array(chr(10), chr(13)), '', $ex[3]); Quote Link to comment https://forums.phpfreaks.com/topic/86843-solved-random-error/ Share on other sites More sharing options...
Nhoj Posted January 19, 2008 Share Posted January 19, 2008 What does $ex[3] =? Quote Link to comment https://forums.phpfreaks.com/topic/86843-solved-random-error/#findComment-443847 Share on other sites More sharing options...
toplay Posted January 19, 2008 Share Posted January 19, 2008 The variable $ex is probably an array. So, the error is stating that there is no array index 3 value. If that variable is being used to hold command line arguments, then maybe you're not invoking the script with the right number of arguments. Also, the $ex[3] syntax is used with strings to get the fourth character in the string. It's always best to use isset() to make sure an array index value is there before trying to use it. Example: $value = isset($ex[3]) ? $ex[3] : ''; // default to whatever value - in this case I use an empty string ('') // Then use $value Another example: if (isset($ex[3])) { $cmd = str_replace(array(chr(10), chr(13)), '', $ex[3]); } Quote Link to comment https://forums.phpfreaks.com/topic/86843-solved-random-error/#findComment-443859 Share on other sites More sharing options...
Kingy Posted January 19, 2008 Author Share Posted January 19, 2008 :word it always starts with a colon : and then the next word, until the next space. and its a lot of things because its in irc, so whenever someone talks i get that error Quote Link to comment https://forums.phpfreaks.com/topic/86843-solved-random-error/#findComment-443865 Share on other sites More sharing options...
toplay Posted January 19, 2008 Share Posted January 19, 2008 Why did you remove the solved? I've given you the answer in my previous post. Quote Link to comment https://forums.phpfreaks.com/topic/86843-solved-random-error/#findComment-443869 Share on other sites More sharing options...
resago Posted January 19, 2008 Share Posted January 19, 2008 its not an error. its just telling you that you hadn't previously assigned that index. If it gives you the expected results, ignore it or turn off notices. Quote Link to comment https://forums.phpfreaks.com/topic/86843-solved-random-error/#findComment-443877 Share on other sites More sharing options...
Kingy Posted January 19, 2008 Author Share Posted January 19, 2008 because i have another question.. I was just trying to work it out before i posted it $first = str_replace(":", '', $ex[3]); $string = "$first"." $ex[4]"." $ex[5]"; i have that there, and im getting the same error, i no how to fix it but unsure of exactly the right code if(isset($ex[5])) { $string = "$first"." $ex[4]"." $ex[5]"; } elseif(!isset($ex[5])) { $string = "$first"." $ex[4]"; } elseif(!isset($ex[4])) { $string = "$first"; } that didn't seem to work? i still get PHP Notice: Undefined offset: 4 in C:\Inetpub\wwwroot\bot.php on line 82 but not undefined offser: 5 anymore, so one of them must be working but no the one with $ex[4] Quote Link to comment https://forums.phpfreaks.com/topic/86843-solved-random-error/#findComment-443878 Share on other sites More sharing options...
toplay Posted January 19, 2008 Share Posted January 19, 2008 its not an error. its just telling you that you hadn't previously assigned that index. If it gives you the expected results, ignore it or turn off notices. Yes it's not an error but a notice, however, it's an indication of poor coding practices or bad logic. IMO, these should never be just ignored. The code/logic should be changed to handle not having an index defined properly. Kingy, just do it the way I showed. Here's an example: <?php $first = isset($ex[3]) ? str_replace(":", '', $ex[3]) : ''; $second = isset($ex[4]) ? $ex[4] : ''; $third = isset($ex[5]) ? $ex[5] : ''; $string = $first . ' ' . $second . ' ' . $third; // I assume you still want the spaces in between ?> You still need to check your logic of why the indexes are empty (not defined). If you know why, that's fine. Quote Link to comment https://forums.phpfreaks.com/topic/86843-solved-random-error/#findComment-443884 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.