Click here to Skip to main content
Click here to Skip to main content

Code Experiment on $_GET

, 11 Nov 2013
Rate this:
Please Sign up or sign in to vote.
An example to promote code experiments.

Introduction

We know that $_GET is a PHP method for accessing an associative array of values passed to a PHP script via the URL parameters submitted through the GET method. Sometimes, I receive queries from my students on how to check the state of $_GET so as to determine the subsequent course of action to perform in their scripts. As usual, what I do is to guide them through some simple code experiment. In this way, they learn to find answers to their programming problems. I always believe in teaching fishing skills than to give away fish. This article serves as an example.

Background

Software development is an adventure, no one can claim that he knows everything. Very often, beginners or veterans, will encounter problems in the course of development which are to be expected. When these happen, we will explore ways and means to find answers - manuals, documentation, Internet searches, ask CodeProject, etc. We may find different answers, correct or not, clear or vague, to the same problems. To be certain, I always advocate conducting code experiments ourselves to ascertain the different answers. In fact, code experiments can help us understand the answers better than just accepting them indiscriminately. If answers is not readily available, we may discover answers ourselves through code experiments.

Using the Code

This is the second edition. Prompted by the feedback from genom, I researched further and found the following information here which states that:

Under loose comparisons "==":

NULL == "" returns true, which means NULL has been converted to "".

Under strict comparison "===":

NULL === "" returns false, NULL stays as NULL.

With this finding, I have added PHP Identical Operators (=== and !==) and the is_null() function in the source code. The purpose of the is_null() function is to confirm if an object is truly NULL or not. We will compare the outputs between the loose Equal Operators (== and !=) and strict Identical Operators, note especially the null conditions on experiment 2. We will discuss the findings after the experiments.

Unzip the source file and you should get a folder name "code_lab" with a one-page PHP file named "get_state.php". Move the "code_lab" folder to the webroot of your Apache server. The content of the "get_state.php" file is shown below:

<?php
// print the $_GET value in its human readable form
print_r($_GET);
echo "<br />";

// get the total number of $_GET parameters
$numPara = count($_GET);

echo "No of &nbsp\$_GET parameters = $numPara <br />";

// echo the state of the 'first' $_GET parameter
if ($numPara > 0){

	if (isset($_GET["first"])) echo "1. 'first' parameter exists.<br />";
	if (!isset($_GET["first"])) echo "2. 'first' parameter does not exists.<br />";
	if (empty($_GET["first"])) echo "3. 'first' parameter is empty string.<br />";
	if (!empty($_GET["first"])) echo "4. 'first' parameter has non-empty string.<br />";	
		
	// using equal operators of == and !=	
	echo "<h3>Equal Operators: ==, !=</h3>";
	if ($_GET["first"] =="") echo "5. 'first' parameter is empty string.<br />";
	if ($_GET["first"] != "") echo "6. 'first' parameter has non-empty string.<br />";
	if ($_GET["first"] == null) echo "7. 'first' parameter is null.<br />";
	if ($_GET["first"] != null) echo "8. 'first' parameter is not null.<br />";
	
	// using identical operators of === and !==
	echo "<h3>Identical Operators: ===, !==</h3>";
	if ($_GET["first"] ==="") echo "9. 'first' parameter is empty string.<br />";
	if ($_GET["first"] !== "") echo "10. 'first' parameter has non-empty string.<br />";
	if ($_GET["first"] === null) echo "11. 'first' parameter is null.<br />";
	if ($_GET["first"] !== null) echo "12. 'first' parameter is not null.<br />";
	
	// added is_null()function
	echo "<h3>Using is_null() function</h3>";
	if (is_null($_GET["first"])) echo "13. 'first' parameter is null.<br />";
	if (!is_null($_GET["first"])) echo "14. 'first' parameter is not null.<br />";	
	
}
?>

