foreach always returns true/false?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

foreach always returns true/false?

Post by JAB Creations »

This somewhat simple script is supposed to detect (X)HTML validators and allow cloaking to compensate for errors in web standards. However it's always returns true or false (not sure which it is).

Code: Select all

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$validator = array(
'W3C_Validator',
'WDG_Validator',
);

foreach ($validator as $ua) {
 if (stristr($validator,$ua)) { 
  $result = TRUE;
  break;
 }else{
  $result = FALSE;
 }
}

if(!$result)
{echo 'not result';}
else
{echo 'result';}

echo '<br />' . $useragent;
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: foreach always returns true/false?

Post by Christopher »

Code: Select all

<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$validator = array(
'W3C_Validator',
'WDG_Validator',
);

$result = FALSE;
foreach ($validator as $ua) {
 if (stristr($useragent,$ua)) { 
  $result = TRUE;
  break;
 }
}

if(!$result)
{echo 'not result';}
else
{echo 'result';}

echo '<br />' . $useragent;
?>
(#10850)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Oh cool, thanks! They need to post more examples at php.net.
Post Reply