gtener Posted February 4, 2021 Share Posted February 4, 2021 function emptyzipcode($request) { global $requestslimit,$postal1,$postal2,$postal3,$postal4,$postal5,$postal6,$postal7,$postal8,$postal9,$postal10; $string = '('; for ($x = $request+1; $x <= 9; $x++) { $string = $string . '!empty($postal' . $x . ') or '; } $string = $string . '!empty($postal10))'; $postal='postal'. $request; echo $string; echo "<br/><br/>"; echo $$postal; echo "<br/><br/>"; echo $postal2; if (empty($$postal) and $$string){ echo '<h2><font color="red">Failed!</font></h2>'; echo '<h3>Request #' . $request . ' Zip Code Empty!</h3>'; exit(); } } I want to able to use the variable $string as a condition in the if statement; for example $string="(!empty($postal5) or !empty($postal6) or !empty($postal7) or !empty($postal8) or !empty($postal9) or !empty($postal10))" Does anybody know how to do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/312096-macro-substitution/ Share on other sites More sharing options...
gw1500se Posted February 4, 2021 Share Posted February 4, 2021 It is not clear to me what you are asking since "empty" returns a boolean value. Is the objective to return the first $postal* that is not empty? Quote Link to comment https://forums.phpfreaks.com/topic/312096-macro-substitution/#findComment-1584229 Share on other sites More sharing options...
Solution requinix Posted February 4, 2021 Solution Share Posted February 4, 2021 If your code has and $$s in it then it is doing something terribly wrong. Use an array for the postal codes like a normal person. Quote Link to comment https://forums.phpfreaks.com/topic/312096-macro-substitution/#findComment-1584230 Share on other sites More sharing options...
gtener Posted February 4, 2021 Author Share Posted February 4, 2021 Thanks, great idea! I will use an array!!! Quote Link to comment https://forums.phpfreaks.com/topic/312096-macro-substitution/#findComment-1584231 Share on other sites More sharing options...
requinix Posted February 4, 2021 Share Posted February 4, 2021 That was too easy... To make sure we're on the same page, I'm talking about replacing those 10 $postals with a single array containing up to 10 items. Note the "up to". You only store the good values in there, meaning the whole "I have to check if they're empty" doesn't need to happen (at least not once the values are in there). It also means you could store more than 10 if you wanted. Like (up to) 15. Or maybe you decide you have too many and want to lower it to having (up to) 5. Or whatever. And the code using the array wouldn't have to change. Quote Link to comment https://forums.phpfreaks.com/topic/312096-macro-substitution/#findComment-1584232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.