Hello guys am a newbie to php .I am here to ask a question as usual.I have seen a code like this ..
switch($_SERVER['REQUEST_METHOD'])
{
case 'GET': $the_request = &$_GET; break;
case 'POST': $the_request = &$_POST; break;
.
. // etc.
.
default:
}
?>
My doubt is $_get here .As we know s_get is used to post the details of the variable to the server.But here $_POST is used as a variable ..
My question is that can we use $_POST as a variable like $_POST = $something..
Am sorry if my question is not up to the standdard since am a newbie ..Any help would be appreciated ..Thanks .:)
yes, $_POST its just an array.
In PHP $_POST is a superglobal array that is populated by data sent via the http post method.
You can manually add to the array, delete things from the array and manipulate it in any way you can manipulate other arrays in PHP.
See the manual page
$_POST is normal array, visible in global scope (see docs). There's rather no point of assigning its values to another array as in most cases there's no benefit from doing so.
As we know s_get is used to post the details of the varibale to the
server
Wrong. $_GET is used to grab the data that you have passed through the querystring of your URL.
an we use $_POST as a variable like $_POST = $something..
Yes but not directly.. You could do this way..
<?php
$somevar = $_POST;
echo $somevar['name'] = 'John';//
You can directly use $_GET and $_POST variables as a normal array.. you can add values, assign values, alter, delete values from it.
There is no need to create a new array and assign values of $_GET or $_POST to it except in some special cases.
Related
I have a general question. I wrote the below code to add an array of arrays to the $_POST variable. I know the post variable is an array too. So, after adding my multidimensional array to the post variable one by one, I tried to print_r the data to view it but, only one array would print out.
why is it that only one array would print out?
$x = 0;
for($x; $x < count($return_auth); $x++){
$_POST = $return_auth[$x];
}
print_r($_POST);
Although nerdlyist answer is absolutely right, but I think it is not the right way, in short, you are modifying $_POST variable and since $_POST variable has a meaning attached to it that it contains all the parameters sent in that POST request. If you overwrite it that change will be affected throughout your application and other modules running in your application will see an additional post parameter which is not actually sent in that request in $_POST array which is not right IMO.
What you are doing is overwriting post. Based on what you have here you could do something like this:
$_POST[return_auth] = $return_auth;
Unless you have a reason to loop an array to create an array...
use this in your for loop
$_POST[] = $return_auth[$x];
Edit
this will work better
$_POST['something' . $x] = $return_auth[$x];
now you can access to (foo) for example
$_POST['somethingfoo'];
I want clear $_POST array content fully, all examples what I see in internet, looks like this:
if (count($_POST) > 0) {
foreach ($_POST as $k=>$v) {
unset($_POST[$k]);
}
}
Tell please, this variant will be not more better? (Point of view as saving resources)
if (count($_POST) > 0) {
$_POST = array();
}
or not ?
Yes, that is fine. $_POST is just another variable, except it has (super)global scope.
$_POST = array();
...will be quite enough. The loop is useless. It's probably best to keep it as an array rather than unset it, in case other files are attempting to read it and assuming it is an array.
To unset the $_POST variable, redeclare it as an empty array:
$_POST = array();
The solutions so far don't work because the POST data is stored in the headers. A redirect solves this issue according this this post.
How to delete $_POST variable upon pressing 'Refresh' button on browser with PHP?
It may appear to be overly awkward, but you're probably better off unsetting one element at a time rather than the entire $_POST array. Here's why: If you're using object-oriented programming, you may have one class use $_POST['alpha'] and another class use $_POST['beta'], and if you unset the array after first use, it will void its use in other classes. To be safe and not shoot yourself in the foot, just drop in a little method that will unset the elements that you've just used: For example:
private function doUnset()
{
unset($_POST['alpha']);
unset($_POST['gamma']);
unset($_POST['delta']);
unset($_GET['eta']);
unset($_GET['zeta']);
}
Just call the method and unset just those superglobal elements that have been passed to a variable or argument. Then, the other classes that may need a superglobal element can still use them.
However, you are wise to unset the superglobals as soon as they have
been passed to an encapsulated object.
You can use a combination of both unset() and initialization:
unset($_POST);
$_POST = array();
Or in a single statement:
unset($_POST) ? $_POST = array() : $_POST = array();
But what is the reason you want to do this?
To answer "why" someone might use it, I was tempted to use it since I had the $_POST values stored after the page refresh or while going from one page to another. My sense tells me this is not a good practice, but it works nevertheless.
My page url just like that
http://wallpapers.wapego.net/main.php?ses=SuNpjmgtjmN7&f=3153038
now i want to take a part of current url that is 3153038 and store it as php integer variable
$_GET['ses'] will give you 'SuNpjmgtjmN7' and $_GET['f'] will give you '3153038'
Check $_GET
Use a GET super global or REQUEST super global variable.
ex -
$value = $_GET['f']; or $value = $_REQUEST['f'];
$_GET super global uses for get values from the URL. $_POST super global uses for put values to the server. I hope you understand it.
Provided your calling this URL in a 'get request' the value of 'f' will be in the $_GET array.
But the best way to store this as an integer value would be to parse it through intval()
$value = intval($_GET['f']);
Best way is to do it like this..
intval($_REQUEST['f']);
PHP has list of reserved variables, visit here.
If i use one of the reserved keyword as my variable, it is working for me.
<?php
$_GET = 10;
echo $_GET;//10
?>
Please correct me, if my understanding is wrong?
"Predefined" is not the same as "reserved". PHP gives these variables default values, but you can still use their names for your own purposes. But you shouldn't, since it's poor style.
If you pass GET data into that file like http://example.com/example.php?id=1
Then it will conflict with your $_GET variable..
$_GET = Array { [id] => 1 }
After your declaration the value will be changed as
$_GET = 10
It will overwrite the old value..
I can't imagine a situation where you would want to do what you're suggesting.
in the case of $_GET, $_SESSION, they are arrays where you can assign a key value pair at will, that's what they're designed for.
$_SESSION["UID"]=username;
for example
$_GET is not a reserved variable.
$_GET is an associative array of variables passed to the current script via the URL parameters. Also is a superglobal. Read the documentation to learn how to use it
You should use like
$_GET['key']=10;//good
Doing
$_GET =10;//bad
will overwrite the value and it works but is a very wrong way to use a superglobal and they were not designed to work that way
I have several variables coming from an array in $POST_['array'] i wish to make some kind of loop for example foreach that makes, for every value in the variable a variable name of it and assigns the value for it.
For example if i have
$POST_['name'];
$POST_['last'];
$POST_['age'];
$POST_['sex'];
I want the loop to create each variable from the array inside the $_POST with the name of the variable like the following:
$name = 'John';
$last = 'Doe';
$age = '32';
$sex = 'male';
NOTE - The array is coming from a serialized jquery string that puts together all the variables and values in a form into one big string.
Is this possible?
You don't need a loop, you want extract:
extract($_POST); // But use caution, see below
Cautions and best practices
As noted in the comments this forces all parameters in the $_POST array into the current symbol space.
In global space
<?php
extract($_GET);
var_dump($_SERVER); // Can be overwritten by the GET param
?>
The code above illustrates the problem as shown in this answer — some pretty dangerous things can be overwritten in the global space.
Inside a function
function myFunc() {
// (Mostly) empty symbol space! (excluding super globals)
extract($_POST);
}
Inside a function, as the first line, no harm done.
Important note: You might think since $_SERVER is a super global, that this exploit could happen inside a function as well. However, in my testing, on PHP Version 5.3.4, it is safe inside a function — neither $_SERVER, $_POST, $_GET, $_SESSION, or presumably other superglobals, could be overwritten.
With options
You can also use extract with extract_type options that do not overwrite.
The best option to use, in my opinion, is simply to prefix all variables from extract:
// $_GET = test=1&name=Joe
extract($_GET, EXTR_PREFIX_ALL, "request_get");
echo $request_get_test; // 1
echo $request_get_name; // Joe
That way you don't have the overwrite problem, but you also know you got everything from the array.
Alternate - looping w/ conditional
If you wanted to do this manually (but still dynamically), or wanted to conditionally extract only a few of the variables, you can use variable variables:
foreach ($_POST as $key => $value) {
if (isset($$key)) continue;
$$key = $value;
}
(The example condition I've used is an overwrite prevention.)
Try not to use extract() when using $_POST. You may overwrite variables you want which will result in unpredictable behaviour. It is a bad habit to get into and while is dynamic may not be the best solution.
You can do something like this:
foreach($_POST as $key => $value)
{
switch($key)
{
case "name":
$name = $value;
break;
case "last":
$last = $value;
break;
}
}
You can actually use the built-in function called extract
Why not use a foreach?
foreach($_POST as $key=>$val){
${$key} = $val;
}
I just want to mention that you can improve foreach technique because we have in HTML the ability to name one of our form fields using square bracket notation, and if we do that, what HTML will do is it will submit a series of values as an array.
<input name="user[name]">
<input name="user[last]">
<input name="user[age]">
<input name="user[sex]">
Than use just one line of code to assign all values from input fields into local array $args.
$args = $_POST['user'];
So what that means is when we go to process the form, we're looking at an array of values instead of a bunch of single values that we have to go retrieve.
So we can just simply go to the post super global and ask for one item: that is user. And what we get back is an associative array. So instead of having to build up that array by going and retrieving all those values one at a time, we're instantly given an array, just because we named our form fields differently.