musher
04-26-2006, 05:54 PM
Is there a difference between the two statements below?
They both seem to execute the same way, is one proper syntax and the other just happens to run correctly? or does it not make a difference.
if (($var_a =="") && ($var_b == "") && ($var_c == "")) {
if ($var_a =="" && $var_b == "" && $var_c == "") {
also if I remember old Cobol (yes yuk) stuff that if you had a statement like if (($var_a =="" && $var_b == "") or $var_c == "") { that var_a and var_b would be checked first and if either a & b were blank or if just c was blank this would be seen as true? does PHP work the same?
trib4lmaniac
04-26-2006, 06:55 PM
The answers to your questions lie in operator precedence (http://uk.php.net/manual/en/language.operators.php#language.operators.precedence).
The answer to your last question is yes btw.
musher
04-26-2006, 07:51 PM
The answers to your questions lie in operator precedence (http://uk.php.net/manual/en/language.operators.php#language.operators.precedence).
Thanks trib4lmaniac, helps to have some one point in the right direction every once in awhile, spent an hr this morning trying to find info on question. And a big thanks also for the link (not just giving the answer) because you also helped answer another question I've had for awhile like what the heck does this do
if ((!$value|| $value == "" || $value == " " || $value == "-" || $value == "http://") && $repuire == true) {$err = true;}
Award 1 Atta-Boy to trib today.
just remember 1 aw-SHooT wipes out 100 atta-boys. :thumbsup:
not sure if you noticed that $repuire is likely spelt wrong and should possibly be $require.. (just an observation ... may not matter)
ralph l mayo
04-26-2006, 10:13 PM
If you have an if statement that looks like that, it's probably best to pare it down a bit if you can. Your example is the same as:
if (!trim($value) || $value == '-' || $value == 'http://' && $require) { /* w/e */ }
If it can't be broken down, you can at least put conditions on a new line so they can be read easily, although that's a style issue and really up to you.
Your example also doesn't need grouping. The final OR condition is evaluated as an OR already because the interpreter works from left to right. Likewise, if the AND condition was first, the ORs *would* need to be grouped, because the first two terms in consideration would be A && B ( || C || D ... ).
musher
04-27-2006, 07:54 AM
Thanks fci, ralph l mayo
Actualy it was a chunk of code I got off here awhile back (field validation routine) can't rember who or what post, I had problems figuring out what it was doing (but it worked :o ), anyway I'm re-writting now that I understand a bit more, the || and | had me confused.
Here's orig code:
function f_CheckInputData($value,$repuire = true,$check = false) {
global $LoggedIn, $UserInfo, $MyData;
$err = false;
if ((!$value|| $value == "" || $value == " " || $value == "-" || $value == "http://") && $repuire == true) {$err = true;}
if ($value && $err == false && $check) {
$split_check = explode(',',$check);
for ($i = 0; $i < count($split_check); $i++) {
if ($split_check[$i] == "url" && (substr($value, 0, 7) != "http://" && substr($value, 0, 8) != "https://") ) {$err = true;}
elseif ($split_check[$i] == "email" && (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$",$value))) {$err = true;}
elseif ($split_check[$i] == "nospecialchar" && (strrpos($value,' ') > 0 || ereg("[^a-zA-Z0-9_]",$value))) {$err = true;}
elseif ($split_check[$i] == "username" && (ereg("[^a-zA-Z0-9_]",$value) || eregi("^((root)|(adm)|(linux)|(webmaster)|(admin)|(god)|(administrator)|(administrador)|(nobody)|(anonymous )|(anonimo)|(anónimo)|(yonetici)|(operator))$",$value) || strrpos($value,' ') > 0)) {$err = true;}
elseif ($split_check[$i] == "numeric" && !is_numeric($value)) {$err = true;}
elseif (substr($split_check[$i],0,7) == "number:") {
$ValLimits = substr($split_check[$i],7,strlen($split_check[$i]));
$split_ValLimits = explode('-',$ValLimits);
$minVal = $split_ValLimits[0];
$maxVal = $split_ValLimits[1];
if ($minVal && $value < $minVal) {$err = true;}
if ($maxVal && $value > $maxVal) {$err = true;}
}
elseif (substr($split_check[$i],0,11) == "limitedlen:" ) {
$limits = substr($split_check[$i],11,strlen($split_check[$i]));
$split_limits = explode('-',$limits);
if (strlen($value) < $split_limits[0] || strlen($value) > $split_limits[1]) {$err = true;}
}
elseif (substr($split_check[$i],0,8) == "compare:") {
$toCompare = substr($split_check[$i],8,strlen($split_check[$i]));
if ($value != $toCompare) {$err = true;}
}
elseif (substr($split_check[$i],0,13) == "checknewuser:" ) {
$DataItems = substr($split_check[$i],13,strlen($split_check[$i]));
$Split_DataItems = explode('-',$DataItems);
$Table = $Split_DataItems[0];
$Field = $Split_DataItems[1];
$NewUserQuery = $MyData->Query("SELECT ".$Field." FROM ".$Table." WHERE ".$Field." = '".$value."' AND status >= 0");
if ($MyData->NumRows($NewUserQuery)) {$err = true;}
}
}
}
return ($err);
}