I am attempting to ask:
If my variable $a is set
And if my variable $a is not apple or orange
Then do something
I'm attempting to do this with the following code, but true is returned.
Any ideas what I am doing wrong?
<?php
$a = 'banana';
if (isset($a) && ($a !== 'apple' | $a !== 'orange')) {
echo 'true';
} else {
echo 'false';
}
You probably meant && instead off || (not |):
And if my variable $a is not apple or orange
So if you don't want it to be either of those values, you'll need
($a !== 'apple' && $a !== 'orange')
So the script becomes
<?php
$a = 'banana';
if (isset($a) && $a !== 'apple' && $a !== 'orange') {
echo 'true';
} else {
echo 'false';
}
Now, false is shown if:
$a is not set
$a === apple
$a === orange
Try it online!
You're missing a | in your or expression
if (isset($a) && ($a !== 'apple' || $a !== 'orange')) {
echo 'true';
} else {
echo 'false';
}
You are missing a | OR operation is ||
if (isset($a) && ($a !== 'apple' || $a !== 'orange')) {
Related
In this exercise I am trying to create a box with for-loops and if statements. I am stuck and not sure which part of my code is wrong. Somehow the $aboxwidth values 1 and 4 get repeated twice.
When substituting spaces for 0 the output looks like this:
output:
+0-0-0+0
|000|0
|000|0
+0-0-0+0
code:
<?php
//box builder
$iboxheight=4;
$aboxwidth=4;
// +-----------+
// | |
// | |
// | |
// +-----------+
// +--+
// | |
// | |
// +--+
//building boxheight
for ($i=1; $i <= $iboxheight; $i++) {
//$boxwidth
for ($a=1; $a <= $aboxwidth; $a++) {
//build + in corners
if (($i === 1 && $a === 1) || //corner top left
($i === 1 && $a === $aboxwidth) || // corner top right
($i === $iboxheight && $a === 1) || // corner bottom left
($i === $iboxheight && $a === $aboxwidth)) { // corner bottom right
echo "+";
}
//build top and bottom
if ($i === 1 || $i === $iboxheight) {
if ($a !== 1 && $a !== $aboxwidth) {
echo "-";
}
}
//build walls
if ($a === 1 || $a === $aboxwidth) {
if ($i !== 1 && $i !== $iboxheight) {
echo "|";
}
}
if ($i !== 1 || $i !== $iboxheight) {
if ($a !== 1 || $a !== $aboxwidth) {
echo "0";
// echo " ";
}
}
}
echo "<br>";
}
I am grateful for any tips and suggestions.
Thanks
The program will echo "0"; for every $i and $a. Why is that?
This ($i !== 1 || $i !== $iboxheight) is always true. And this $a !== 1 || $a !== $aboxwidth is always true. When $i is 1, it is not equal $iboxheight, therefore true. When $a is 1, it is not equal $aboxwidth, therefore true. And so on. The program echo's 0 for every iteration.
(You might try echo "($i,$a)"; in place of echo "0"; to see it in action).
If the || are changed to && it would accomplish the desired result, ie only echo {whatever} when the (row,column) is not on the "border".
This seems pretty simple, but I can't figure that out.
I am checking a few variables before showing some stuff on the screen, and I have a variable which can be null at some point.
On my if statement I have:
if ($a != 'abc' && ($a == $b || $b == $c) && min($variable) > 3) { ... }
How can I set it true if min($variable) is null, if all the other statements are true?
As I understood from your question,
the min($variable) maybe null and you want to check it whether it's value less than 3 or null value ?
if ($a != 'abc' && ($a == $b || $b == $c)
&& (min($variable) > 3 || min($variable) == null)) { ... }
could be using a ternary operator inside ( )
if ($1 != 'abc' && ($1 == $2 || $2 == $3) &&
( $variable == null ? true : min($variable) > 3) ) { ... }
I am attempting to use both AND and OR statements in my IF/ELSE statement, but I cannot get the desired effect.
What I would like to achieve is that if either 'a' or 'b' has a value of '1' but both 'c' and 'd' must be 1 then I get 'Yes'.
All my attempts have given me either 'Yes' or have not worked (blank screen).
<?php
$a = "0";
$b = "1";
$c = "1";
$d = "1";
if (($a == "1") || ($b == "1") && ($c == "1") && ($d == "1")) {
echo "Yes";
}
else {
echo "No";
}
?>
Thank you.
You need and extra parenthesis, to make sure the evaluation order will be done correctly, like in math:
if ( ( ($a == "1") || ($b == "1") ) && ($c == "1") && ($d == "1")) {
^ ^
That way, let's say for example:
$a = 1;
$b = 2;
$c = 1;
$d = 2;
The first parenthesis will be evaluated as true || false. The final result will be true.
So now you have true && ($c == "1") && ($d == "1")
$c = 1, so again, the next evaluation will be true && true && ($d == 1)
$d = 2, so the next round will be true && true && false, final result, in this example, will be false.
You need to add parenthesis.
Why?
Because inner parenthesis are evaluated first before outer parenthesis. Take this example:
((1 == 1 && (2 == 2)) || 3 == 3)
What will be evaluated first? The 2 == 2 then the 1 == 1 and then the 3 == 3. In your if condition, because you are mixing AND's and OR's, you will not get the desired affect.
( (($a == "1") || ($b == "1")) && ($c == "1") && ($d == "1") )
Should work for you. In fact you can do this so that it looks even better:
(($a == 1 || $b == 1) && $c == 1 && $d == 1)
Because it is not necessary to put 1 in quotes ie: "1". PHP's truthiness will evaluate 1 == "1" to be true. However if you wanted to check for an actual string that contains 1, then you would use the === operator.
$a = 1;
$b = "1"
$a == "1"; // true
$b == 1; // true
$a === "1"; // false
$b === "1"; // true
However for more information go here: http://php.net/manual/en/language.operators.precedence.php
The equality operators will be evaluated first, then &&, then ||. Parentheses will be evaluated before anything else, so adding them can change the order.
Check the answer In Java, what are the boolean "order of operations"?
It will always echo a Yes because PHP interpreter places The AND operation before the OR operation.
So your if statement interpretes like this:
If
a = 1 or b = 1 and c = 1 and d = 1
then
echo 'Yes'
else
echo 'No'
That's why you always get a yes..
Any ideas how to shorten if statment in an elegant way.
My if statement:
if(getfoo1() == getfoo2() && getfoo2() == 1)
{
}
EDIT:
I'm looking for something like:
if(getfoo1() == getfoo2() ==1)
{
}
But I suppose we can't do this.
$a = getfoo1();
$b = getfoo2(); // less operations, while it not produces duplicate calls
if($a == $b && $b == 1){
// do something
}
$variable = ((getfoo1() == getfoo2() && getfoo2() == 1) ? $value1 : $value2);
More elegant, combined:
$a = getfoo1();
$b = getfoo2();
$variable = (($a == $b && $b == 1) ? $value1 : $value2);
Since we don't know the possible return values from the functions, if you assume they are integers then you can say:
$a = getfoo1();
$b = getfoo2();
if (($a * $b) === 1) { // strict equality for the win
echo 'hi';
}
The result would only be true iff both $a AND $b are 1.
Another way:
$both = array(getfoo1(), getfoo2());
// use array_diff_assoc so it checks multiple occurrences of the same value
$diffCount = count(array_diff_assoc($both, array(1, 1)));
if ($diffCount === 0) {
echo 'hi';
}
Since anyway getfoo2() == 1 must be true, a better approach is to first check whether getfoo2() is equal to 1. If it false no matter about 2nd condition. But If you first check getfoo1() == getfoo2() and and then check getfoo2() == 1 you have to check 2 conditions all the times.
Therefore go for
$a = getfoo1();
$b = getfoo2();
if($b == 1 && $a == $b)
{
// logiv
}
else
{
}
Try this.
$a = getfoo1();
$b = getfoo2();
if( intval($a && $b) === 1) {
echo 'hi';
}
I want to do something like this:
if( $a = 'something' && $b = substr( $a, 2 ) )
{
//do something
}
I mean, on an if condition, evaluate two conditions, and the second one passing the first assigned $a as a parameter to the second condition function substr().
It is just an example, so I don't want answers to this functionality, just generic answers.
The above code throws 'Undefined' $a, since $a is not still assigned.
I could do the next:
if( $a = 'something')
{
if( $b = substr( $a, 2 ) )
//do something
}
}
but this will make my code bigger.
Is there any way to achieve something like the first example?
Edit:
I don't want to compare. Just assign and ensure that $a and $b are not null, false, ...
Your only problem is the wrong precedence of the && and = operators. This works just fine:
if (($a = 'something') && $b = substr($a, 2))
This way, $a is undefined:
if ($a = 'something' && $b = substr($a, 2))
But if you give the = operator priority:
if (($a = 'something') && $b = substr($a, 2))
It will be set.
Moreover, you can simply write:
if( $b = substr( $a = 'something', 2 ) )
This question intrigued me along with #moonwave99 answer, so I did some testing with his last answer.
if( $b = substr( $a = NULL, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = FALSE, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = 0, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = TRUE, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
FAIL
if( $b = substr( $a = 233, 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
PASS
if( $b = substr( $a = "SOMETHING", 2 ) ) { echo "PASS"; } else { echo "FAIL"; }
PASS
The only way to get it to fail was to pass the Boolean TRUE. But if you are expecting string values, it should fail all Boolean values, zero and NULL and evaluate to true on ints, floats, and string values. (Haven't tested with array, but I suspect it would fail for any non-primitive types). Interesting question.
Use isset() for that.Also keep in mind use == or === for comparison operations since = is assignment operator
if( (isset($a) && $a == 'something') && (isset($b) && $b == substr( $a, 2 )) )
{
//do something
}