Why does undivided switch case conditon not works [duplicate] - php

whats wrong with my switch ?
Now result:
< more
> less
= equality
!= no't equality
As it should be:
< more
= equality
<?php
$page = 99;
switch ($page)
{
case $page < 121:
echo '< more <br/>';
case $page > 123:
echo '> less <br/>';
case $page == 99:
echo '= equality <br/>';
case $page != 99:
echo '!= no\'t equality <br/>';
}
?>

In your switch statement you're comparing a number with boolean values.
Let's take the first case $page < 121 is true, so the comparison taking place is 99==true which is true according to http://docs.php.net/language.types.type-juggling (switch performs a loose comparison, not a strict like ===). Thus the first case block is executed.
And since you don't have a break statement it falls through to the next case block and the next and so on...
Meaning: This won't work as intended regardless of whether you use break or not.

You don't seem to understand how switch works. What you want is a series of if statements, i.e.
if ($page < 121)
echo '< more <br/>';
if ($page > 123)
echo '> less <br/>';
if ($page == 99)
echo '= equality <br/>';
if ($page != 99)
echo '!= no\'t equality <br/>';

Switch is to be used only when you want to compare a variable against a set of values.
switch ($variable)
{
case "me":
echo "variable is me";
break;
case "you":
echo "variable is you";
break;
default:
echo "Variable is neither of us";
}
The above switch case block can be written as shown below:
if ($variable=="me")
{
echo "variable is me";
}
elseif ($variable=="you")
{
echo "variable is you";
}
else
{
echo "variable is neither of us";
}
DO NOT put an expression near the case statement.
switch ($somethng)
{
case $something < 10:
break;
case $something > 20:
break;
}
Switch is meant to be used only for comparing a variable against a set of values. ONLY! For everything else use a if...elseif..else statement.
The block above is wrong usage. Sometimes more than one of those expressions could be true.

$var = "cat";
switch($var)
{
case "cat":
echo 'My '.$var.' is called Bob.';
break;
case "dog":
echo 'My '.$var.' is called James.';
break;
default:
echo "I don't have an animal";
break;
}
In a switch statemant you compare $var against value in a case. If there is a match, the actual case will be executed, otherwise the default will be executed. You can't use <>!=... in a case, only values like: 1, '1', 'dog', $var2, and so on.
If you want to run the same command for two case you can do:
$var = "cat";
switch($var)
{
case "cat":
case "dog":
echo 'My '.$var.' is called James.';
break;
default:
echo "I don't have an animal";
break;
}
In your code, your forgot to put break; at the end of each case, that's why you see 'everything' in your output. And you miss default: too.
For the task you're doing, i suggest you to use if statements.

if iam not wrong you can't use this characters < > raw in html. use instead the entities > and <.
if you run the script in the command line i got following output.
<?php
ob_start();
$page = 99;
switch ($page)
{
case $page < 121:
echo '< more <br/>';
case $page > 123:
echo '> less <br/>';
case $page == 99:
echo '= equality <br/>';
case $page != 99:
echo '!= no\'t equality <br/>';
}
$buffer = ob_get_clean();
echo str_replace('<br/>', "\n", $buffer);
output
< more
> less
= equality
!= no't equality
which seems to be the correct behavoir.
It is important to understand how the
switch statement is executed in order
to avoid mistakes. The switch
statement executes line by line
(actually, statement by statement). In
the beginning, no code is executed.
Only when a case statement is found
with a value that matches the value of
the switch expression does PHP begin
to execute the statements. PHP
continues to execute the statements
until the end of the switch block, or
the first time it sees a break
statement.
http://de.php.net/manual/de/control-structures.switch.php

';
break;
case $page > 123:
echo '> less ';
break;
case $page == 99:
echo '= equality ';
break;
case $page != 99:
echo '!= no\'t equality ';
break;
default: echo 'Default';
}
?>

Related

strtotime time/date in PHP switch/case statement

