Jump to content

Archived

This topic is now archived and is closed to further replies.

bagnallc

variables

Recommended Posts

hi

i 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

Share this post


Link to post
Share on other sites
not directly, but you could do something like this:
[code]
<?php
echo "Posted Variables:<br />\n";
foreach ($_POST as $key => $val) echo "$key == $val<br />\n";
?>
[/code]

Share this post


Link to post
Share on other sites
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]<?php
echo "<pre>", print_r($_POST), "</pre>";
?>[/code]
[b]EDIT:[/b] Beaten to it!

Share this post


Link to post
Share on other sites
[code=php:0]
print_r(get_defined_vars());
[/code]

Share this post


Link to post
Share on other sites
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?


Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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]
<?php
extract($_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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.