Php get all variable names

Php get all variable names

It may be worth specifically noting, if variable names follow some kind of «template,» they can be referenced like this:

// Given these variables .
$nameTypes = array( «first» , «last» , «company» );
$name_first = «John» ;
$name_last = «Doe» ;
$name_company = «PHP.net» ;

// Then this loop is .
foreach( $nameTypes as $type )
print $ < "name_ $type " >. «\n» ;

// . equivalent to this print statement.
print » $name_first \n $name_last \n $name_company \n» ;
?>

This is apparent from the notes others have left, but is not explicitly stated.

In addition, it is possible to use associative array to secure name of variables available to be used within a function (or class / not tested).

This way the variable variable feature is useful to validate variables; define, output and manage only within the function that receives as parameter
an associative array :
array(‘index’=>’value’,’index’=>’value’);
index = reference to variable to be used within function
value = name of the variable to be used within function

$vars = [ ‘id’ => ‘user_id’ , ’email’ => ‘user_email’ ];

function validateVarsFunction ( $vars )

//$vars[‘id’]=34; // define allowed variables
$user_id = 21 ;
$user_email = ’email@mail.com’ ;

echo $vars [ ‘id’ ]; // prints name of variable: user_id
echo $< $vars [ 'id' ]>; // prints 21
echo ‘Email: ‘ .$< $vars [ 'email' ]>; // print email@mail.com

// we don’t have the name of the variables before declaring them inside the function
>
?>

The feature of variable variable names is welcome, but it should be avoided when possible. Modern IDE software fails to interpret such variables correctly, regular find/replace also fails. It’s a kind of magic 🙂 This may really make it hard to refactor code. Imagine you want to rename variable $username to $userName and try to find all occurrences of $username in code by checking «$userName». You may easily omit:
$a = ‘username’;
echo $$a;

If you want to use a variable value in part of the name of a variable variable (not the whole name itself), you can do like the following:

$price_for_monday = 10 ;
$price_for_tuesday = 20 ;
$price_for_wednesday = 30 ;

$price_for_today = $< 'price_for_' . $today >;
echo $price_for_today ; // will return 20
?>

PHP actually supports invoking a new instance of a class using a variable class name since at least version 5.2

class Foo public function hello () echo ‘Hello world!’ ;
>
>
$my_foo = ‘Foo’ ;
$a = new $my_foo ();
$a -> hello (); //prints ‘Hello world!’
?>

Additionally, you can access static methods and properties using variable class names, but only since PHP 5.3

class Foo public static function hello () echo ‘Hello world!’ ;
>
>
$my_foo = ‘Foo’ ;
$my_foo :: hello (); //prints ‘Hello world!’
?>

You may think of using variable variables to dynamically generate variables from an array, by doing something similar to: —

foreach ( $array as $key => $value )
$ $key = $value ;
>

?>

This however would be reinventing the wheel when you can simply use:

extract ( $array , EXTR_OVERWRITE );
?>

Note that this will overwrite the contents of variables that already exist.

Extract has useful functionality to prevent this, or you may group the variables by using prefixes too, so you could use: —

$array =array( «one» => «First Value» ,
«two» => «2nd Value» ,
«three» => «8»
);

extract ( $array , EXTR_PREFIX_ALL , «my_prefix_» );

?>

This would create variables: —
$my_prefix_one
$my_prefix_two
$my_prefix_three

containing: —
«First Value», «2nd Value» and «8» respectively

Another use for this feature in PHP is dynamic parsing..

Due to the rather odd structure of an input string I am currently parsing, I must have a reference for each particular object instantiation in the order which they were created. In addition, because of the syntax of the input string, elements of the previous object creation are required for the current one.

Normally, you won’t need something this convolute. In this example, I needed to load an array with dynamically named objects — (yes, this has some basic Object Oriented programming, please bare with me..)

// this is only a skeletal example, of course.
$object_array = array();

// assume the $input array has tokens for parsing.
foreach ( $input_array as $key => $value ) <
// test to ensure the $value is what we need.
$obj = «obj» . $key ;
$ $obj = new Obj ( $value , $other_var );
Array_Push ( $object_array , $ $obj );
// etc..
>

?>

Now, we can use basic array manipulation to get these objects out in the particular order we need, and the objects no longer are dependant on the previous ones.

