is it possible to have 2 conditions in the same foreach loop with one which continues, and one which breaks. so I want to find all full input fields and make them $$key
, but make only the first empty input field $_POST['email']
. can I do this in the same loop or do I nee 2 loops? here is an example, this doesn't work, it breaks.
foreach( $_POST as $key=> $for ) {
if ( $for!='') {
$$key = $for; continue;
} else if ( $for=='') {
$$key = $_POST['email']; break;
}
Your example code doesn't make a lot of sense: if ($for!='')
, then the only other thing which can possibly be is $for==''
, therefore the second if clause is redundant. Further, what are you tring to achive by setting the result in $$key
- there may be a reason for this but it is not shown in the code you've published.
Yes, it's quite possible to have multiple break and continue constructs inside a loop. And they will work properly.
However it would be much more efficient to implement your code as simply:
$email=$_POST['email'];
(although this also removes a lot side-effects and potential vulnerabilities from the code).
Yes you obviously can.
foreach( $_POST as $key=> $for ) {
if ( $for != '') {
$$key = $for;
} else {
$$key = $_POST['email'];
break;
}
}