View Full Version : For Each Loop Help

08-13-2007, 11:32 PM
The below loop only seems to increment $abusecount for the first value in the array - if $q contains any of the other array values the script executes. What have I done wrong?

$stopwords = array('<', '>', 'bcc:', 'cc:', 'a href', '[url]');
$abusecount = 0;
foreach ($stopwords as $check){
if (strpos($q, $check) > 0){

if ($abusecount > 0){
} else {
// Execute

08-13-2007, 11:45 PM
The strpos() function returns the position of the string, if it is found, and "false", if it is not. If the string is found in the first byte, i.e. the "0"th byte, strpos() returns "0". Therefore, you cannot do if (strpos($q, $check) > 0), you must instead do if (strpos($q, $check) === false) {/*do false logic */} else {$abusecount++;}

This is covered in depth in the PHP manual (http://us2.php.net/manual/en/function.strpos.php).

08-13-2007, 11:57 PM
Ah yes, thanks Fumigator.