I have a test.php and i have the below code
<?php
if(isset($_GET['p']) or $_GET['p'] != null) {
echo $_GET['p'];
} else {
echo "Not found";
}
?>
I have listed out below urls then required output are show
Test 1 : http://localhost/example/test.php
output : Notice: Undefined index: p in R:\xampp\htdocs\example\test.php on line 3
Not found
Test 2 : http://localhost/example/test.php?p
output : blank page
Test 3 : http://localhost/example/test.php?p=
output : blank page
Test 4 : http://localhost/example/test.php?p=1
output : 1
I accept that Test 1 and Test 2 are true
But when Test 2 and Test 3 fails out the solution.
<?php
if(!empty($_GET['p']))
echo $_GET['p'];
else
echo "Not found";
?>
if(isset($_GET['p']) and $_GET['p'] != null) {
echo $_GET['p'];
} else {
echo "Not found";
}
you need and as you want to check that it is set and is not null.
Like others have pointed out - you could use empty() :
if(!empty($_GET['p'])) {
echo $_GET['p'];
} else {
echo "Not found";
}
This will return true when the value is empty, the following is considered empty :
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
<?php
if(isset($_GET['p']) and $_GET['p'] != null) {
echo $_GET['p'];
} else {
echo "Not found";
}
?>
The above code you mentioned as or so it will take any one of the following as true
try with and
<?php
if(isset($_GET['p']) and $_GET['p'] != null) {
echo $_GET['p'];
} else {
echo "Not found";
}
?>
And it will work perfect.
You are using an OR, so the second test will ALWAYS run. Meaning, if it's not set it will still try access it. You can use an AND instead (&&) so it will only check the value if it exists.
You should use empty() instead of isset().
<?php
if(empty($_GET['p'])) {
echo "Not found";
} else {
echo $_GET['p'];
}
Related
Here is my sample code:
$issue_id = $_POST['issue_id'];
if(!empty($issue_id)){
echo 'true';
}
else{
echo 'false';
}
If I pass 0 to $_POST['issue_id'] by form submitting then it echo false. Which I want is: Condition will be true if the following conditions are fulfilled:
1. true when I pass any value having 0.
2. false when I don't pass any value. i.e: $_POST['issue_id'] is undefined.
I also tried this:
if(!isset($issue_id)){
echo 'true';
}
else{
echo 'false';
}
if(!empty($issue_id) || $issue==0){
echo 'true';
}
else{
echo 'false';
}
The last one is okay, meaning if I pass any value having ZERO then it will echo true. But it will also echo true if I don't pass any value. Any idea?
The last is okay, meaning if I pass any value having ZERO then it echo true. But it also echo true if I don't pass any value. Any idea?
if (isset($_POST["issue_id"]) && $_POST["issue_id"] !== "") {
}
please notice I used !== not !=. this is why:
0 == "" // true
0 === "" // false
See more at http://php.net/manual/en/language.operators.comparison.php
also if you are expecting number you can use
if (isset($_POST["issue_id"]) && is_numeric($_POST["issue_id"])) {
}
since is_numeric("") returns false
http://php.net/manual/en/function.is-numeric.php
Alternatively if you expect number good option is filter_var
if (isset($_POST["issue_id"]) {
$issue_id = filter_var($_POST["issue_id"], FILTER_VALIDATE_INT);
if ($issue_id !== false) {
}
}
since filter_var("", FILTER_VALIDATE_INT) will returns false and filter_var("0", FILTER_VALIDATE_INT) will return (int) 0
http://php.net/manual/en/function.filter-var.php
if(isset($_POST['issue_id'])) {
if($_POST['issue_id'] == 0) {
echo "true";
}
else {
echo "false";
}
}
When you get data from a form, remember:
All text boxes, whether input or textarea will come as strings. That includes empty text boxes, and text boxes which contain numbers.
All selected buttons will have a value, but buttons which are not selected will not be present at all. This includes radio buttons, check boxes and actual buttons.
This means that $_POST['issue_id'] will be the string '0', which is actually truthy.
If you need it to be an integer, use something like: $issue_id=intval($_POST['issue_id']);
#Abdus Sattar Bhuiyan you can also full fill your two condition like below one:
<?php
$_POST["issue_id"] = "0";
$issue_id = isset($_POST['issue_id']) ? (!empty($_POST['issue_id']) || $_POST['issue_id'] === 0 || $_POST['issue_id'] === "0") ? true : false : false;
if($issue_id){
echo 'true';
}
else{
echo 'false';
}
When I am displaying my array by using var_dump I get the following result:
array(1) { [0]=> NULL }
I want to apply a condition that when my array has a null value it should do something. I have tried using array[0]== NULL and array[0]= NULL inside my condition but it does not work. Can anyone tell me what could be the correct condition for it?
PHPs empty() checks if a variable doesn't exist or has a falsey value (like array(), 0, null, false, etc).
<?php
if (!empty($array[0])) {
echo "Not empty";
} else {
echo "empty";
}
?>
or by using is_null
<?php
if(is_null($array[0])) {
echo "empty";
} else {
echo "not empty";
}
?>
or
<?php
if($array[0] === NULL) {
echo "empty";
} else {
echo "not empty";
}
?>
You can do it by several ways:
if(is_null($array[0])) {}
or
if(!isset($array[0])) {}
or
if($array[0] === null) {}
By the way, == makes a comparison, = is an assignment (even in an if statement) and === compares values and type.
$arr = array();
if (!empty($arr)){
//do your code
}
else {
echo "Hey I'm empty";
}
I'm trying to pull in the parameter of a URL and use that to determine what information to display on page, but for some reason the information is being read wrong. The first thing I do is check for the parameter below and assign it to $page
<?php
if(isset($_GET["page"])) {
$page=$_GET["page"];
}
?>
I then check if the $page is equal to 2 or 3. For some reason, if I echo out $page, I get the proper value of the parameter but it displays incorrect info.
<?php
if(isset($page) == '2') { ?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) == '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
For some reason, even though $page returns 3, I receive INFO A that's supposed to be displayed on page 2. Am I pulling the parameter wrong? The URL Looks like this:
feed.php?page=3
php isset function return Boolean.
You should change code to:
<?php
if(isset($page) && $page== '2') {
?>
DISPLAY INFO A
ECHO $PAGE RETURNS 2
<?php } elseif(isset($page) && $page== '3') { ?>
DISPLAY INFO B
ECHO $PAGE RETURNS 3
<?php } else { something here } ?>
This is wrong:
if(isset($page) == '2') {
It should be
if( $page == '2') {
This will only seem to work on page 1, because isset($page) returns true, which truthy gets converted to 1. The isset() function is only to check if the variable has been set or not
if($page == '3')
Why the isset()? You already do that when you assign it. Maybe this as well:
if(isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = '0'; // or something
}
isset() return a TRUE/FALSE, yet you're comparing it against normal integers. Boolean TRUE in mysql is equivalent to integer 1, but will fail the rest of your tests. You need to have:
if (isset($_GET['page'])) {
if ($_GET['page'] == 1) { ... 1 stuff }
else if ($_GET['page'] == 2) { ... 2 stuff }
}
...and I'm checking this field like so:
if((bool)$website['IsDeleted']) { }
but it ALWAYS returns an empty string regardless of the value in the MySQL field wether its 0 or 1:
["IsDeleted"]=> string(1) "" }
Please tell me what am I doing here? Should the if condition be modified?
I can't see anything wrong with your if-statement.
<?php
$website['IsDeleted'] = 1;
if((bool)$website['IsDeleted']) { echo 'found you';}
?>
would output
found you
<?php
$website['IsDeleted'] = 1;
if((bool)$website['IsDeleted']) { echo $website['IsDeleted'];}
?>
would output 1
If your value is NULL, it won't work though. Then you will have to check NULL-value as well.
<?php
$website['IsDeleted'] = null;
if((bool)$website['IsDeleted'] || $website['IsDeleted'] === null) { echo 'found you';}
?>
would output found you.
in if condition i want to check
if(isset($_GET['q']))
{
echo "ok";
}
esle
{
echo "not ok";
}
when $_GET['q']=0 if send me in else part.
But i want to go in if .
if $_GET['q'] have any value even for 0 if should print ok
any help pls ?
This is what isset does. Try:
$x["q"] = 0;
var_dump(isset($x["q"]));
You will get true. If you think isset() returns false on 0 you are looking at the wrong place, look for a bug elsewhere.
0 is not null http://php.net/manual/en/function.isset.php
You might need something like this considering the value you want is integer
if(isset($_GET['q']) && intval($_GET['q']) > 0 )
{
echo "ok";
}
else
{
echo "not ok";
}
perhaps array_key_exists() would be more appropriate.
if ( array_key_exists( 'q', $_GET ) ) ...
I think this is the correct one...
if(array_key_exists('q', $_GET)){
echo "ok";
}
else{
echo "not ok";
}