I've created a PHP switch / case statement:
switch(true)
{
case ($eff >= '10/2017'):
echo "Greater than 10-2017";
break;
case ($eff <= '09/2017'):
echo "Less thank 10-2017";
break;
default:
echo '';
break;
}
I'm using strtotime to cut down a date from 08/01/2017 to just 08/2017 like this:
$eff = date("m/Y", strtotime($userinfo['ExpDate']));
Unfortunately when I use this to create my case statement it doesn't give me the conditional output. I was hoping for when using a > or < operator and just defaults to the first case statement.
How do I use this to properly use greater/less than?
You can use a solution like the following:
<?php
$eff = date("m/Y", strtotime('10/01/2017'));
$eff = date_create_from_format('m/Y', $eff);
switch(true)
{
case (date_create_from_format('m/Y', '10/2017')->diff($eff)->format('%R%m') >= 0):
echo "Greater than 10-2017";
break;
case (date_create_from_format('m/Y', '09/2017')->diff($eff)->format('%R%m') <= 0):
echo "Less thank 10-2017";
break;
default:
echo '';
break;
}
demo: https://ideone.com/Z2un3C / some tests: https://3v4l.org/YP1Ub

PHP switch and if statement output different results

Could someone please explain what's going on here?
Here's the code:
$num = 0;
switch($num){
case ($num==0):
echo $num , " is ZERO";
break;
case ($num>0):
echo $num , " is POSITIVE";
break;
default:echo $num , " is NEGATIVE";
}
The above outputs 0 is POSITIVE
if($num==0){
print ($num." is ZERO");
}
elseif($num>0){
echo $num , " is POSITIVE";
}
else{
echo $num , " is NEGATIVE";
}
This works as expected - 0 is ZERO.
If I replace
case($num==0) with case(0) the output is OK.
Why does the case($num==0) fail?
Someone told me the issue with evaluating multiple expressions in the case statements, but it seems fine syntactically.
switch compares everything in the switch (...) expression to each case:
switch ($num) {
case 0 :
...
case 1 :
...
...
}
You don't write case $num == 0, as that's equivalent to if ($num == 0 == $num).
If at all, you'd have to do:
switch (true) {
case $num == 0 :
...
case $num > 0 :
...
...
}
But there are people who frown upon that.
The logical structure of switch operator is this:
switch($x):
case val1:
action 1;
break;
case val2:
action 2;
break;
default:
not val1 and val2;
switch compares $x with one of the values or gives default branch in case nothing matches. So, you can't write:
case ($num > 0):
or
case ($num == 0 ):
In your case it gives POSITIVE, because php first evaluates the expressions inside cases, and we get the following in the output:
Is $num == 0 ?: yes => 1
Is $num > 0 ?: no => 0
And the real switch php evaluates is this:
$num = 0;
switch($num){
case 1:
echo $num , " is ZERO";
break;
case 0:
echo $num , " is POSITIVE";
break;
default:
echo $num , " is NEGATIVE";
}
Output is: POSITIVE.

How to write a PHP ternary operator [duplicate]

