Folks,
PHP.
I am trying to check if the $_GET['page_no'] is a number or not.
I know anything inside a single quote is a string.
Eg.
echo 'string';
I know variables should not be inside single quotes. Can be inside double quotes.
Eg.
echo "$var";
I know you assign values to variables with a single equal sign.
Eg.
$var=1;
I know the double equal sign means "equal to".
Eg.
if(1==1)
Bearing all these in mind of what I know about PHP, I still have the following questions that relate to single and double quotes. Single and double equal signs.
I would appreciate if you can number your answers, matching my question numbers. It would be easier for me to understand your answers.
On all my following examples, check what comes after the '&&'.
The differences of each example, is after the '&&'.
And, out of the following 10 examples, can you note the valid ones and rank them according to your preferences where the top best is on the top and least best on the bottom. You understand what I mean. From your rankings, I should give heed to the most accurate examples.
Thanks
Example 1
<?php
if(ISSET($_GET['page_no']) && ($_GET['page_no'] == ""))
{
echo '1a. Page Number
Missing!';
}
QUESTION 1:
I can understand why the above Example 1 is working when I check for blank space in the $_GET['page_no'] with "double quotes".
But why does the following work when I check with 'single quotes' since I am not checking whether the value is blank string or not.
Remember, I not checking for string but a number.
I thought, in PHP, anything inside a single quote is a string.
Example 2
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) && ($_GET['page_no'] == ''))
{
echo '1b. Page Number Missing!';
}
QUESTION 2:
What is wrong if I check with a single equal sign instead of the double equal sign ?
Are not the following 2 also valid ways of checking whether the value is a number or not since it seems from Example 2 that, we can check for a number value with single quotes also ?
EXAMPLE 3
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) && ($_GET['page_no'] = ""))
{
echo '1a. Page Number Missing!';
}
EXAMPLE 4
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) && ($_GET['page_no'] = ''))
{
echo '1a. Page Number Missing!';
}
QUESTION 3:
Out of the following 3 examples, which is preferred over the other and why ?
EXAMPLE 5
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) &&(empty($_GET['page_no'])))
{
echo '1a. Page Number Missing!';
}
EXAMPLE 6
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) && ($_GET['page_no'] == ""))
{
echo '1a. Page Number Missing!';
}
EXAMPLE 7
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) && ($_GET['page_no'] == "NULL"))
{
echo '1a. Page Number Missing!';
}
QUESTION 4:
The value that is supposed to be a number, can we check if it is empty or not by using the NULL ?
If so, then can we check the NULL bit with both the single equal sign aswell as the double equal sign ? And check with single quote aswell as the double quote ?
For example, which of these are invalid and why ?
EXAMPLE 7
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) && ($_GET['page_no'] == "NULL"))
{
echo '1a. Page Number Missing!';
}
EXAMPLE 8
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) && ($_GET['page_no'] == 'NULL'))
{
echo '1a. Page Number Missing!';
}
EXAMPLE 9
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) && ($_GET['page_no'] = "NULL"))
{
echo '1a. Page Number Missing!';
}
EXAMPLE 10
<?php
//Check if Page Number is missing or not.
if(ISSET($_GET['page_no']) && ($_GET['page_no'] = 'NULL'))
{
echo '1a. Page Number Missing!';
}