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.
Related
I have a question about IF statements with multiple logical OR operators.
If we let:
$x=1;
A. I typical would write a IF statement comparing two items like this:
if($x == 1 || $x == 2) echo 'good';
else echo 'bad';
B. But, is this a valid IF statement? If not, why? (because it seems to work)
if($x == (1 || 2)) echo 'good';
else echo 'bad';
C. I typical would write a third comparison like this:
if($x == 1 || $x == 2 || $x == 3) echo 'good';
else echo 'bad';
D. What about this, following suit with B, above? (it does not seem to work)
if($x == (1 || 2 || 3)) echo 'good';
else echo 'bad';
The example in B, above works, but not the example in D. Why?
I cannot find any PHP documentation as to why.
Here is what happens for every version:
A. $x == 1 || $x == 2
PHP will compare $x with the value 1, this is true so it can short-circuit the if and echo 'good'.
B. $x == (1 || 2)
PHP will evaluate 1 || 2 because parentheses indicate the priority, as the result should be a boolean expression it will cast 1 to a boolean which evaluates to true so the expression becomes $x == true.
Now PHP will evaluate this expression. First it will cast both types to the same, according to the documentation, it will "Convert both sides to bool". So, same as above, as $x is 1 it will be cast to true and then the expression becomes true == true which is true.
C. $x == 1 || $x == 2 || $x == 3
It is the same as A.
D. $x == (1 || 2 || 3)
It is quite the same as B.
And, for the record 1 == (1 || 2 || 3) evaluates to true.
Within a for loop, i need to add some HTML that outputs only when the loop is on a [(multiple of 3) minus 1].
For example, what i could do is:
for($i=0; $i<count($imagearray); $i++)
{
if($i=="0" || $i=="2" || $i=="5" || $i=="8" || $i=="11")
{
echo 'string';
}
}
but this isnt very elegant and extremely useless for big for loops, is there a proper way to do this?
if ( $i==0 || ($i+1)%3 == 0 )
{
//do stuff
}
What this will do, is go to the next index, divide it by 3, and see if there is a remainder. If there is none, then that means that the current index is one less than a number that is divisible by 3
Use the modulus operator.
if (! (($i+1) % 3) ) {
If $i+1 divides into 3 with no remainder, the result will be zero. Then you just need a boolean not.
If you want to match 0 as well (since you use it in your example, but it doesn't match your description) then you will have to special case it with an ||.
You want to use the modulo for that:
(1 % 3) == 1
(2 % 3) == 2
(3 % 3) == 0
(4 % 3) == 1
Good luck
Modulo is the same thing as saying, give me the remainder of a division. So 1 / 3 equals 0 remainder 1, and so on.
if(($i+1)%3 == 0){
//do something
}
The % operator is known as the modulus operator and returns the remainder of a division.
the most elegent method is thus
if ($i % 3 === 2) {
//do stuff
}
as it doesn't add things to the $i value, but all answers are essentially correct!
I have some loop. How can i optimize this code that it would be executed each 12 items starting from 2nd item. I'm using hardcoded code for each of +12 item, but of course this is not a good solution :) I know it's easy to do and i was trying to do it with modulus operator, however it worked somehow incorrectly.
<?php if( ($counter == 2) || ($counter == 14) || ($counter == 26) || ($counter == 38) || ($counter == 50) || ($counter == 62) || ($counter == 74) || ($counter == 86) || ($counter == 98 .... ?>
Thanks for help!
Answer
Take a look at the Modulus operator (%):
<?php
if ( ( $counter - 2 ) % 12 == 0 ) {
//....
}
?>
Explanation
The Modulus operator ($a % $b) is the remainder of $a divided by $b.
$counter - 2 - as you're starting with an offset of 2, remove that from $counter
% 12 - will return the remainder of $counter - 2 divided by 12
== 0 - if the above returns 0, you know that it is exactly divisible
I am trying to convert the following code into a Ternary Operator, but it is not working and I am unsure why. I think my problem is that I do not know how to express the elseif operation in ternary format. From my understanding and elseif is performed the same way as an if operation by using the format : (condition) ? 'result'.
if ($i == 0) {
$top = '<div class="active item">';
} elseif ($i % 5 == 0) {
$top = '<div class="item">';
} else {
$top = '';
}
$top = ($i == 0) ? '<div class="active item">' : ($i % 5 == 0) ? '<div class="item">' : '';
$top = ($i == 0) ? '<div class="active item">' : (($i % 5 == 0) ? '<div class="item">' : '');
you need to add parenthesis' around the entire else block
The Ternary Operator doesn't support a true if... else if... else... operation; however, you can simulate the behavior by using the following technique
var name = (variable === 1) ? 'foo' : ((variable === 2) ? 'bar' : 'baz');
I personally don't care for this as I don't find it more readable or elegant. I typically prefer the switch statement.
switch (variable) {
case 1 : name = 'foo'; break;
case 2 : name = 'bar'; break;
default : name = 'bas'; break;
}
Too late probably to share some views, but nevertheless :)
Use if - else if - else for a limited number of evaluations. Personally I prefer to use if - else if - else when number of comparisons are less than 5.
Use switch-case where number of evaluations are more. Personally I prefer switch-case where cases are more than 5.
Use ternary where a single comparison is under consideration (or a single comparison when looping), or when a if-else compare is needed inside the "case" clause of a switch structure.
Using ternary is faster when comparing while looping over a very large data set.
IMHO Its finally the developer who decides the trade off equation between code readability and performance and that in turn decides what out of, ternary vs. if else-if else vs. switch-case, can be used in any particular situation.
//Use this format before reducing the expression to one liner
$var=4; //Change value to test
echo "Format result: ";
echo($var === 1) ? 'one' : //if NB.=> $varname = || echo || print || var_dump(ternary statement inside); can only be (placed at the start/wrapping) of the statement.
(($var === 2) ? 'two' : //elseif
(($var === 3) ? 'three' : //elseif
(($var === 4) ? 'four' : //elseif
'false' //else
))); //extra tip: closing brackets = totalnumber of conditions - 1
// Then echo($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false')));
echo "<br/>";
var_dump("Short result: ", ($var === 1)?'one':(($var === 2)?'two':(($var === 3)?'three':(($var === 4)?'four':'false'))) );
I have something like
just a snipplet
$i = 1; while (...) {
echo ($i % 5 == 1) ? 'class="first-col"' : ($i % 5 == 0) ? 'class="last-col"' : '';
$i++;
}
but even when $i % 5 == 1, I will get class="last-col" echo-ed is my logic right?
This is actually a CSS fix for IE so that I wont need to use nth-child. I am trying to target the 1st and last columns of my grid which contains 5 col/row
The ?: operator is left-associative, i.e. you have
echo ( ($i % 5 == 1) ? 'class="first-col"' : ($i % 5 == 0) ) ? 'class="last-col"' : '';
See http://php.net/manual/en/language.operators.comparison.php
It is best not to nest ternary operators.
Better use if / elseif / else constructs, they are more legible.
adding parenthesis helps:
echo (($i % 5 == 1) ? 'class="first-col"' : (($i % 5 == 0) ? 'class="last-col"' : ''));
For a start you're using nested ternary operations. I'd at least use brackets around the individual conditions to make it obvious what should be carried out first.