Next, start your Apache server and carry out the following experiments:

  1. Enter "http://localhost/code_lab/get_state.php" into the URL bar of your favorite browser, you should see the following output on your browser:
    Array ( ) 
    No of  $_GET parameters = 0 
  2. Enter "http://localhost/code_lab/get_state.php?first" or "http://localhost/code_lab/get_state.php?first= " will both produce the following output:
    Array ( [first] => ) 
    No of  $_GET parameters = 1 
    1. 'first' parameter exists.
    3. 'first' parameter is empty string.

    Equal Operators: ==, !=

    5. 'first' parameter is empty string.
    7. 'first' parameter is null.

    Identical Operators: ===, !==

    9. 'first' parameter is empty string.
    12. 'first' parameter is not null.

    Using is_null() function

    14. 'first' parameter is not null.   
  3. Enter "http://localhost/code_lab/get_state.php?first=peter" will produce the following output:
    Array ( [first] => peter ) 
    No of  $_GET parameters = 1 
    1. 'first' parameter exists.
    4. 'first' parameter has non-empty string.

    Equal Operators: ==, !=

    6. 'first' parameter has non-empty string.
    8. 'first' parameter is not null.

    Identical Operators: ===, !==

    10. 'first' parameter has non-empty string.
    12. 'first' parameter is not null.

    Using is_null() function

    14. 'first' parameter is not null.
  4. Lastly, "http://localhost/code_lab/get_state.php?first=peter&second=leow" will produce the following output:
    Array ( [first] => peter [second] => leow ) 
    No of  $_GET parameters = 2 
    1. 'first' parameter exists.
    4. 'first' parameter has non-empty string.

    Equal Operators: ==, !=

    6. 'first' parameter has non-empty string.
    8. 'first' parameter is not null.

    Identical Operators: ===, !==

    10. 'first' parameter has non-empty string.
    12. 'first' parameter is not null.

    Using is_null() function

    14. 'first' parameter is not null.

Points of Interest

By varying the URL parameters in the above experiments, we are able to find out the different states returned by the $_GET method. In particular, Experiment 2 has provided us the following findings:

  • Finding 1: Outputs 9, 12 and 14 of Experiment 2 have proved that $_GET URL parameter with no value, e.g. "http://localhost/code_lab/get_state.php?first" or "http://localhost/code_lab/get_state.php?first= " is being treated as empty string and not null as confirmed by the is_null() function.
  • Finding 2: Finding 1 has indirectly proved that NULL is being converted to empty string under loose comparison as shown in outputs 5 and 7 of Experiment 2.

Based on Findings 1 and 2, we can conclude that strict comparison operators, i.e. '===' and '!==' should be used when one of the operands involves NULL value.

This article demonstrates an example of code experiment. I hope this article will promote code experiments among the community and encourage everyone to share your findings and experience here.

History

  • Second Edition: 08 Nov 2013, added more experiments and included two findings and one conclusion. Thanks to genom for his valuable feedback.
  • First Edition: 04 Nov 2013.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Peter Leow
Instructor / Trainer
Singapore Singapore
Graduated from the National University of Singapore with the following qualifications:
1. Graduate Diploma in Systems Analysis (2002)
2. Master of Technology (Knowledge Engineering) (2013)
 
Currently, lecturing in the area of social media and web development at a technical institution.
 
Having hibernated for ages, finally woke up to serve the Community in Oct 2013.
Follow on   LinkedIn

Comments and Discussions

 
GeneralNice Read and Impressive Idea Pinmemberkarthy Udhaykumar24-May-14 18:22 
Questionparameter always exists Pinmembergenom11-Nov-13 23:16 
QuestionFirst Edition: 04 November 2014?!? PinmemberErik Anderson6-Nov-13 12:58 
AnswerRe: First Edition: 04 November 2014?!? PinprofessionalPeter Leow6-Nov-13 15:20 
Questioncomparing with NULL Pinmembergenom5-Nov-13 19:59 
AnswerRe: comparing with NULL [modified] PinprofessionalPeter Leow5-Nov-13 21:17 
Suggestionextended code PinmemberErnst Bolt4-Nov-13 9:23 
GeneralRe: extended code [modified] PinprofessionalPeter Leow4-Nov-13 17:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 11 Nov 2013
Article Copyright 2013 by Peter Leow
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid