amclean Posted October 2, 2008 Share Posted October 2, 2008 New guy here, having an issue with what I imagine is an unusual script. I've got a form that posts to session variables. Every part of the form is required so I was hoping to save time validating/passing an error back for every iteration, so I decided to try having the script create its own variables. So the first part goes through each Session variable and checks for value. If it has a value of 0 or false, it tries to create a session variable of the same name but with an "error_" prefix to pass back. Then on the original form page when it bounces back (script not shown) each form element with missing values can simply use the newly created variable to set their very own red-pink error background. There are probably better ways to do this, and I'm open to suggestions, but practical or no - I think this should work. The problem is it doesn't work, and I'm not sure why. When I run the script through ZendStudio, it gives me this error (processes and displays everything up until that point): Fatal error: Allowed memory size of 18874368 bytes exhausted (tried to allocate 3776 bytes) in path/validation.php on line 36 When I see it from a browser, it shows everything up until that point but doesn't show that error - just seems to cack out there. Here's the script pertinent to the problem. foreach($_SESSION as $var=> $value) { if ($var<1) { $_SESSION["error_$var"]='style="background-color:#FFD8D9;"'; } } Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/126694-need-help-with-dynamic-or-variable-variables/ Share on other sites More sharing options...
sKunKbad Posted October 2, 2008 Share Posted October 2, 2008 You might try enclosing the $var portion of $_SESSION["error_$var"] with brackets, like: $_SESSION["error_{$var}"] or do something like: $_SESSION["'error_' . $var"] Quote Link to comment https://forums.phpfreaks.com/topic/126694-need-help-with-dynamic-or-variable-variables/#findComment-655311 Share on other sites More sharing options...
discomatt Posted October 2, 2008 Share Posted October 2, 2008 Seems like your variable has nothing to do with this. Something in your script is filling up your memory Quote Link to comment https://forums.phpfreaks.com/topic/126694-need-help-with-dynamic-or-variable-variables/#findComment-655312 Share on other sites More sharing options...
PFMaBiSmAd Posted October 2, 2008 Share Posted October 2, 2008 The posted code dynamically adds elements to the same array that is being looped over, resulting in a forever loop that consumes all available memory. Quote Link to comment https://forums.phpfreaks.com/topic/126694-need-help-with-dynamic-or-variable-variables/#findComment-655327 Share on other sites More sharing options...
amclean Posted October 2, 2008 Author Share Posted October 2, 2008 ok i figured out part of the problem. the "=> $value" part was unnecessary for what i was doing (I think) so i removed it. The revised script has new behavior but still not what I was hoping for. foreach($_SESSION as $var) { if (empty($var)) { $temp="error_$var"; $_SESSION[$temp]='style="background-color:#FFD8D9;"'; #$_SESSION["error_$var"]= } } produces this and stops without looping over anything else: error_=>style="background-color:#FFD8D9;" Quote Link to comment https://forums.phpfreaks.com/topic/126694-need-help-with-dynamic-or-variable-variables/#findComment-655362 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.