The $_GET Variable

The $_GET variable is an array of variable names and values sent by the HTTP GET method.

The $_GET variable is used to collect values from a form with method="get". Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and it has limits on the amount of information to send (max. 100 characters).

Example

<form action="welcome.php" method="get">

Name: <input type="text" name="name" />

Age: <input type="text" name="age" />

<input type="submit" />

</form> When the user clicks the "Submit" button, the URL sent could look something like this:

http://www.w3schools.com/welcome.php?name=Peter&age=37 The "welcome.php" file can now use the $_GET variable to catch the form data (notice that the names of the form fields will automatically be the ID keys in the $_GET array):

Welcome <?php echo $_GET["name"]; ?>.

You are <?php echo $_GET["age"]; ?> years old!

Why use $_GET?

Note: When using the $_GET variable all variable names and values are displayed in the URL. So this method should not be used when sending passwords or other sensitive information! However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

Note: The HTTP GET method is not suitable on large variable values; the value cannot exceed 100 characters.

The $_REQUEST Variable

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

Example

Welcome <?php echo $_REQUEST["name"]; ?>.

You are <?php echo $_REQUEST["age"]; ?> years old! The $_POST Variable

The $_POST variable is an array of variable names and values sent by the HTTP POST method.

The $_POST variable is used to collect values from a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Example

<form action="welcome.php" method="post">

Enter your name: <input type="text" name="name" />

Enter your age: <input type="text" name="age" />

<input type="submit" />

</form> When the user clicks the "Submit" button, the URL will not contain any form data, and will look something like this:

http://www.w3schools.com/welcome.php The "welcome.php" file can now use the $_POST variable to catch the form data (notice that the names of the form fields will automatically be the ID keys in the $_POST array):

Welcome <?php echo $_POST["name"]; ?>.

You are <?php echo $_POST["age"]; ?> years old!

Why use $_POST?

Variables sent with HTTP POST are not shown in the URL

Variables have no length limit

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

The $_REQUEST Variable

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.

Example

Welcome <?php echo $_REQUEST["name"]; ?>.

You are <?php echo $_REQUEST["age"]; ?> years old! The PHP date() function is used to format a time or a date.

The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.

Syntax

date(format,timestamp)

Parameter Description format Required. Specifies the format of the timestamp timestamp Optional. Specifies a timestamp. Default is the current date and time (as a timestamp)

PHP Date - What is a Timestamp?

A timestamp is the number of seconds since January 1, 1970 at 00:00:00 GMT. This is also known as the Unix Timestamp.

PHP Date - Format the Date

The first parameter in the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Here are some of the letters that can be used:

d - The day of the month (01-31)

m - The current month, as a number (01-12)

Y - The current year in four digits

An overview of all the letters that can be used in the format parameter, can be found in our HYPERLINK "C:\Inetpub\wwwroot\w3schools\php\php_ref_date.asp" PHP Date reference.

Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:

<?php

echo date("Y/m/d");

echo "

";

echo date("Y.m.d");

echo "

";

echo date("Y-m-d");

?> The output of the code above could be something like this:

2006/07/11

2006.07.11

2006-07-11

PHP Date - Adding a Timestamp

The second parameter in the date() function specifies a timestamp. This parameter is optional. If you do not supply a timestamp, the current time will be used.

In our next example we will use the mktime() function to create a timestamp for tomorrow.

The mktime() function returns the Unix timestamp for a specified date.

Syntax

mktime(hour,minute,second,month,day,year,is_dst) To go one day in the future we simply add one to the day argument of mktime():

<?php

$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));

echo "Tomorrow is ".date("Y/m/d/", $tomorrow);

?> The output of the code above could be something like this:

Tomorrow is 2006/07/12

Vinay Malik

PAGE \* MERGEFORMAT 11

You've reached the end of published parts.

⏰ Last updated: Jan 14, 2010 ⏰

Add this story to your Library to get notified about new parts!

PHP-languageWhere stories live. Discover now