bagnallc Posted July 27, 2006 Share Posted July 27, 2006 hii have a page with a form on, which has several variables associated with it.when the submit button is clicked it takes you to another page which uses the set variables.i am using isset to establish whether they are set or not.i was wondering though, if there is a function which lists all variables that are set?thanks Quote Link to comment https://forums.phpfreaks.com/topic/15803-variables/ Share on other sites More sharing options...
obsidian Posted July 27, 2006 Share Posted July 27, 2006 not directly, but you could do something like this:[code]<?phpecho "Posted Variables:<br />\n";foreach ($_POST as $key => $val) echo "$key == $val<br />\n";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15803-variables/#findComment-64644 Share on other sites More sharing options...
zq29 Posted July 27, 2006 Share Posted July 27, 2006 Off the top of my head, I don't know a function that lists [i]all[/i] set variables, but you could list the posted variables like so:[code]<?phpecho "<pre>", print_r($_POST), "</pre>";?>[/code][b]EDIT:[/b] Beaten to it! Quote Link to comment https://forums.phpfreaks.com/topic/15803-variables/#findComment-64645 Share on other sites More sharing options...
trq Posted July 27, 2006 Share Posted July 27, 2006 [code=php:0]print_r(get_defined_vars());[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15803-variables/#findComment-64652 Share on other sites More sharing options...
bagnallc Posted July 27, 2006 Author Share Posted July 27, 2006 ok many thanks for all replys. not sure whats best for my own purpose here.in this particular instance, i want to set a new variable if any one of a number of variables from the previous page are set. some variables from the previous page have no bearing though.so my current code would be if(isset($a)) { $new="blah blah"; }elseif(isset($b)) $new=blah blah"; }elseif(isset($c)) $new=blah blah"; }elseif(isset($d)) $new=blah blah"; }im sure there must be a better way though?i had thought there may be something along the lines of - if(isset($a) or isset($b) or isset($c) or isset($d)) { $new="blah blah"; }any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/15803-variables/#findComment-64654 Share on other sites More sharing options...
trq Posted July 27, 2006 Share Posted July 27, 2006 [quote]i had thought there may be something along the lines of[/quote]There is... either the way you just showed, or....[code=php:0]if (isset($a) || isset($b) || isset($c) || isset($d)) { $new="blah blah"; }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15803-variables/#findComment-64656 Share on other sites More sharing options...
obsidian Posted July 27, 2006 Share Posted July 27, 2006 easiest way when coming off a $_POST like that is to extract() all your variables then test them like you and thorpe have already said:[code]<?phpextract($_POST);if (isset($a) || isset($b) || isset($c) || isset($d)) $new = "blah blah";?>[/code]notice that extract() will create variables from an array named by the key Quote Link to comment https://forums.phpfreaks.com/topic/15803-variables/#findComment-64666 Share on other sites More sharing options...
bltesar Posted July 27, 2006 Share Posted July 27, 2006 a better option might be to name the POSTed fields with a unique prefix, e.g. eee_varname1, eee_varname2, etc.then use the follwing code:[code]$flag=false;foreach($_POST as $key=>$value){ if(substr($key, 0, 4)='eee_') { $flag=true; }}if($flag){ $new='blah, blah, blah...';}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15803-variables/#findComment-64704 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.