Home »
PHP »
PHP find output programs
PHP find output programs (Filters) | set 2
Find the output of PHP programs | Filters | Set 2: Enhance the knowledge of PHP Filters by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 26, 2021
Question 1:
<?php
$IP = "2021:0abd:85c4:09d2:2219:3b2e:4e70:6234";
$isValid = filter_var($IP, FILTER_VALIDATE_IPV6);
if ($isValid == true) echo "IP address is valid<br>";
else echo "IP address is not valid<br>";
?>
Output:
IP address is not valid
Explanation:
In the above program, we created $IP initialized with a valid IPV6 address. But we did not use filter properly in filter_var() function to validate IPv6 address. The correct way is given below:
$isValid = filter_var($IP, FILTER_VALIDATE_IP,FILTER_VALIDATE_IPV6);
The above function will return true, If $IP contains a valid IPv6 address, otherwise it will return false. In our case we did not use filters properly, then "IP address is not valid" will be printed on the webpage.
Question 2:
<?php
$URL = "https://www.includehelp.com?A=10&B=20";
$isValid = filter_var($URL, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED);
if ($isValid == true) echo ("Given URL is a valid URL with a query string");
else echo ("Given URL is not a valid URL with a query string");
?>
Output:
Given URL is a valid URL with a query string
Explanation:
The above program will print "Given URL is a valid URL with a query string" on the webpage. Here, we created variable $URL and initialized with "https://www.includehelp.com?A=10&B=20", here we also used 'A' and 'B' for query-string in the given URL.
$isValid = filter_var($URL, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED);
The above function will return true, because we used URL with query-string then "Given URL is a valid URL with a query string" will print on the webpage.
Question 3:
<?php
$A = "<h2>";
$B = "IncludeHelp";
$C = "ÆØÅ!";
$D = "<h2>";
$S = $A . $B . $C . $D;
$R_S = filter_var($S, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
echo $R_S;
?>
Output:
IncludeHelp!
Explanation:
In the above program, we created variable $A, $B, $C, and $D and initialized with some values. Then we concatenate all variables and assigned to $S.
$R_S = filter_var($S, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
The above statemment used FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH filters that will remove HTML tags and NON-ASCII characters.Then "IncludeHelp!" will be printed on the webpage
Question 4:
<?php
$val = 222;
$min_range = 100;
$max_range = 300;
$isValid = filter_var($val, FILTER_VALIDATE_INT, array(
"options" => array(
"min_range" => $min_range,
"max_range" => $max_range
)
));
if ($isValid == true) echo ("The val lies in given range");
else echo ("The val does not lie in given range");
?>
Output:
The val lies in given range
Explanation:
In the above program, we created three variables $val, $min_range, and $max_range initialized with 222, 100, and 300 respectively.
$isValid = filter_var($val, FILTER_VALIDATE_INT, array(
"options" => array(
"min_range" => $min_range,
"max_range" => $max_range
)
));
The above function will return true because 222 lying between 100 and 300, then "The val lies in given range" will print on the webpage.
Question 5:
<?php
$val = 222;
$min_range = 100;
$max_range = 300;
$isValid = filter_var($val, FILTER_VALIDATE_INT, array(
"options" => array(
"xxx" => $min_range,
"yyy" => $max_range
)
));
if ($isValid == true) echo ("The val lies in given range");
else echo ("The val does not lie in given range");
?>
Output:
The val lies in given range
Explanation:
In the above program, we created three variables $val, $min_range, and $max_range initialized with 222, 100, and 300 respectively.
$isValid = filter_var($val, FILTER_VALIDATE_INT, array(
"options" => array(
"xxx" => $min_range,
"yyy" => $max_range
)
));
The above function will return true because 222 lying between 100 and 300, then "The val lies in given range" will print on the webpage.