Check for multiple null isset values - php

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.

Related

How to treat zero values as true using php?

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

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

Compare integers and strings PHP

Need to capture two variables via POST.
If they are different integers, save $mensalidade = 4 and a integer and any other state save $mensalidade=8.
I try this but dont work..
if (is_int($_POST['linha_ida']) != is_int($_POST['linha_volta'])) {
$mensalidade= $_POST['mensalidade']=4;
} else {
$mensalidade= $_POST['mensalidade']=8;
}
Broke a little more head and now it's perfect!
Thanks to all
The code looked like this
if($linha_ida === $linha_volta || preg_match( '/[A-Z]/' , $linha_volta )|| preg_match( '/[A-Z]/' , $linha_ida )){
$mensalidade= $_POST['mensalidade']=8;
} else{
$mensalidade= $_POST['mensalidade']=4;
}
Just use the (int) parser to convert the string to an integer.
if ((int)$_POST['linha_ida']) != (int)($_POST['linha_volta']) && is_int($_POST['linha_ida']) && is_int($_POST['linha_volta']) {
$mensalidade= $_POST['mensalidade']=4;
} else {
$mensalidade= $_POST['mensalidade']=8;
}
Taken from the Question comments from caCtus
caCtus:
is_int() returns true or false if the parameter is an integer or not. If you compare is_int($iamaninteger) to is_int($iamanintegertoo), you compare true and true, not your variables' values.
if (is_int($_POST['linha_ida']) && is_int($_POST['linha_volta']) && $_POST['linha_volta'] != $_POST['linha_ida']) {
$mensalidade= $_POST['mensalidade']=4;
} else {
$mensalidade= $_POST['mensalidade']=8;
}

php wont check the second argument even if the first is true

I wrote this:
$a[] = "guy";
$b[] = "g";
function login($a1, $b1)
{
if( user($a1) == true and pass1($b1) == true)
{
login2($a1, $b1);
}
else
{
echo "error!!!!";
}
}
function login2($a1, $b1)
{
if (array_search($_REQUEST["user"],$a1) == array_search($_REQUEST["pass"],$b1))
{
echo "you are logged in";
}
else
{
echo "erorr";
}
}
function user($user1)
{
if(in_array($_REQUEST["user"],$user1))
{
echo "gooooooood?";
}
}
function pass1($pas)
{
if(in_array($_REQUEST["pass"],$pas))
{
echo "goooooooood!!!!!!!!";
}
else
{
echo "bad";
}
}
login($a, $b);
and I know that pass() and user() are true because I changed their positions on the function login() and every time I did this the first argument was returned as true and it didn't check the second one. Does anyone know why this happens?
user and pass1 functions should return true or false, not echo out.
Your user and pass1 functions are not returning an explicit value, so they are implicitly returning the NULL value. As described on this page in the PHP manual the NULL type is converted to false when a boolean is expected. So both your user and pass1 functions return false every time.
The && and and logical operators in PHP use short-circuiting for efficiency (see the first code example on this page of the PHP manual) so in any and statement whose first operand evaluates to false it can never be possible for the whole and statement to evaluate to true, so the second operand (in the case of your code above, the second operand is the call to pass1($b1)) is never evaluated because it would be a waste of time to do so.
Which means you're seeing the user function being called, but never the pass1 function.
Try using this instead:
$a[] = "guy";
$b[] = "g";
function login($a1, $b1)
{
if( user($a1) == true && pass1($b1) == true)
login2($a1, $b1);
else
echo "error!!!!";
}
function login2($a1, $b1)
{
if (array_search($_REQUEST["user"],$a1) == array_search($_REQUEST["pass"],$b1))
echo "you are logged in";
else
echo "erorr";
}
function user($user1)
{
if(in_array($_REQUEST["user"],$user1))
echo "gooooooood?";
}
function pass1($pas)
{
if(in_array($_REQUEST["pass"],$pas))
echo"goooooooood!!!!!!!!";
else
echo "bad";
}
login($a, $b);

checking if condition for 0 value

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

Categories