set value to $_REQUEST variable in PHP - php

Is it possible to assign a value to $_REQUEST variable in PHP? For example:
if ($_REQUEST['name'] == "") {
$_REQUEST['name'] = "John";
}

Yes. $_REQUEST is a variable like any other, but can be pre-populated and is superglobal.

it seems that that variable is read only, if you want to save global variable you can try to make a new variable in the global scope, like this :
$GLOBALS['some_var'] = 'new var';
then you can get the value everywhere in your code, like this :
$your_var = $GLOBAS['some_var'];

Related

Php: check if variable value exist as a variable || make variable value a variable

Is it possible to check if a variable exist using a variable value?
Like
//some variables:
$variable_a;
$variable_b;
$variable_c;
$variable_d;
$variable = '$variable_b';
if (the content $variable which is '$variable_b' exists == true){
}
or how to make a variable value into a variable?
Like
$variable = 'variable_name';
...some code to make 'variable_name' a variable
You can use variable variables in PHP, and then check if such a variable has a value. For instance:
$variableA = 'Hello';
$variableB = 'variableA';
echo ${$variableB};
returns Hello on your screen. You can also check if $variableA has a value by doing:
if (isset(${$variableB})) {
....
}
Note that in your question you have variables that have no value, they are not set. The whole purpose of variables is to have a value, hence their name, so your variables are some kind of zombies, not really alive, not really dead. They are not set, so isset() will return false.

Get a value from an associative array with $_GET

Say for example that I have this variable
$p0001 = array("title"=>"This is the title","name"=>"Just Me");
And the URL is
https://www.example.com?id=p0001
How do I get the correct array from PHP? I've tried
echo $_GET["id"]["title"];
To explain little more, say I have two array variables. I want it to echo the "title" from from the array $p0001. So how do I make sure I gets the variable I put in $_GET?
You can use Variable variables
So, your variable name is $p0001. Your GET parameter id is basically a pointer to the variable name, so using variable variables, we can reference the variable we're looking for:
$varname = $_GET['id']; // $varname = 'p0001';
$$varname; // this is basically $p0001
$$varname['title']; // and you can get your title from $p0001
You could also check if a variable exists before using with isset($$varname)

How to dynamically set the argument to $_GET or $_POST?

I'm writing a function in php to check, if all arguments are set. This is for preventing the program to crash when a argument isn't set.
Unfortunately I get this error:
Notice: Undefined variable: _POST
The code running is the following:
# check for the right arguments
function checkArguments($type, $arg_array) {
# $type is either 'GET' or 'POST'
# $arg_array is an array with all the arguments names
foreach($arg_array as $arg) {
print(${"_" . $type}["name"]);
$type = "_" . $type;
if(!isset($$type[$arg])) {
$missing[] = $arg;
}
}
}
HI I will assume you wanted a variable variable, I try to avoid them as they are very hard to read in code. It also breaks ( or doesn't work in ) most IDE editors.
One thing I just saw that is relevant.
http://php.net/manual/en/language.variables.variable.php
Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.
The $_POST would be counted among the "Superglobal" as is $_GET
You could assign them to a temporary variable and use that.
$arr = $_GET;
if ($type == "POST") $arr = $_POST;

Use variable variables with superglobal arrays

I wonder if is possible to read dynamically superglobal variables, I would like to do something like that:
<?php
$n = 'GET';
$var = '$_'.$n.'[\'something\']'; // pour lire $_GET['something']
echo $var;
//Or
$n = 'POST';
$var = '$_'.$n.'[\'something\']'; // pour lire $_POST['something']
echo $var;
?>
This code don't work as I want, but I would like to know if is workable in PHP?
You can't use variable variables with superglobals, functions or class methods and not with $this.
And a quote from the manual (It's right before the user comments if you search it):
Warning:
Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.
Thank you is exactelly what I search
But we can't use that into function please?
$n = '_GET';
// don't work => Undefined variable: _GET
function f($n) {
echo ${$n}['a'];
}
f($n);
//work fine
echo ${$n}['a'];

How to get the value of a variable which is partially created from a string?

$prefix = 'some';
$name_of_variable = $prefix.'_var';
So I have a variable named $some_var.
How can I check the value of it?
if($name_of_variable) ...
will return the value of $name_of_variable instead of the value of $name_of_variable.
Variable variables. But you do NOT want to use them. They make for impossible-to-debug code. They're almost always a sign of bad design.
DO NOT use a variable which is partially created from a string.
Use arrays instead.
$prefix = 'some';
$name_of_variable = 'var';
echo $array[$prefix][$name_of_variable];
Variable variable usually used when you need to create variables from string,for example convert $_POST keys into variable with its value .
$allowed_var = array('name',..);
foreach( $_POST as $key => $value
{
if( isset($allowed_var[$key] ) )
${$key} = $value;
}
...

Categories