checking if condition for 0 value - php

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";
}

Related

Check for multiple null isset values

I am checking if two values are null. If both are null I want to return false, if either or both are not null, I want to return true.
My current code returns true only when both are not null but I want it to return true when either or not null.
// check if both null
if (!isset($myarray['dataone'], $myarray['datatwo']))
{
echo 'false';
);
} else {
echo 'true';
);
}
return $emptytabs;
For that you can use relational operators. AND (&&) OR (||)
By using AND (&&) operators.
if ( (!isset($myarray['dataone']) || (!isset$myarray['datatwo'] ))
{
echo 'false';
}
else
{
echo 'true';
}
By using OR ( || ) operators.
if (isset($myarray['dataone'] && isset$myarray['datatwo'])
{
echo 'false';
}
else
{
echo 'true';
}
// check if both null
if ( !isset($myarray['dataone']) && !isset($myarray['datatwo'])) {
echo 'false';
} else {
echo 'true';
}
// check if one or both are null
if ( !isset($myarray['dataone']) || !isset($myarray['datatwo'])) {
echo 'false';
} else {
echo 'true';
}
// check if both null
if ( !isset($myarray['dataone'], $myarray['datatwo']) )
{
echo 'false';
} else {
echo 'true';
}
return $emptytabs;
this approach you provided is totally true , but it only return true if all the provided parameters are set according to php documentations .
so your code should works correctly . except you have unwanted parentheses that should deleted
The simplest way is to use the OR (||) operator. You want to show 'true' if one thing is set OR another thing is set. Just say that with code.
if ( isset($myarray['dataone']) || isset($myarray['datatwo']) ) {
echo 'true';
} else {
echo 'false';
}
Using the AND operator adds pointless complexity by checking that both of the two things are not set in order for it to show 'false'. That's not an intuitive way to think about it, so it doesn't make sense to write the code that way.
DISCLAIMER: This answer is opinionated.

Correct way to check if value exists and is an array with items

So one way to do it would be..
if(isset($arrayVar)) {
if(is_array($arrayVar)) {
if(count($arrayVar) > 0) {
// Success
print_r($arrayVar);
}
}
}
Are there any better ways?
You can do it using is_array and empty:
if (!empty($arrayVar) && is_array($arrayVar)) {
// ...
}
empty() will check if isset and not empty at once.
if (!empty($arrayVar) && is_array($arrayVar))
!empty covers both isset and empty arrays (all falsey values in fact), you then just need to confirm that it's also actually an array.
You can check like this,
if(is_array($arrayVar) && sizeof($arrayVar) > 0)
{
echo 'Array value exists';
}
else
{
echo 'array empty or it is not array';
}

php If statement inside html table wrong output

i need to use if statement inside my table
the value of the row is "Released" but i want to show Pending on my html table. how can i achieve it. this is my current code. what's wrong with my if statement?
if (strlen($row['signed']) == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen checks for string length. first check either signed is set in the array and then check if value is equal
if (isset($row['signed']) && $row['signed'] == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen() returns the length of the argument, so it returns an integer. You can check if value is equals to the string which you want something like this:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo "Released";
}
strlen() is used to count the length of the string, to check if it matches "Released", just use == to compare:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo $row['signed'];
}
To find out is $row['signed'] is set, just use isset():
if (isset($row['signed'])) {
echo "Pending";
} else {
echo $row['signed'];
}
More information on isset(): http://php.net/manual/en/function.isset.php
More information on PHP operators: http://php.net/manual/en/language.operators.php
Try this:
if ($row['signed'] == "Released") {
$value = "Pending";
} else {
$value = "Released"
}
And add <?php echo $value; ?> in your table

Condition for null value in an array in php

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";
}

$_GET shows empty value

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'];
}

Categories