I haven’t fully tested the implimentation of the objects. The scope of a variable-variable’s object attributes (get all that?) is a little tough to crack. Regardless, this is another example of the manner in which the var-vars can be used with precision where tedious, extra hard-coding is the only alternative.

Then, we can easily pull everything back out again using a basic array function: foreach.

//.
foreach( $array as $key => $object )

echo $key . » — » . $object -> print_fcn (). »
\n» ;

?>

Through this, we can pull a dynamically named object out of the array it was stored in without actually knowing its name.

Источник

PHP : How to get all variable name in php post method

Solution 3: To output all POST variables, try this: Variables which are included within the URL are GET variables actually: For example if I post Instead of doing: is there a way of looping through all POST variables and setting it to the same named standard variable?

PHP : How to get all variable name in php post method

I would like to get and display all variable names which are posted by method=»post» in a form. I am not aware of variables which passed from post method in HTML. Is there any method to list the all variables posted by post method . Thanks in advance.

example: http://www.dhamu.in/oncreate2.php?workload=10&request_type=project&name=web%20design&description=we%20have%20done%20it&budget=1&bidperiod=11&project_guidelines=checked&job_113=1&xxxx=10 Here i do no the variable name «xxxx»

foreach ($_POST as $key => $value) < echo "= \r\n"; > 

And BTW, those are $_GET variables (so adjust the above to use foreach ($_GET as $key => $value)< .) You can also use $_REQUEST to cover both.

. for all the POST keys and values.

Variables which are included within the URL are GET variables actually:

Make array of all GET-variables, There is a $_GET super global array to get all variables from query string. //

PHP — Capture All POST Variables

Is there a way in PHP to capture all POST and GET variables that are sent to a page?

I am testing my PayPal Subscription website in the PayPal Sandbox but every transaction I do triggers the invalid transaction in my code.

So, I would like to capture everything that PayPal sends to my ipn page and see if I can make sense of what is going on. Is this possible?

For a PayPal IPN , you should be able to use $postdata = file_get_contents(‘php://input’); to fetch the raw post data needed for the validation callback.

I’ve got a simple method that helps me capture all the post data:

$post_vars = ""; if ($_POST) < $kv = array(); foreach ($_POST as $k =>$v) < if (is_array($v)): $temp = array(); foreach ($v as $v2) < $temp[] = $v2; >$kv[] = "$k=" . join("|", $temp); else: $kv[] = "$k=$v"; endif; > $post_vars = join("&", $kv); > 

This allows you to capture all the post data (regardless of its name or value) and then store than in a string, great for inserting into a database, though you might want to url encode it. I’ve updated it to include support for arrays but you’ll have to customise it for your own requirements, it produces output like this:

On your IPN page, you can log all get, post and cookie variables pretty easy.

ob_start(); print_r($_REQUEST); $data = ob_get_contents(); ob_end_clean(); file_put_contents("Path/to/log.file",$data); 

Get values are in the $_GET array.

Get, post and cookie values are placed in $_REQUEST

Simply save and/or print the contents of those arrays as needed.

For more detailed info, check out:

PHP Iterate through $_POST and use values by name, $_POST as $key => ; if (strstr · $key, ; $id = str_replace · ‘item’,

PHP, set all $_POST to its own named variable

on an PHP POST, is there a way of automatically setting each POST variable to its own named variable?

name = "henry" age = "20" location = "earth" 
$name = $_POST['name']; $age = $_POST['age']; $location = $_POST['location']; 

is there a way of looping through all POST variables and setting it to the same named standard variable?

$_POST = ['foo' => 'bar']; extract($_POST); var_dump($foo) //returns 'bar' 

It is not recommended to set $_POST variables programmatically. However, if you want it you can use extract function

 extract($_POST,EXTR_OVERWRITE,'prefix'); 
if ($_POST) < foreach ($_POST as $key =>$value) < $name = ""; $$name = $value; echo "
"; echo $name; > echo "
"; echo $name; echo "
"; echo $age; echo "
"; echo $location; > 

PHP post data convert to local variables, foreach($_POST as $variable => $value) $$variable = $value;. Then if you do a var_dump($manufacturer) : string(10) "Dell Inc.

Источник

Читайте также:  Html htm php opendivx md5
Оцените статью