PHP: If-statement, need value of condition that triggered - php

I'm sorry for the vaguely described title. This is what I want:
if($a[$f] === false || $a[$g] === false || $a[$h] === false || $a[$i] === false || $a[$j] === false)
{
// do something
}
I want to do something with the condition that actually triggered the statement (if a[$f] = true and a[$g] = false, I want to do something with $g).
I know that in this case, the first statement that went true (i.e. $a[$g] == false) triggers. But is there any way to do something with $g? I've never seen this in my programming life before and can't seem to find anything about it.
Thanks in advance.
--- Edit ---
I forgot to mention: I'm using a function on all the array data. So, shortened, I get this:
if(valid($a[$f]) === false || valid($a[$g]) === false)
{
// do something
}
--- Edit 2 ---
This piece of OOP-based PHP, where I'm in a class, is my code.
if($this->validatedText($product[$iName]) == false ||
$this->validatedUrl($product[$iUrl]) == false ||
$this->validatedNumber($product[$iTax]) == false ||
$this->validatedValuta($product[$iPrice]) == false ||
$this->validatedText($product[$iArticleNumber]) == false ||
$this->validatedText($product[$iDescription]) == false ||
$this->validatedText($product[$iMetaDescription]) == false ||
$this->validatedText($product[$iTitle]) == false)
{
// do something with the first iVariable
}

Simplest solution will be
if(false!==($sIndex = array_search(false, $a, 1)))
{
//your $sIndex is first index with false value
}
if you want all keys, you may use array_filter(), like this:
$rgFalse = array_keys(array_filter($a, function($x)
{
//here valid is your function
return false===valid($x);
}));

Related

php: I need the OPPOSITE of this please...?

The following searches the string $fruit for any of the words apples, oranges and bananas and acts accordingly:
if ((stristr($fruit,'apples')) || (stristr($fruit,'oranges')) || (stristr($fruit,'bananas')) !== false) {//some code }
I need the OPPOSITE. I need code to run if the string $fruit does NOT have any of the three. I was thinking something like
if not ((stristr($fruit,'apples')) || (stristr($fruit,'oranges')) || (stristr($fruit,'bananas')) !== false) {//some code }
but that doesnt seem to work...
A little hand holding please? Thanks...
What you're looking for is DeMorgan's law:
The negation of a conjunction is the disjunction of the negations.
The negation of a disjunction is the conjunction of the negations.
Try:
if ( stristr($fruit, 'apples') === FALSE && stristr($fruit,'oranges') === FALSE && stristr($fruit,'bananas') === FALSE) {
// some code
}
Since stristr only returns a string or FALSE, this code can be simplifed to:
if ( !stristr($fruit, 'apples') && !stristr($fruit,'oranges') && !stristr($fruit,'bananas')) {
// some code
}
Thanks for the help everyone.
I ending up doing this, based on all input from those that replied, which worked:
if (!((stristr($fruit,'apples')) || (stristr($fruit,'oranges')) || (stristr($fruit,'bananas')) !== false))
Again, thanks for the help!
if (!(stristr($fruit,'apples')) || !(stristr($fruit,'oranges')) || !(stristr($fruit,'bananas')) !== false) {//some code }

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

PHP - An if statement with concatenated '&&' and '||' can give problems?

I have a problem with a form check that use an if statement with multiple 'and' and 'or' operators. This check return me an anomalous occasionally false value.
public function insert_checkForm($form) {
$form = array_filter($form);
if (
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
isset($form['travel_go_from']) != isset($form['travel_go_to']) ||
isset($form['work_go_from']) != isset($form['work_go_to']) ||
!isset($form['travel_go_from']) &&
!isset($form['travel_go_to']) &&
!isset($form['work_go_from']) &&
!isset($form['work_go_to'])
) {
return false;
} else {
return $form;
}
}
Last question, the above code changes compared to this (in spite of the priorities of and operators)?
[...]
!isset($form['report_id']) ||
!isset($form['date']) ||
!isset($form['technical_id']) ||
(isset($form['travel_go_from']) != isset($form['travel_go_to'])) ||
(isset($form['work_go_from']) != isset($form['work_go_to'])) ||
(!isset($form['travel_go_from']) && !isset($form['travel_go_to']) && !isset($form['work_go_from']) && !isset($form['work_go_to']))
[...]
Thanks =)
The most common problem with isset() is that it returns false when the item is NOT SET but also returns false if the item IS SET && IS NULL.
isset($arr['nonexisting']); //this returns: false
$arr['existing'] = null;
isset($arr['existing']); //this returns: false

IF statement with multiple conditions

I can’t get the desired result from a series of conditions in an IF.
if (($varteam == $_POST['rteam1']) && ($varteam == $_POST['rteam2']) && ($varteam == $_POST['rteam3']) && ($varteam == $_POST['rteam4']) && ($varteam == $_POST['rteam5']))
{true}
else
{false}
Starting from the variable $varteam I want to obtain true if all the compared values are identical, otherwise false.
The compared values may also be null.
With the code I’ve posted it works if all the values are equal or different but I get true instead of false if one or more values are different.
Why does it happen?
I am guessing that you may get false positives when you have 0 mixed with null or false. Just to be on the safe side, use === instead of == so type checking is in effect. That way, null !== false !== 0.
if (($varteam === $_POST['rteam1']) &&
($varteam === $_POST['rteam2']) &&
($varteam === $_POST['rteam3']) &&
($varteam === $_POST['rteam4']) &&
($varteam === $_POST['rteam5']))
{
// true
}
else
{
// false
}

PHP nested if conditions

What I'm trying to achieve is a nested condition like if ((a = true && b = true) or (c = true && d = true)) {...}. This doesn't seem to be working for me.
To further explain: I have two variables in what we could call a 'top', and two variables in what we could call a 'bottom'. What I need to do is execute code if variables are true in both 'top' and 'bottom'.
To more succinctly illustrate:
if ((t1 = true && b1 = true) or
(t1 = true && b2 = true) or
(t2 = true && b1 = true) or
(t2 = true && b2 = true)
) {
...do some stuff...
}
I'm tempted to call it 'conditional statement for lattice problem'... except it isn't really a lattice problem... but it does sort of look like a lattice if you drew it |X| ... s'yeah, you're amazing if you can tell me a good way to do this, and you're super amazing if you can tell me what I ought to call it.
= is used for assignment, == is used to test for equality, === is used to test for equality and equal types. You need to use either == or ===, depending on whether t1, t2, b1, b2 are already boolean or something else. Also the stuff that Adam said, except that or is perfectly valid.
You need to use two equal signs == instead of =. Also, your variables need to have $ in front of them. And your "or" should be ||.
The logic is ok.
It should look like this:
if (($t1 == true && $b1 == true) ||
($t1 == true && $b2 == true) ||
($t2 == true && $b1 == true) ||
($t2 == true && $b2 == true)
) {
...do some stuff...
}
change condition
if (($t1 == true && $b1 == true) or
($t1 == true && $b2 == true) or
($t2 == true && $b1 == true) or
($t2 == true && $b2 == true)
)
= is assignment operator == is comparison operator.

Categories