How to Check If a Get Variable Is Set in PHP
Variables are objects in PHP that allow you to hold values for your Web application. The "null" value means the variable's value is not set. If you have a variable that is passed from another page, you use the "Get" function to extract the information from the page. For instance, when a website form has a first and last name, users type the information and you use "Get" to use the information on the processing page. PHP has an "is_null" function that checks if the variable has a value.
Instructions
-
-
1
Right-click the PHP file you want to edit and select "Open With." Double-click your PHP editor. If you do not have a PHP editor, you can use Windows Notepad to edit PHP.
-
2
Scroll down to the section of your code where you want to check your variable. If you do not have a variable created, type the following code:
$var = $_GET["first_name"];;
This is an example of a variable set to the "Null" value, if the user does not fill out the text box labeled "first_name."
-
-
3
Use the "is_null" function to check the variable's value. For instance, the following code checks the variable created in step 3. The function returns "true" to indicate that the variable is set to null.
$variable_set = is_null($var);
Replace "$var" with your own variable. If the variable is set, the function returns "false."
-
4
Press "Ctrl+S" to save your changes. Use your PHP editor to view the changes, or open the PHP file in a browser to view the changes.
-
1