$n=21;
$p=$n%10==1 && $n%100!=11 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2;
why is $p = 2?
it is supposed to be $p = 0!
is it a bug or am I missing something?
I got this from trying to get the plural form for Russian on: http://www.gnu.org/s/hello/manual/gettext/Plural-forms.html
should be this one:
$p=($n%10==1 && $n%100!=11) ? 0 : (($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20)) ? 1 : 2);
the error was in missing bracets
you can see here: http://php.net/manual/en/language.operators.comparison.php that "It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious". You can see that if you enclose the else part for the first if between ( and ) you will get another result:
$p=$n%10==1 && $n%100!=11 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2);
Maybe you should consider changing you statement to a "regular" if block, something like:
if ($n%10==1 && $n%100!=11)
{
$p =0 ;
}
elseif ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20))
{
$p = 1;
}
else
{
$p= 2;
}
this way being easier to read
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 am using this code to select and compare dates and information from 2 separate MySQLi tables. The code should produce the "discount_amount"IF the discount code entered is the same as the discount code in the database OR the discount code in the database is set to "open". Yet even with the dates set and the code set to "open" I am only getting the discount_amount if I type "open" in to the "$design_discount_code" manually.
if($discount_code == $design_discount_code or $discount_code == 'open' && $date >= $discount_start_date && $date <= $discount_end_date){
$design_price_total = $discount_amount;
$discount = 'yes';
} else {
$design_price_total = $original_price;
$discount = 'no';
}
I have also attempted to change the PHP operator to || and xor with no better results. Any ideas as to why it is only returning 'yes' when I type "open" and not automatically as it should?
You seem to assume that or has higher precedence than and. This is incorrect. You should use parentheses to indicate that the or check is one of the and conditions. Like this:
if(($discount_code == $design_discount_code or $discount_code == 'open') and $date >= $discount_start_date and $date <= $discount_end_date)
Also note that mixing the English and/or with the symbolic &&/|| is not a great idea.
Check the priority of the and and or, maybe you can use parentheses () to specify which should be evaluated first.
if( ( $discount_code == $design_discount_code || $discount_code == 'open') && ( $date >= $discount_start_date && $date <= $discount_end_date) )
Is this a valid syntax in php? I want a call to be done if these two conditions are met.
if($valuear[$i] < $rake) and ($winuser != $winar[$i])
Thanks.
No, it isn't
if (($valuear[$i] < $rake) && ($winuser != $winar[$i]) ){}
You must wrap the whole condition in brackets.
This could help you:
and : &&
or : ||
Set the brackets in this way:
if($valuear[$i] < $rake and $winuser != $winar[$i])
http://php.net/manual/en/control-structures.if.php
The basic syntax is:
if (expr)
statement
you will get syntax error for what you have done
if($valuear[$i] < $rake) and ($winuser != $winar[$i])
It should be
if(($valuear[$i] < $rake) and ($winuser != $winar[$i]))
^ ^
This is the valid way :
if($valuear[$i] < $rake && $winuser != $winar[$i])
I am trying to do:
if(x != 1 || 2) echo 'okay';
With my code here:
if($_POST["timezone"] != ("Pacific/Midway" || "America/Adak" || "Etc/GMT+10" || "Pacific/Marquesas")) {
$timezone_error = 'Invalid timezone';
}
Whereas I put in information that did not equal, and $timezone_error was still not set, what is the proper OR operator that I should be using, or is this possible at all? I would rather not write $_POST['x'] != 1, $_POST['x'] != 2 all out separately as this is quite a long list.
what you want is something like this
$array = array("Pacific/Midway" , "America/Adak" , "Etc/GMT+10" , "Pacific/Marquesas");
if (!in_array($_POST["timezone"], $array){
$timezone_error = 'Invalid timezone';
}
Correct format would be:
if(x!= 1 || x!=2) echo 'okay';
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'))) );