I am just making this short but if there are lots of conditions in this if statement, is there a way to shorten it?
if(($post->id <= 37) || ($post->id == 42) || ($post->id == 44) || ($post->id == 45) || ($post->id == 46))){
//code here
}
it'll all be id but the number would be different and there's no ranging or old / even numbers just randoms
Is there a way to make it shorter?
if($post->id <= 37 || in_array( $post->id, array( 42,44,45,46)))
Related
I am trying to consolidate a few different if statements. What I am trying to accomplish would read something like this:
If (this is true and this is true) OR (this is true and this is true) AND (This is true)
So, one at least one of the first two sets of parentheses would need to be true, and if one of those is true, then also the last set of parentheses would need to be true, in order for the code inside to be executed.
Here is the specific code I am (unsuccessfully) trying to make work:
if(($calc->number % 2 == 1 && $calc->doubleColor == 'b2' | $calc->number % 2 == 0 && $calc->doubleColor = 'r2') && in_array($calc->number, $backToBet)){
}
Is there a way to do this? A possibility? Is there any drawback to getting a lot into a single if statement?
EDIT
$blackMatch = $calc->number % 2 == 1 && $calc->doubleColor == 'b2';
$redMatch = $calc->number % 2 == 0 && $calc->doubleColor = 'r2';
$numberMatch = in_array($calc->number, $backToBet);
if(($blackMatch || $redMatch) && $numberMatch){
}
/ ** Calc->number = 2, $blackMatch = false, $redMatch = false,
$numberMatch array contains 2 **/
Basically what I end with is a 'true' result, even though neither of the conditions within the inner parentheses are satisfied.
to make code easier to read, I'd suggest to use separate variables, like this:
$condition1 = ($calc->number % 2 == 1) && ($calc->doubleColor == 'b2');
$condition2 = ($calc->number % 2 == 0) && ($calc->doubleColor == 'r2');
$condition3 = in_array($calc->number, $backToBet);
if (($condition1 || $condition2) && $condition3) {
}
two things to note:
|| is logical OR, | is bitwise OR
== is comparison, = is assignment
I have a loop in which I check for numbers. Something like this, more or less:
<?php $counter = 0; ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $counter++; ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php if ($counter == (2 || 4)) : ?>
// DO SOMETHING
<?php endif; // counter ?>
...
But $counter ==(2||4) returns true for every number (1, 2, 3, 4 etc.). I also tried:
$counter == 2 || 4
$counter === 2 || 4
$counter == (2 or 4)
But to no avail. The first and last one also return true for every natural number and the second one never returns true.
An alternate solution would be to do the following:
$allowed = array(2, 4);
if (in_array($counter, $allowed)) {
// good value
}
The advantage of this is that you can extend the list of allowed values very easily, maybe even placing them in an external config.
simply replace
if ($counter == (2 || 4)) :
with
if ($counter == 2 || $counter == 4) :
see also the manual: http://www.php.net/manual/en/control-structures.if.php
If you want to do something when the counter is even, you can use the following:
<?php if ($counter % 2 === 0) : ?>
// do something
// will match 0, 2, 4, 6, ...
<?php endif; ?>
Its not possible to check for multiple values inside a condition like that.
This is one reason why php sucks... you're are forced to use two conditions, like
if($counter == 2 || $counter == 4)
or if you want to trigger your conditions on even numbers
if($counter % 2 == 0)
someone mentioned in_array() but you should know that arrays are very expensive in php in terms of resources.
The expression (2 || 4) will always return true. This is because both operands to the || operator are non zero valued constants. It is essentially equivalent to (true || true) which is always true as is (false || true).
the expression
$counter == (2 || 4)
will yield true except when $counter is 0
You want to write
($counter === 2 || $counter === 4)
note that you should always use === instead of == because the latter performs type coercion on its operands.
if I have something like:
if (($browser->getBrowser() == Browser::BROWSER_SAFARI && $browser->getVersion() >= 3 ) ||
($browser->getBrowser() == Browser::BROWSER_CHROME && $browser->getVersion() >= 5 ))
{
// code here
}
but I really want to say also if Chrome >= 5 but less 6...
I will add an else if for 6+
later on in else () less than version 5 would fall into..
How would I write >= 5 but < 6?
So you can do this directly by adding another condition:
if (($browser->getBrowser() == Browser::BROWSER_SAFARI
&& $browser->getVersion() >= 3 ) ||
($browser->getBrowser() == Browser::BROWSER_CHROME
&& $browser->getVersion() >= 5 && $browser->getVersion() < 6 ))
{
// code here
}
I'm assuming getVersion must be capable of returning non-integer numbers, otherwise you could just check for equality ($browser->getVersion() == 5).
I can't get this code to work
if ($title != 'Main' || $nSpace == 56 || $nSpace == 30 && $ifHome != 361)
So i settled for this code
if ($title != 'ArticleCategories' || $nSpace == 1)
if ($nSpace == 0 && $ifHome != 1)
So, i am now wondering how can i get those two lines into one line in the way so that it works? I know how to get multiple or || statements into one line but not the or || and and && statements together.
Use parens to group the logic together in a manner that makes sense for your program.
if ($title != 'Main' || $nSpace == 56 || ($nSpace == 30 && $ifHome != 361))
In the PHP below if I compare a variable using == it works as I would expect it to, if I use != then my code breaks, can someone explain or help?
$_GET['p'] = 'home';
// DOES NOT work, it will always return "show JS" regardless to what string I have
if ($_GET['p'] != 'home' || $_GET['p'] != 'create.account'){
echo 'show JS';
}else{
echo 'do not show JS';
}
// Works as I would expect it to
if ($_GET['p'] == 'home' || $_GET['p'] == 'create.account'){
echo 'do not show JS';
}else{
echo 'show JS';
}
$_GET['p'] can't be two different things at the same time. You say int the first expression;
p not home or not create.account. Its always true. You should use && instead of ||
You can see the problem by DeMorgans Laws for negation
!( A || B ) === (!A && !B)
The solution you give is impossible because there is no way for both statements to be false, because that would imply the string is equivalent to both the compared strings. The only way the else block would be hit is if both were false (because of the OR statement).
(X != A) || (X != B)
≡ !(X == A) || !(X == B)
≡ !((X == A) && (X == B))
(X == A) && (X == B) is always false since the condition A != B but X cannot be both A and B at the same time. So !((X == A) && (X == B)) and your (X != A) || (X != B) is always true.
if ($_GET['p'] != 'home' && $_GET['p'] != 'create.account')