Difference between returning False or True first
I deliberated whether to post this on code review or here. I came to the
conclusion that it is a straightforward question about PHP and with the
number of supporters here, it would likely get an answer, as code review
is slow and the question has a greater chance of not being answered.
I have the following function.
I can run it to test if true, or else false or vice versa as shown.
$fname=$_POST['fname'];
$lname=$_POST['lname'];
function namecheck ($fname,$lname)
{
$names= array ($fname,$lname);
$regexp ="/^[A-Za-z]+$/";
//filter through names
for ($i=0; $i<2; $i++)
if (! preg_match($regexp, $names[$i]))
{
return false;
}
return true;
}
for ($i=0; $i<2; $i++)
if (preg_match($regexp, $names[$i]))
{
return true;
}
return false;
}
Which is the better way to write this, in terms of efficiency and good
coding practices? Or is there no difference?
For a small array like this, it is not an issue, but I am wondering how
programs are affected when they become larger and more complex.
No comments:
Post a Comment