0

I know that it is possible to nest ternary operators, and I want to use them in this case to save time (at least in the future).

I have a variable that will hold one of four values:

  • "admin"
  • "edit"
  • "wadmin"
  • "wuser"

Each of these is used to determine necessary password lengths based on the user type, 16, 12, 8, and 8, respectively.

I want PHP to echo each of those numbers based on the contents of the variable, in this case named $match

What I have so far is this:

echo $match == "admin" ? "16" : $match == "edit" ? "12" : "8";

But this always echoes 12. How can I rewrite this to properly echo "16", "12", or "8"?

Liftoff
  • 24,717
  • 13
  • 66
  • 119
  • PHP's ternary operator is broken. – elclanrs Aug 17 '13 at 06:04
  • @elclanrs So you're saying this *should* work, even though it doesn't? – Liftoff Aug 17 '13 at 06:05
  • possible duplicate of [Which coding style you use for ternary operator?](http://stackoverflow.com/questions/243217/which-coding-style-you-use-for-ternary-operator) – 웃웃웃웃웃 Aug 17 '13 at 06:05
  • "possible to nest ternary operators" — Possible, but not a good idea, it just makes code hard to read — "and I want to use them in this case to save time" — Ternary operators take more time to plan then simple if statements (so don't save development time) and don't run any faster than an if statement (so don't save any runtime). – Quentin Aug 17 '13 at 06:09
  • I suppose so, but they were still a part of programming I never understood before, and now I do! :) – Liftoff Aug 17 '13 at 06:11

4 Answers4

4

Although it doesn't directly answer the question, you could avoid the nested-ternary entirely:

<?php
$minlen = array(
    "admin" => 16,
    "edit" => 12,
    "wadmin" => 8,
    "wuser" => 8,
);

// Example usage
$match = "admin";
echo $minlen[$match];
?>
dbr
  • 165,801
  • 69
  • 278
  • 343
  • 1
    This is way better than the original approach with ternary operators. It avoids having to fiddle with complicated code, and allows for very easy changing or adding of the values. – Sven Aug 17 '13 at 07:57
1

This will do the trick.

enclose the false condition in brackets / parentheses

echo ($match == 'admin') ? '16' : (($match == 'edit') ? '12' : '8');

or

echo $match == 'admin' ? '16' : ($match == 'edit' ? '12' : '8');
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
  • 1
    Yay! That makes sense too. Dang parentheses! 8 min til I can accept. – Liftoff Aug 17 '13 at 06:09
  • 1
    This is PHP specific because unlike most other languages PHP reads ternary operator left-to-right. It's not worth even doing this IMO, I'd use a `switch` or `elseif`. – elclanrs Aug 17 '13 at 06:13
0

Use nested, i hope help you. You can extend forever ;)

echo $match == "admin" ? "16" : ($match == "edit" ? "12" : ($match == "wadmin" ? "8" : ($match == "wuser" ? "4" : "NONE")));
Bora
  • 10,529
  • 5
  • 43
  • 73
0

use parantheses: echo $match == "admin" ? "16" : ($match == "edit" ? "12" : "8");

yafrani
  • 521
  • 4
  • 9