Built-in PHP Arrays
Built-in PHP Arrays
built-in arrays
PHP has several built-in arrays that you can use when writing PHP scripts. Different types of information are stored in different arrays. For example, information about your server (such as headers, paths, and script locations) is stored in an array called $_SERVER. When you want to display the name of the current script that is running, it’s available in the $_SERVER built-in array in $_SERVER[‘PHP_SELF’].
Using superglobal arrays
Currently, two sets of built-in arrays contain the same information. One set of arrays, introduced in PHP 4.1.0, are called superglobals or autoglobals because they can be used anywhere, even inside a function. (Functions and the use of variables inside functions are explained in Chapter 8.) The older arrays, with long names such as $HTTP_SERVER_VARS, must be made global before they can be used in an array, as explained in Chapter 8. Unless you’re using an old version of PHP, use the newer arrays, those whose names begin with an underscore (_). The older arrays should be used only when you’re forced to use a version of PHP older than PHP 4.1.0.
A new php.ini setting introduced in PHP 5 allows you to prevent PHP from automatically creating the older, long arrays. It’s very unlikely that you will need to use them, unless you’re using some old scripts containing the long variables. The following line in php.ini controls this setting:
register_long_arrays = On
Handy Built-in Arrays
$GLOBALS Contains all the global variables. For example, if you use the statement, $testvar = 1, you can then access the variable as $GLOBALS[‘testvar’].
$ _POST Contains all the variables contained in a
form if the form uses method=”post”.
$HTTP_POST_VARS Same as $ _POST.
$ _GET Contains all the variables passed from a previous page as part of the URL. This includes variables passed in a form using method=”get”.
$HTTP_GET_VARS Same as $ _GET.
$ _COOKIE Contains all the cookie variables.
$HTTP_COOKIE_VARS Same as $ _COOKIE.
$ _SESSION Contains all the session variables.
$HTTP_SESSION_VARS Same as $ _SESSION.
$ _SESSION Contains all the session variables.
$HTTP_SESSION_VARS Same as $ _SESSION.
$_FILES Contains the names of files that have been uploaded.
$HTTP_FILES_VARS Same as $_FILES.
$_SERVER Contains information about your server. Because your Web server provides the information, the information that’s available depends on what server you’re using.
$HTTP_SERVER_VARS Same as $_SERVER.
$_ENV Contains information provided by your operating system, such as the operating system name, the system drive, and the path to your temp directory. This info varies depending on your
operating system.
$HTTP_ENV_VARS Same as $_ENV.
$_SERVER and $_ENV
The $_SERVER and $_ENV arrays contain different information, depending on the server and operating system you’re using. You can see what information is in the arrays for your particular server and operating system by using the following statements:
foreach($_SERVER as $key =>$value) { echo “Key=$key, Value=$value\n”; }
phpinfo()
PHPinfo is a function that returns information, in HTML form, about the PHP environment on your server. To run PHPinfo you must save the following code in a file on your computer using your text editor:
<?php phpinfo (); ?>
cookies
You can store information as cookies, which are small amounts of information containing variable=value pairs, similar to the pairs you can add to a URL. The user’s browser stores cookies on the user’s computer. Your scripts can then use the cookie information.Unless you know for sure that all your users will have cookies turned on or you can request that they turn on cookies and expect them to follow your request, cookies are a problem. If your application depends on cookies, it won’t run if cookies are turned off. Storing and retrieving information in cookies You store cookies by using the setcookie function.
The general format is as follows:
setcookie(“variable”,”value”); setcookie(“state”,”UP”);
Storing and retrieving information in cookies when the user moves to the next page, the cookie information is available in the built-in array called $_COOKIE. The next Web page can display the information from the cookie by using the following statement.
echo “Your home state is “.$_COOKIE[‘state’];
The output from this statement is as follows:
Your home state is UP
Setting expiration dates
If you want the information stored in a cookie to remain in a file on the user’s computer after the user leaves your Web site, set your cookie with an expiration time, as follows:
setcookie(“variable”,”value”,expiretime);
The expiretime value sets the time when the cookie expires. The value for expiretime is usually set by using either the time or mktime function Setting expiration dates
time:
This function returns the current time in a format the computer can understand. You use the time function plus a number of seconds to set the expiration time of the cookie, as shown in the following
statements:
setcookie(“state”,”CA”,time()+3600); #expires in one hour setcookie(“Name”,$Name,time()+(3*86400)) #expires 3 days
mktime:
This function returns a date and time in a format that the computer can understand. You must provide the desired date and time in the following order: hour, minute, second, month, day, and year. If any
value is not included, the current value is used. You use the mktime function to set the expiration time of the cookie, as shown in the following statements:
setcookie(“state”,”CA”,mktime(3,0,0,6,1,2010)); #expires at 3:00 AM on june 1, 2010
removing a cookie
You can remove a cookie by setting its value to nothing. Either of the following statements removes the cookie:
setcookie(“name”); setcookie(“name”,””);
Comments (0)
Leave a comment
Related Blogs
Introduction to Kibana
Aug 1, 2020, 6:19:45 PM | Anurag Srivastava
Bucket Aggregation in Elasticsearch
Aug 29, 2018, 7:15:06 PM | Anurag Srivastava
Metrics Aggregations in Elasticsearch
Aug 18, 2018, 6:02:20 PM | Anurag Srivastava
Introduction to Elasticsearch Aggregations
Aug 14, 2018, 4:47:56 PM | Anurag Srivastava
Wildcard and Boolean Search in Elasticsearch
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Basics of Data Search in Elasticsearch
Aug 4, 2018, 7:02:21 AM | Anurag Srivastava
Top Blogs
Wildcard and Boolean Search in Elasticsearch
Aug 10, 2018, 7:14:40 PM | Anurag Srivastava
Elasticsearch REST APIs
Jul 31, 2018, 6:16:42 PM | Anurag Srivastava
How to count number of words in a HTML string and find Read time in Python 3
Jun 30, 2018, 12:07:47 PM | jitender yadav
Create a Chess board in PHP
Mar 9, 2020, 8:45:41 AM | Rocky Paul
Bucket Aggregation in Elasticsearch
Aug 29, 2018, 7:15:06 PM | Anurag Srivastava
Metrics Aggregations in Elasticsearch
Aug 18, 2018, 6:02:20 PM | Anurag Srivastava