0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【PHP】【javascript】三項演算子の入れ子 switch文が不要になる?

Last updated at Posted at 2023-08-02

コード

<?php
$test = 1;
echo ($test === 2 ? '○' : ($test === 1 ? '◎' : '×') );

echo ($test === 2 ? '○' 
   : ($test === 1 ? '◎' 
   : '×'
));
<?php
$test = 3;
echo ($test === 2 ? '○' 
   : ($test === 1 ? '◎' 
   : ($test === 3 ? 'a' 
   : 'b'
)));


$test = 77;
echo ($test === 2 ? '○' 
   : ($test === 1 ? '◎' 
   : ($test === 3 ? 'a' 
   : ($test === 4 ? 'c' 
   : 'b'
))));

javascript

process.stdin.resume();
process.stdin.setEncoding('utf8');
test = 1;
console.log(test === 2 ? '' : (test === 1 ? '' : '×') );

結果

javascript 入れ子

process.stdin.resume();
process.stdin.setEncoding('utf8');
let x = 5;

let a = x < 10 ? 1
      : x < 20 ? 2
      : x < 30 ? 3
      : 4;
      
console.log(a);

結果

1
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?