yoki32 wrote:I have the two following if conditions which I would like to merge into one if condition. Is that possible to do? The only difference between the two sentences is that $n doesn't point to the same field value.
I might simplify (and correct it) to:
if (array_search($n, $tocheck) !== false
&& ($_POST[$n] == '' || $_POST[$n] == $i || preg_match('/^[0-9]{4}/', $_POST[$n]) == 0)
&& ($n == 'phonenumber1' || $n == 'phonenumber2'))
Note that if you actually want to check if $_POST[$n] is empty then you should use [man]empty[/man] instead of comparing with an empty string. If you want to compare with more than two phone numbers (?), you might write the last clause as in_array($n, array('phonenumber1', 'phonenumber2', 'phonenumber3')).
Of course, I am assuming that the comparison of phone numbers involves OR rather than AND, otherwise the condition will always be false since $n cannot match 'phonenumber1' and 'phonenumber2' at the same time.
Finally, if you decide to turn this into a function as in dagon's example, you would just return the expression rather than check it and then return true or false accordingly.