Jump to content

variables


bagnallc

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
Link to comment
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!
Link to comment
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?


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.