0

first time posting here and very new to PHP so bear with me. I have utilized a few sets of scripts I found on the interwebs for a PHP mail referral form I am deploying for a family members health service. It is currently working well validating fields and not allowing the form to be sent unless reCaptcha is returned true. Perfect so far

It's validating fields using a loop of if statements. However I would like it to either gather data from non mandatory fields or skip them. I have had issues using the

if (empty($variable)) {
    continue;
}

If anyone can offer any assistance I would appreciate

  • You can validate variable with isset($variable) function. About it https://www.php.net/manual/en/function.isset.php. empty() is essentially the concise equivalent to !isset($var) || $var == false. – Au Nguyen Sep 25 '19 at 04:53

3 Answers3

0

continue should be used only inside Loops or any other structured statement. https://www.php.net/manual/en/control-structures.continue.php

Change your validation:

if (!isset($variable) || $variable == ''){
   die('Variable is empty');
}
Roldan
  • 225
  • 2
  • 14
0

You can stop the code flow by just using die or exit methods. You can use like this. Depending on data type which you have to check and i assume that you have string data in variable. you can use condition like this.

if(empty($variable_name) OR $variable_name = ''){
exit; //or die;
}

Continue statement cannot be used in if conditions because if is a decision making statement. But you can use if statement in loop and use continue there. Because continue and break only used in loop structure to break the loop and continue the looping when condition full fill.

For Ref.

https://stackoverflow.com/questions/1795025/what-are-the-differences-in-die-and-exit-in-php#answer-50140070

https://stackoverflow.com/questions/4364757/difference-between-break-and-continue-in-php#answer-4364796
https://www.afterhoursprogramming.com/tutorial/php/break-and-continue/
Bilal Ahmed
  • 119
  • 1
  • 11
0

Thanks for the input, everyone. I seemed to solve my own problem. It turns out I didn't need to include what I thought I did in the if statement. I was able to parse the data from non-mandatory fields at the end of the script and output to mail fairly painlessly..