FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Global Variable not working. Please test.





imagefree
I have this code to automatically extract expected $_GET variables and make working variables. But when i use var_dump, it shows NULL.

Code:

//Page URL is
//test.php?hamdusa=k
//So it should print k, but it prints NULL


function expect_get( $variable_list = array() )
{
   if( !is_array( $variable_list ) )
   {
      $variable_list   = explode( ',' , $variable_list );
   }
   
   if( count( $variable_list ) )
   {
      foreach( $variable_list as $variable_name )
      {
         $variable_name   = trim( $variable_name );
         
         //Below regx is obtained from php.net docs
         if( preg_match( "/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/" , $variable_name ) )
         {
            if( isset( $_GET[$variable_name] ) )
            {
               $$variable_name   = $_GET[$variable_name];
            }
            else
            {
               $$variable_name   = null;
            }
            GLOBAL $$variable_name;
         }
      }
   }
}

expect_get( 'hamdusa' );
//echo $hamdusa;
var_dump( $hamdusa );
rickylau
Refer to PHP manual page about variable scope, in the example of global keyword, the global statement is executed before the variable is used. Then I modify your codes, placing global statement before assigning value to the variable, and it just works as you expected. Try it Wink
imagefree
thanks rickylau. I will try it.
Nemesis234
could you not just do something like;
Code:
  foreach ($_GET as $key => $value) {
    $_GET[$key] = $value;
  }
Fire Boar
Nemesis234 wrote:
could you not just do something like;
Code:
  foreach ($_GET as $key => $value) {
    $_GET[$key] = $value;
  }


That's... a bit pointless. It does absolutely nothing, though I see where you're going with the example.

rickylau's version works, but probably not quite how you'd expect within functions. I suggest making a class or something to get the variables from statically, then you'd use something like...

Code:
$hamdusa = Request::get('hamdusa');
imagefree
Nemesis234 wrote:
could you not just do something like;
Code:
  foreach ($_GET as $key => $value) {
    $_GET[$key] = $value;
  }


I could use whole $_GET array, but there is a php alternative register_globals, and thats insecure. In my script, it creates only those variables that the script writer wants. Also key in $_GET may be a numeric or in other way may be not a key that can be a valid variable name. So, it may create problems when you expect a variable and it exists in $_GET but as not correspondent $variable.

@rickylau, thanks it worked. I made a little more enhanced version where you can define default value for each variable in case of failure. See: http://phpleaks.blogspot.com/2010/06/creating-variables-from-array-keys-in_20.html (this is my blog where i write my rough ideas but because of poor english i dont usually tell others about it).

@Fireboar, thankls for suggestions. I was just curious whether it was possible to do this with php, and i found a right solution with the help of you guys. Classes are a bit complicated and i hate using classes.
Related topics
Cross-upgrading Mambo to Joomla
sound, movies and link on it - sugestions, please test
please please beta test it (PHP coders)
Need Help in PHP
Upload file
Upload a preset file using PHP
perl help... script almost working... please help..
Menu[1] is Undefined [SOLVED]
PHP IP Ban
The Perfect Audio Rips
php page-refresh/redirect/boot help needed please
Help Please
phpNuke Register new users Problem Problem
AJAX tutorial [2nd part now updated]
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.