This is what I wrote :
$Myprovince = (
($province == 6) ? "city-1" :
($province == 7) ? "city-2" :
($province == 8) ? "city-3" :
($province == 30) ? "city-4" : "out of borders"
);
But for every field I got the valuecity-4
. I want to use ternary operators instead ofswitch/if
because I want to experiment and see how it would be done.
What's the problem with this code?
Others have already suggested the right way of doing it but if you really want to use ternary operator you need to use parenthesis as:
$province = 7;
$Myprovince = (
($province == 6) ? "city-1" :
(($province == 7) ? "city-2" :
(($province == 8) ? "city-3" :
(($province == 30) ? "city-4" : "out of borders")))
);
The ternary operator is evaluated from left to right. So if you don't group the expressions properly, you will get an unexpected result.
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.
Your code actually is evaluated as:
(
(
(
$province == 6 ? "city-1" : $province == 7
) ? "city-2" :
$province == 8
) ? "city-3" : $province == 30
) ? "city-4" : "out of borders";
where it should be
$province == 6 ? "city-1" : (
$province == 7 ? "city-2" : (
$province == 8 ? "city-3" : (
$province == 30 ? "city-4" : "out of borders"
)
)
);
This code might look fine but someone will read it and they will need more time than they should to understand what this code is doing.
You would be better off with something like this:
$map = array( 6 = >'city-1',
7 => 'city-2',
8 => 'city-3',
30 => 'city-4');
$Myprovince = "out of borders";
if(array_key_exists($province, $map)) {
$Myprovince = $map[$province];
}
Or as @Jonah mentioned in his comment:
$Myprovince = isset($map[$province]) ? $map[$province] : 'out of borders';
Don't abuse the ternary operator for that sort of thing. It makes debugging near impossible to follow. Why not do something like
switch($province) {
case 6: $Myprovince = "city-1"; break;
case 7: ...
}
or simply some chained if/then/else
if ($province == 6) {
$Myprovince = "city-1";
} elseif ($province = ...) {
...
}
Some people have suggested using a switch statement or an if/else statement. But I would use an array instead, to make it easier to maintain and easier to read:
$provinces = array (
6 => 'city-1',
7 => 'city-2',
8 => 'city-3',
30 => 'city-4'
);
// then you can call:
$Myprovince = isset($provinces[$province]) ? $provinces[$province] : 'out of borders';
The code will probably eventually be easier to manage. Maybe you'll want to add those province-to-city mappings from database one day.. etc.. That will be hard to maintain with a bunch of switch/case statements.
I understand this is a question about PHP, but since this is just an educational exercise anyways I thought you might be interested in learning that Ruby and Javascript actually behave the way you expect.
Ruby:
ree-1.8.7-2012.02 :009 > def foo x
ree-1.8.7-2012.02 :010?> x == 1 ? "city 1" : x == 2 ? "city 2" : "out of borders"
ree-1.8.7-2012.02 :011?> end
=> nil
ree-1.8.7-2012.02 :012 > foo 1
=> "city 1"
ree-1.8.7-2012.02 :013 > foo 2
=> "city 2"
ree-1.8.7-2012.02 :014 > foo 3
=> "out of borders"
Javascript:
> function f(x) { return x == 1 ? "city 1" : x == 2 ? "city 2" : "out of borders"; }
undefined
> f(1)
"city 1"
> f(2)
"city 2"
> f(3)
"out of borders"
Try with some more parenthesis :
$Myprovince = (
($province == 6) ? "city-1" :
(($province == 7) ? "city-2" :
(($province == 8) ? "city-3" :
(($province == 30) ? "city-4" : "out of borders"
))));
Your code has a problem with the ternary operator priority.
But I think you should really drop this operator and try to use aswitch
instead.
I got myself into the same problem today. The others already giving acceptable solutions. Mine just an emphasis to one liner ifs. More readable in my opinion.
if ($province == 6) $Myprovince = 'city-1';
elseif ($province == 7) $Myprovince = 'city-2';
elseif ($province == 8) $Myprovince = 'city-3';
elseif ($province == 30) $Myprovince = 'city-4';
else $Myprovince = 'out of borders';
Use switch instead. Ternary operators really shouldn't be used for more than single conditions, as they quickly become very difficult to understand.
switch ($province) {
case 6:
$Myprovince = 'city-1';
break;
case 7:
$Myprovince = 'city-2';
break;
case 8:
$Myprovince = 'city-3';
break;
case 30:
$Myprovince = 'city-4';
break;
default:
$Myprovince = 'out of borders';
}
I think the more readable way to write nested ternary operators in PHP is like this:
$myprovince =
$province == 6 ? "city-1" : (
$province == 7 ? "city-2" : (
$province == 8 ? "city-3" : (
$province == 30 ? "city-4" : "out of borders" )));
All you need to do is count the number of opening parenthesis ((
) and add the same number of closing parenthesis ()
) at the end of the last line.
Another option is to use one-line if/elseif/else, as already suggested - however, I would format them visually like this for even more readability:
if ($province == 6) $myprovince = "city-1";
elseif ($province == 7) $myprovince = "city-2";
elseif ($province == 8) $myprovince = "city-3";
elseif ($province == 30) $myprovince = "city-4";
else $myprovince = "out of borders";
Another solution for this was introduced with the in PHP 8:
$Myprovince = match ($province) {
6 => "city-1",
7 => "city-2",
8 => "city-3",
30 => "city-4",
default => "out of borders",
};
It's essentially just a less-verboseswitch
statement that's ideal for simple assignment. Multiple conditions can be added as well:
$Myprovince = match ($province) {
4, 5, 6 => "city-1",
7, 9, 10 => "city-2",
8 => "city-3",
30 => "city-4",
default => "out of borders",
};
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.