Boolean handling in PHP - php

I have following code in my php script:
if ( $x != 'some_val_1' && $y['index']->title != "some_val_2" && $y['index']->title != "some_val_3" ) {
// Do something.
}
In the code shown above, index may/may not be set and thus when it is not set this code throws a php notice.
I changed it to:
if (isset($y['index']->title)) {
if ( $x != 'some_val_1' && $y['index']->title != "some_val_2" && $y['index']->title != "some_val_3" ) {
// Do something.
}
}
However since the "Do something" part is set to work when isset($y['index']->title is not equal to some_val_2 and some_val_3, the above does not work because the isset() condition I added skips the code.
So now the script does not throw notice but at the cost of completely changing the condition to something not desirable.
How can I change this code to not throw the PHP notice?

You can handle it in different ways, I initialize the variables if they are not defined:
if (! isset($y['index']->title)) {
$y['index']->title = '';
}
if ( $x != 'some_val_1' && $y['index']->title != "some_val_2" && $y['index']->title != "some_val_3" ) {
// Do something.
}
I prefer this way because I don't need to change my code and add if conditions (with isset for example) in all my program. I just add the conditions at the top of my code.

Since you are dealing with an array of objects, you can use the function array_key_exists().
Your code will look like this:
if ( array_key_exists('index',$y) && $x != 'some_val_1' && $y['index']->title != 'some_val_2' && $y['index']->title != 'some_val_3' ) {
// Do something.
}
You can read more about it here: http://php.net/manual/en/function.array-key-exists.php

You can try using isset & in_array in this scenario.
if ( $x != 'some_val_1'
&& isset($y['index']->title)
&& !in_array($y['index']->title, array("some_val_2", "some_val_3") ) ) {
// Do something.
}
The benefit of using in_array is that you validate case-sensitivity by passing TRUE / FALSE in the third parameter.

Related

Multiple checkbox validation before sql insert

I have a problem similar to the post:
how to check multiple $_POST variable for existence using isset()?
My issue is, I have a rather large list of checkboxes I need to know if they were checked in order to set the status of $row. it would be exactly like the mentioned script,all and's, except I have and OR thrown in to the works.
if ( isset($_POST['Facial1'])
&& ($_POST['Facial2'])
&& ($_POST['Facial3'])
&& ($_POST['Facial4'])
&& ($_POST['Intra-Oral1'])
&& ($_POST['Intra-Oral2'])
&& ($_POST['Intra-Oral3'])
&& ($_POST['Intra-Oral4'])
&& ($_POST['Intra-Oral5'])
&& ($_POST['Intra-Oral6'])
&& (
(
($_POST['X-Ray1'])
&& ($_POST['X-Ray3'])
)
|| ($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
Each time I run a test, as soon as it gets to an element that is not checked it throws an error: undefined index: xxx. Where xxx is the first none checked item.
Checkboxes that are not checked do not get sent as part of the POST body. Your code needs to use isset:
if ( isset($_POST['Facial1'])
&& isset($_POST['Facial2'])
&& isset($_POST['Facial3'])
&& isset($_POST['Facial4'])
&& isset($_POST['Intra-Oral1'])
&& isset($_POST['Intra-Oral2'])
&& isset($_POST['Intra-Oral3'])
&& isset($_POST['Intra-Oral4'])
&& isset($_POST['Intra-Oral5'])
&& isset($_POST['Intra-Oral6'])
&& (
(
isset($_POST['X-Ray1'])
&& isset($_POST['X-Ray3'])
)
|| isset($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
the isset() allows you to check one variable isset($var) or multiple variables at a time isset($var,$var1,$var2) this will return true is all exists and false if one or more are not set.
Your code is only checking one and then doing well I am not sure what
Try
if ( isset(
$_POST['Facial1'], $_POST['Facial2'], $_POST['Facial3'],
$_POST['Facial4'], $_POST['Intra-Oral1'], $_POST['Intra-Oral2']
$_POST['Intra-Oral3'], $_POST['Intra-Oral4'],
$_POST['Intra-Oral5'], $_POST['Intra-Oral6']
)
&&
(
isset($_POST['X-Ray1'], $_POST['X-Ray3'])
||
isset($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
I hope this is what you intended, its not totally obvious from your code.

PHP IF construct when dealing with a part that is an exception

I'm having some trouble getting my code to work. And I lost my overview. Who can give me some insight?
I'm looking for how to build an IF construct.
If I would describe the IF construct in words:
MismatchBundle = true unless (adminPass = true & user = admin)
I wrote the following code, but this didn't seem to work:
if (($MismatchBundle == true) && ($adminPass != true && $thisUser['rankid'] != 1)
Anyone who knows how to get the 'unless' part in the code?
What about this?
if (($MismatchBundle == true) && !($adminPass == true && $thisUser['rankid'] == 1)
Is this allowed with the ! before a () section?
if (($MismatchBundle == true) && !($adminPass == true && $thisUser['rankid'] == 1)
Using ! on the entire section within ( ) worked as intended.

If either of two cookies exist do something, in PHP

I know that || or && need to be used but I can't work out the correct or best way to format this.
My code for one cookie:
if(isset($_COOKIE['mycookie'])) {
if($_COOKIE['mycookie']=="value1") {
// do some stuff
}
}
But I'd like to include another cookie in this routine where either one can be true for the "stuff" to work.
I'm just not sure how to format this. Is it something like this?
if(isset($_COOKIE['mycookie'] || ['mycookie2')) {
if($_COOKIE['mycookie']=="value1" || $COOKIE['mycookie2']=="value2") {
// do some stuff
}
}
You can write all in one if statement if you want like this:
(The OR statement in the isset() function is not going to work)
if ( (isset($_COOKIE['mycookie']) && $_COOKIE['mycookie'] == "value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") )
You need to do the || outside the function, to combine the results of all the calls.
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
// do some stuff
}
It will be:
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
if ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2") {
// do some stuff
}
}
Or even:
if ((isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2') && ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2")) {
// do some stuff
}
to avoid nested if.
Try this
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1")
|| 9isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] =="value2" )) {
// do some stuff
}
Try this. It puts all requirements in one if statement:
if( (isset($_COOKIE['mycookie'] && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2']=="value2") ) {
// do some stuff
}
You can use one if condition instead of nested if. If you required to validate both then
if(isset($_COOKIE['mycookie'], $_COOKIE['mycookie2']) && ($_COOKIE['mycookie'] == "value1" && $_COOKIE['mycookie2']=="value2")) {
// do some stuff
}
Or if you have to validate one of them then
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") ) {
// do some stuff
}

PHP - Check GET Variables Exist And Not Empty

I want to check the GET variables are not empty, I tried ways but they didn't work.
So I had the code like this:
$u = isset($_GET["u"]);
$p = isset($_GET["p"]);
if ($u !== "" && $p !== "") {
//something
} else {
//do something
}
The I checked the code by sending create.php?u=&p=, but the code didn't work. It kept running the //do something part. The I tried:
echo $u;
echo $p;
It returned 1 and 1. Then I changed it to:
if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "") {
//something
} else {
//do something
}
But it continued to run //do something.
Please help.
You can just use empty which is a PHP function. It will automatically check if it exists and whether there is any data in it:
if(empty($var))
{
// This variable is either not set or has nothing in it.
}
In your case, as you want to check AGAINST it being empty you can use:
if (!empty($u) && !empty($p))
{
// You can continue...
}
Edit: Additionally the comparison !== will check for not equal to AND of the same type. While in this case GET/POST data are strings, so the use is correct (comparing to an empty string), be careful when using this. The normal PHP comparison for not equal to is !=.
Additional Edit: Actually, (amusingly) it is. Had you used a != to do the comparison, it would have worked. As the == and != operators perform a loose comparison, false == "" returns true - hence your if statement code of ($u != "" && $p != "") would have worked the way you expected.
<?php
$var1=false;
$var2="";
$var3=0;
echo ($var1!=$var2)? "Not Equal" : "Equal";
echo ($var1!==$var2)? "Not Equal" : "Equal";
echo ($var1!=$var3)? "Not Equal" : "Equal";
echo ($var1!==$var3)? "Not Equal" : "Equal";
print_r($var1);
print_r($var2);
?>
// Output: Equal
// Output: Not Equal
// Output: Equal
// Output: Not Equal
Final edit: Change your condition in your if statement to:
if ($u != "" && $p != "")
It will work as you expected, it won't be the best way of doing it (nor the shortest) but it will work the way you intended.
Really the Final Edit:
Consider the following:
$u = isset($_GET["u"]); // Assuming GET is set, $u == TRUE
$p = isset($_GET["p"]); // Assuming GET is not set, $p == FALSE
Strict Comparisons:
if ($u !== "")
// (TRUE !== "" - is not met. Strict Comparison used - As expected)
if ($p !== "")
// (FALSE !== "" - is not met. Strict Comparison used - Not as expected)
While the Loose Comparisons:
if ($u != "")
// (TRUE != "" - is not met. Loose Comparison used - As expected)
if ($p != "")
// (FALSE != "" - is met. Loose Comparison used)
You need !empty()
if (!empty($_GET["p"]) && !empty($_GET["u"])) {
//something
} else {
//do something
}
Helpful Link
if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "")
why are you using "!==" and not "!=".
to always simplify your problem solve the logic on paper once using the runtime $u and $p value.
To check if $_GET value is blank or not you can use 2 methods.
since $_GET is an array you can use if(count($_GET)) if you have only u and p to check or check all incoming $_GET parameters.
empty function #Fluffeh referred to.
if($_GET['u']!=""&&$_GET['p']!="")
Hope it helps thx
In you code you should correctly check the variable existence like
if ($u != NULL && $p != NULL && $u != 0 && $p != 0) {
//something
} else {
//do something
}
Wow! I was so dumb... isset returns a boolean. I fixed my problem now. Thank you for answering anyway :)
This fixes:
$u = $_GET["u"];
$p = $_GET["p"];

Using if (!empty) on multiple variables but displaying content if one isn't empty

I'm using the following
if (!empty($data['var_1'])
&& !empty($data['var_2'])
&& !empty($data['var_3'])
&& !empty($data['var_4'])
&& !empty($data['var_5'])
&& !empty($data['var_6'])
&& !empty($data['var_7'])
&& !empty($data['var_8'])
&& !empty($data['var_9'])) {
//BLOCK HERE
}
Basically, what I'm trying to achieve is if all of the variables are empty, hide the block. If 8 or less are empty, display the block.
Where am I going wrong?
You want || not &&. This will display the block only if they are all not empty. I think there is probably a nicer way to do this, though, like array_filter.
Well, you could just use a loop and an $isok variable:
$isok = false;
for($i=1; $i<10; $i++) {
if( !empty($data['var_'.$i])) {
$isok = true;
break; // no need to continue looping
}
}
if( $isok) {
// BLOCK HERE
}
This is easier to edit too, in case you change the var_ part or want a different range of numbers.
You can also try
$data = array_filter($data); // remove all empty value form array
if (count($data)) {
// do your thing
}
The code you wrote will display the block if ALL of the variables aren't empty. If you want it to be displayed when ANY of the variable isn't empty, use OR instead of AND by replacing the && by ||.
<?php
if (!empty($data['var_1']) || !empty($data['var_2']) || !empty($data['var_3']) || !empty($data['var_4']) || !empty($data['var_5']) || !empty($data['var_6']) || !empty($data['var_7']) || !empty($data['var_8']) || !empty($data['var_9'])) {
//BLOCK HERE
}
You can use array_values() for that:
if ( count(array_values($data)) ) {
//BLOCK HERE
}
Replace && (AND) with || (OR)
if (!empty($data['var_1'])
|| !empty($data['var_2'])
|| !empty($data['var_3'])
|| !empty($data['var_4'])
|| !empty($data['var_5'])
|| !empty($data['var_6'])
|| !empty($data['var_7'])
|| !empty($data['var_8'])
|| !empty($data['var_9'])) {
//BLOCK HERE
}
if (empty(array_values($data))) { /* will return you true if all variables are empty*/}

Categories