0
0

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 3 years have passed since last update.

php 三項演算子

Posted at

三項演算子

if文をすっきり書けるので便利ですよね。
実装してて気になったので、備忘録をかねて

①基本

条件式 ? 式1 : 式2
$lang = 'En';

$lang == 'Jp'  ? $msg = 'こんにちは' : $msg = 'Hello';
print $msg;
'Hello'

②代入+基本

代入式 + 条件式 ? 式1 式2;
$lang = 'Jp';

$msg = $lang == 'Jp'  ? 'こんにちは' : 'Hello';
print $msg;
'こんにちは'

これなら式1、式2にそれぞれに式を書く手間が省ける

今まで①パターンで書いてたけど、次からは②のパターンを使用してみようっと
なにかの参考になれば、幸いです。

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?