bluebutterflyofyourmind Posted October 21, 2007 Share Posted October 21, 2007 I'm running a loop but eventually I get to a point where i get the error "Notice: Undefined offset:" is there a way to catch this error as it happens and then exit the loop acordingly with out actually hitting the error? Quote Link to comment https://forums.phpfreaks.com/topic/74165-solved-how-can-one-catch-a-notice-undefined-offset/ Share on other sites More sharing options...
phpSensei Posted October 21, 2007 Share Posted October 21, 2007 Can you post the code please? Quote Link to comment https://forums.phpfreaks.com/topic/74165-solved-how-can-one-catch-a-notice-undefined-offset/#findComment-374550 Share on other sites More sharing options...
bluebutterflyofyourmind Posted October 21, 2007 Author Share Posted October 21, 2007 k it's a bit messy rigt now from bug testing but here it is $y = 1; $x = 0; $tempCount = 'x'; $prev = 'y'; $hostPortion = explode(".", $hostName); while($x != 1){ echo $prev." = prev before equal tempcount <BR>"; echo $tempCount." = tempcount before equal prev <BR>"; $prev = $tempCount; echo $prev." = prev after equal tempcount <BR>"; echo $tempCount." = tempcount after equal prev <BR><BR>"; if($hostPortion == false){ echo "hostportion was false and is now exiting the loop"; $x = 1; $count = $y; } else{ $tempCount = $hostPortion[$y]; } echo "host portion after false if = ".$hostPortion[$y]."<BR>"; echo $x." = x after false if statement <BR><BR>"; echo $tempCount." = tempcount after = host protion <BR><BR>"; echo $prev." = prev before if <BR>"; echo $tempCount." = tempcount before if <BR>"; echo $x." = x before if statement <BR>"; /* if($prev == $tempCount){ $count = $y; $x = 1; } */ echo $x." = x after if statement <BR><BR>"; $y++; echo $y." = y at end of loop<BR>"; } echo $hostPortion[$count]."<BR><BR>"; Quote Link to comment https://forums.phpfreaks.com/topic/74165-solved-how-can-one-catch-a-notice-undefined-offset/#findComment-374551 Share on other sites More sharing options...
phpSensei Posted October 21, 2007 Share Posted October 21, 2007 which line does the error occur in? Quote Link to comment https://forums.phpfreaks.com/topic/74165-solved-how-can-one-catch-a-notice-undefined-offset/#findComment-374552 Share on other sites More sharing options...
bluebutterflyofyourmind Posted October 21, 2007 Author Share Posted October 21, 2007 if($hostPortion == false){ echo "hostportion was false and is now exiting the loop"; $x = 1; $count = $y; } else{ $tempCount = $hostPortion[$y]; } echo "host portion after false if = ".$hostPortion[$y]."<BR>"; when it try's to use $hostPortion[$y] pretty much it's getting to the number 6. but that's an error but it still has to run that sixth time in order to make $prev = $tempCount sorry if this is a bit confusing Quote Link to comment https://forums.phpfreaks.com/topic/74165-solved-how-can-one-catch-a-notice-undefined-offset/#findComment-374555 Share on other sites More sharing options...
bluebutterflyofyourmind Posted October 21, 2007 Author Share Posted October 21, 2007 the problem with the code just above is that it's not returning false.....just notifying. what i'm trying to do overall is determine what the ending of the host name of a user is....ie. .com .ca .org excetera. since all host name seem to have a different number of .'s i need to run the loop an unknown amount of time to find the last one. at which point i'm getting the notify error Quote Link to comment https://forums.phpfreaks.com/topic/74165-solved-how-can-one-catch-a-notice-undefined-offset/#findComment-374557 Share on other sites More sharing options...
derwert Posted October 21, 2007 Share Posted October 21, 2007 You can stop the notice by checking if the variable is set first before using it. If you want to do it inline you can use the ternary operator, <?php echo "host portion after false if = ".(isset($hostPortion[$y]) ? $hostPortion[$y] : '')."<BR>"; ?> But the way you're going about this is more than you need. The below code will give you the TLD of the host. <?php $host = 'www.example.com'; echo substr($host, strrpos($host, '.')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/74165-solved-how-can-one-catch-a-notice-undefined-offset/#findComment-374561 Share on other sites More sharing options...
bluebutterflyofyourmind Posted October 21, 2007 Author Share Posted October 21, 2007 hey thanks. the first bit of code you gave did'nt work, but suggestion on how to do it worked well. may i ask exactly what it's doing when i say echo substr($hostName, strrpos($hostName, '.')); thanks Quote Link to comment https://forums.phpfreaks.com/topic/74165-solved-how-can-one-catch-a-notice-undefined-offset/#findComment-374562 Share on other sites More sharing options...
derwert Posted October 21, 2007 Share Posted October 21, 2007 strrpos — Find position of last occurrence of a char in a string.(http://www.php.net/manual/en/function.substr.php) substr — Return part of a string.(http://www.php.net/manual/en/function.strrpos.php) See the links for more information on the parameters. Quote Link to comment https://forums.phpfreaks.com/topic/74165-solved-how-can-one-catch-a-notice-undefined-offset/#findComment-374574 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.