This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 2 years ago.
How do I write a PHP ternary operator with the elseif portion?
I see basic examples with the if and else portions of the PHP ternary operator like this:
echo (true) ? "yes" : "no"; //prints yes
echo (false) ? "yes" : "no"; //prints no
How do I get the "elseif" portion like this into the ternary operator?
<?php
if($result->vocation == 1){
echo "Sorcerer";
}else if($result->vocation == 2){
echo 'Druid';
}else if($result->vocation == 3){
echo 'Paladin';
}else if($result->vocation == 4){
echo 'Knight';
}else if($result->vocation == 5){
echo 'Master Sorcerer';
}else if($result->vocation == 6){
echo 'Elder Druid';
}else if($result->vocation == 7){
echo 'Royal Paladin';
}else{
echo 'Elite Knight';
}
?>
A Ternary is not a good solution for what you want. It will not be readable in your code, and there are much better solutions available.
Why not use an array lookup "map" or "dictionary", like so:
$vocations = array(
1 => "Sorcerer",
2 => "Druid",
3 => "Paladin",
...
);
echo $vocations[$result->vocation];
A ternary for this application would end up looking like this:
echo($result->group_id == 1 ? "Player" : ($result->group_id == 2 ? "Gamemaster" : ($result->group_id == 3 ? "God" : "unknown")));
Why is this bad? Because - as a single long line, you would get no valid debugging information if something were to go wrong here, the length makes it difficult to read, plus the nesting of the multiple ternaries just feels odd.
A Standard Ternary is simple, easy to read, and would look like this:
$value = ($condition) ? 'Truthy Value' : 'Falsey Value';
or
echo ($some_condition) ? 'The condition is true!' : 'The condition is false.';
A ternary is really just a convenient / shorter way to write a simple if else statement. The above sample ternary is the same as:
if ($some_condition) {
echo 'The condition is true!';
} else {
echo 'The condition is false!';
}
However, a ternary for a complex logic quickly becomes unreadable, and is no longer worth the brevity.
echo($result->group_id == 1 ? "Player" : ($result->group_id == 2 ? "Gamemaster" : ($result->group_id == 3 ? "God" : "unknown")));
Even with some attentive formatting to spread it over multiple lines, it's not very clear:
echo($result->group_id == 1
? "Player"
: ($result->group_id == 2
? "Gamemaster"
: ($result->group_id == 3
? "God"
: "unknown")));
Since this would be a common task I would suggest wrapping a switch/case inside of a function call.
function getVocationName($vocation){
switch($vocation){
case 1: return "Sorcerer";
case 2: return 'Druid';
case 3: return 'Paladin';
case 4: return 'Knight';
case 5: return 'Master Sorcerer';
case 6: return 'Elder Druid';
case 7: return 'Royal Paladin';
default: return 'Elite Knight';
}
}
echo getVocationName($result->vocation);
echo ($result ->vocation == 1) ? 'Sorcerer'
: ($result->vocation == 2) ? 'Druid'
: ($result->vocation == 3) ? 'Paladin'
....
;
It’s kind of ugly. You should stick with normal if statements.
How to write a basic PHP Ternary Operator:
($your_boolean) ? 'This is returned if true' : 'This is returned if false';
Example:
$myboolean = true;
echo ($myboolean) ? 'foobar' : "penguin";
foobar
echo (!$myboolean) ? 'foobar' : "penguin";
penguin
A PHP ternary operator with an 'elseif' crammed in there:
$chow = 3;
echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three";
three
But please don't nest ternary operators except for parlor tricks. It's a bad code smell.
I'd rather than ternary if-statements go with a switch-case. For example:
switch($result->vocation){
case 1:
echo "Sorcerer";
break;
case 2:
echo "Druid";
break;
case 3:
echo "Paladin";
break;
case 4:
echo "Knight";
break;
case 5:
echo "Master Sorcerer";
break;
case 6:
echo "Elder Druid";
break;
case 7:
echo "Royal Paladin";
break;
default:
echo "Elite Knight";
break;
}
You wouldn’t: it’s messy and hard to read.
You’re looking for the switch statement in the first case. The second is fine as it is but still could be converted for consistency
Ternary statements are much more suited to boolean values and alternating logic.
To be honest, a ternary operator would only make this worse, what i would suggest if making it simpler is what you are aiming at is:
$groups = array(1=>"Player", 2=>"Gamemaster", 3=>"God");
echo($groups[$result->group_id]);
and then a similar one for your vocations
$vocations = array(
1=>"Sorcerer",
2=>"Druid",
3=>"Paladin",
4=>"Knight",
....
);
echo($vocations[$result->vocation]);
With a ternary operator, you would end up with
echo($result->group_id == 1 ? "Player" : ($result->group_id == 2 ? "Gamemaster" : ($result->group_id == 3 ? "God" : "unknown")));
Which as you can tell, only gets more complicated the more you add to it
In addition to all the other answers, you could use switch. But it does seem a bit long.
switch ($result->vocation) {
case 1:
echo 'Sorcerer';
break;
case 2:
echo 'Druid';
break;
case 3:
echo 'Paladin';
break;
case 4:
echo 'Knight';
break;
case 5:
echo 'Master Sorcerer';
break;
case 6:
echo 'Elder Druid';
break;
case 7:
echo 'Royal Paladin';
break;
default:
echo 'Elite Knight';
break;
}
You could also do:
echo "yes" ?: "no" // Assuming that yes is a variable that can be false.
Instead of:
echo (true) ? "yes" : "no";
PHP 8 (Left-associative ternary operator change)
Left-associative ternary operator deprecation https://wiki.php.net/rfc/ternary_associativity.
The ternary operator has some weird quirks in PHP. This RFC adds a deprecation warning for nested ternary statements. In PHP 8, this deprecation will be converted to a compile time error.
1 ? 2 : 3 ? 4 : 5; // deprecated
(1 ? 2 : 3) ? 4 : 5; // ok
source: https://stitcher.io/blog/new-in-php-74#numeric-literal-separator-rfc

