php - Any difference between returning False or True first? -


i have following function.
can run test if true, or else false or vice versa shown.

    $fname=$_post['fname'];     $lname=$_post['lname'];  function namecheck ($fname,$lname)  {         $names= array ($fname,$lname);      $regexp ="/^[a-za-z]+$/";      //filter through names      ($i=0; $i<2; $i++)     if (! preg_match($regexp, $names[$i]))      {          return false;     }     return true; } 

(alternate version):

for ($i=0; $i<2; $i++) if (preg_match($regexp, $names[$i]))  {     return true; } return false; 

which better way write this, both in terms of efficiency , coding practices? or there no difference?

it may not issue small array this, wondering effect have on larger , more complex program.

the difference both loops checking different results.

  • the 1st loop checking whether $regexp matches elements of array - in case, returns false match fails , if return statement after loop reached, means elements match.
    honest, not having braces around for loop can confusing (like confused me first). suggest add relevant brace:

    for ($i=0; $i<2; $i++) {     if (preg_match($regexp, $names[$i])) {          return true;     } } return false; 
  • the 2nd loop checking whether $regexp matches element of array - in case, return true match succeeds, , if return statement after loop reached, means none of elements match.

    for ($i=0; $i<2; $i++) {     if (! preg_match($regexp, $names[$i])) {          return false;     } } return true; 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -