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
Related
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']);
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.
We always retrieve the value in a $_GET['var'], but is it possible to assign a value as well? I have a php page where at one point, through ajax, I want to stay on the same page but change the query string or do an assignment like this:
$_GET['myvar'] = 'newvalue';
Is this possible?
Yes you can override the $_GET. however that is only for that request.
with ajax you do a new request and in the ajax call you can just use diffrent values for the data.
Yes, you can assign to the $_GET array, but it won't change the query string in the URL.
It's probably not the wisest thing to do though, as it will be overwritten in the next request
$_GET is just like a regular array. The only difference is that the keys of this array will be automatically populated with values that come with the request using HTTP GET. They are called superglobals because they are automatically and globally available anywhere in your script, other wise they behave just like regular arrays.
So if you have a request like mypage.php?key=value, PHP automatically does something equal to this for you:
$_GET['key'] = 'value';
And just like any regular array, you can overwrite it with a different value. However I really do not see a use case for that unless you are doing some testing or some really weird thingy..
I have a bunch of methods of sorting date=desc,etc,etc and they are posted via a $_GET I want to take all the $_GET variables ($_GET['anythingatall']) and transform them from $_GET['variable]=blah to &variable=blah
Is there a simple way to do this?
You are interested in $_SERVER['QUERY_STRING'], I think. This will contain everything passed in $_GET but in the format you desire.
This might work for you
$string = http_build_query($_GET, null, '&')
Alex's solution should work too, and admittedly cleaner. If you wanted to create a query string from any other array using http_build_query should work fine.
If all you're wanting to do is pass on the existing query string (which is available in $_GET), you can use $_SERVER['QUERY_STRING'], which will be exactly what you're looking for, a query string representation of the $_GET array (assuming you didn't change it)
See the PHP documentation on the $_SERVER superglobal.
you may want to take a look at http://us.php.net/manual/en/function.http-build-query.php