Jump to content


Photo

variables


  • Please log in to reply
7 replies to this topic

#1 bagnallc

bagnallc
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 27 July 2006 - 01:46 PM

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 July 2006 - 01:59 PM

not directly, but you could do something like this:
<?php
echo "Posted Variables:<br />\n";
foreach ($_POST as $key => $val) echo "$key == $val<br />\n";
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 27 July 2006 - 02:01 PM

Off the top of my head, I don't know a function that lists all set variables, but you could list the posted variables like so:
<?php
echo "<pre>", print_r($_POST), "</pre>";
?>
EDIT: Beaten to it!

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 27 July 2006 - 02:09 PM

print_r(get_defined_vars());


#5 bagnallc

bagnallc
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 27 July 2006 - 02:12 PM

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?




#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 27 July 2006 - 02:15 PM

i had thought there may be something along the lines of


There is... either the way you just showed, or....

if (isset($a) || isset($b) || isset($c) || isset($d)) { $new="blah blah"; }


#7 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 July 2006 - 03:13 PM

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:
<?php
extract($_POST);
if (isset($a) || isset($b) || isset($c) || isset($d)) $new = "blah blah";
?>

notice that extract() will create variables from an array named by the key
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#8 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 27 July 2006 - 04:02 PM

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:
$flag=false;
foreach($_POST as $key=>$value)
{
     if(substr($key, 0, 4)='eee_')
     {
          $flag=true;
     }
}
if($flag)
{
     $new='blah, blah, blah...';
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users