Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Questions on IF statements

    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.

    Code:
    if (($var_a =="") && ($var_b == "") && ($var_c == "")) {
    Code:
    if ($var_a =="" && $var_b == "" && $var_c == "") {
    also if I remember old Cobol (yes yuk) stuff that if you had a statement like
    Code:
    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?
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #2
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The answers to your questions lie in operator precedence.

    Edit: The answer to your last question is yes btw.

  • #3
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by trib4lmaniac
    The answers to your questions lie in operator 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
    Code:
    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.
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #4
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not sure if you noticed that $repuire is likely spelt wrong and should possibly be $require.. (just an observation ... may not matter)

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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:
    PHP Code:
    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 ... ).

  • #6
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ), anyway I'm re-writting now that I understand a bit more, the || and | had me confused.

    Here's orig code:
    PHP 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($value07) != "http://" && substr($value08) != "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,' ') > || 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);

    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •