Accessing Data in Query Strings in PHP

Accessing Data in Query Strings in PHP To access the field names and values in a query string you simply use the $_GET superglobal array function.

Syntax

$firstName = $_GET[“firstName”];

Example

Here, we will illustrate an example which displays a sequence of Square numbers.
Save the script as number_squaring.php in the document root folder, and run it in the browser.

Code

<!DOCTYPE html>
<html>
<head>
<title> Number squaring </title>
<link rel=”stylesheet” type=”text/css” href=”common.css” />
<style type=”text/css”>
th { text-align: left; background-color: #999; }
th, td { padding: 0.4em; }
tr.alt td { background: #ddd; }
</style>
</head>
<body>
<?php
define( ‘PAGE_SIZE’, 10 );
$start = 0;
if ( isset( $_GET[‘start’] ) and $_GET[‘start’] >= 0 and $_GET[‘start’] <= 1000000 ) {
$start = (int) $_GET[‘start’];
}
$end = $start + PAGE_SIZE – 1;
?>
<h2> Number squaring </h2>
<p> Displaying the squares of the numbers <?php echo $start ?> to 7lt?php echo $end?> </p7gt
<table cellspacing=”0” border=”0” style=”width: 20em; border: 1px solid #666;”>
<tr>
<th> n </th>
<th> n <sup> 2 </sup> </th>
</tr>
<?php
for ( $i=$start; $i <= $end; $i++ )
{
?> <tr <?php if ( $i % 2 != 0 )?> >
<td> <?php echo $i?> </td>
<td> <?php echo pow( $i, 2 )?> </td>
</tr>
<?php
}
?>
</table>
<p>
<?php if ( $start > 0 ) { ?>
<a href=”number_squaring.php?start= <?php echo $start – PAGE_SIZE?> ” > Previous Page </a> |<?php } ?>
<a href=”number_squaring.php?start= <?php echo $start + PAGE_SIZE ?> ” > Next Page > </a>
</p>
</body>
</html>

Output

img

 

Scroll to Top