php switch case problem

I am trying to say $level > -100 && $level < 100
$level = 0;
switch($level){
case $level > -100:
break;
case $level < 100:
break;
default:
echo '5';
return null;
}
can you use a switch statement like this.
None of the answers presented so far have explicitly connected the spirit of the original question with a proper switch construction. So, for the record:
switch (true) {
case (($level>-100) && ($level<100)):
echo 'in range one';
break;
case (($level>200) && ($level<300)):
echo 'in range two';
break;
default:
echo 'out of range';
}
There's absolutely nothing wrong with this usage of switch.
When you say switch ($level) you're already comparing the value of $level. Each case can then only check for equality, you can't do comparisons like in your example. You'll have to use an if statement instead:
if ($level > -100 && $level < 100)
; // do nothing; equivalent of break in this case
else
echo '5';
Even simpler, just negate the conditions:
if ($level <= -100 || $level >= 100)
echo '5';
Apart of if/else, another way to do it:
switch (true)
case $level > -100:
break;
case $level < 100:
break;
default:
echo '5';
return null;
}
The other answers are both correct and incorrect at the same time. Incorrect, in that it is possible to do what you want in PHP... change switch($level) to switch(true) and your example will work. Correct, in that it's bad form and if any other programmers see that in your code they'll probably come after you with pitchforks. Its not how the switch statement is intended to be used, and wouldn't work like that in most other languages.
No you can't. Switch does only 'equals' type comparison.
No, you can't. The switch statement needs literals in the case blocks. Use an if statements instead:
if(!($level > -100 && $level < 100))
{
echo '5';
return null;
}
This is one of the reasons people advocating case as a superior solution to if-else are off base. I don't like the syntax or the limitations - if-ifelse-else is much more useful.

PHP CASE statement not working with ZERO values

I don't understand what's happening here. Logically, it doesn't make any sense to me.
<?php
$level = 0;
switch ($level) {
case $level > 80: $answer = 'high'; break;
case $level > 60: $answer = 'moderate-to-high'; break;
case $level > 40: $answer = 'moderate'; break;
case $level > 20: $answer = 'low-to-moderate'; break;
default: $answer = 'low'; break;
}
echo $answer;
?>
When $level == 0, it returns "high". This doesn't make any sense to me. Can someone explain what's happening here?
Change switch ($level) to switch (true) and this will work.
switch statements perform equality tests on the values in the cases. PHP is evaluating your > comparisons, so case $level > 80 becomes case false. false is considered to be equal to 0, so the first case matches.
The quantity after the case needs to be just the value, not a boolean expression. I'm guessing that PHP is evaluating case $level > 80 as case ($level > 80) which is becoming case 0 (i.e., false, since $level is indeed NOT less than 80) and so you're matching the first case.
Are you sure you can do this in php?
I just checked the manual of switch and you have to provide a distinct value.
I think if you can write it again into something like:
$levelDivTwenty = intval($level/20);
$levelDivTwenty = ($levelDivTwenty>4)?4:$levelDivTwenty;
and then case on that.
switch ($levelDivTwenty) {
case 4: //same as $level > 80 before...
case 3: //>60 etc...
}
As others have pointed out you can't use switch like that, but how about defining it like this:
<?
$level = 21;
$answers = array('low', 'low-to-moderate',
'moderate', 'moderate-to-high', 'high');
echo $answers[intval(($level-1)/20)];
?>
Note: If $level = 0, then expression inside intval() will be -1/20, which is less then -1 and therefore will be rounded to 0.
This isn't really how switch is intended to be used. It's to evaluate for a specific value.
Use an If/else if here, instead of complicating your life to make a switch work like one.

Categories