0
0

More than 1 year has passed since last update.

PHPの論理演算子を使ってみよう!

Posted at

PHPの論理演算子で条件をまとめてみましょう!

index.php
<?php
$score = 30;
$name = 'kana';
if ($score >=20) {
  if($name === 'kana') {
    echo '素晴らしい!' . PHP_EOL;
  }
}

ターミナルで以下を実行すると...

~ $ php index.php

このように表示されました!

~ $ php index.php
素晴らしい!
~ $ 

ではこちらの処理をもっと見やすく、

「&&」と言う論理演算子でまとめて書いていきます。

「&&」は、なおかつやandという意味です!

index.php
$score = 30;
$name = 'kana';
if ($score >=20 && $name === 'kana') {
   echo '素晴らしい!' . PHP_EOL;
}

先程と同じようにターミナルに以下を入力します!

~$ php index.php

先程と同じ結果が出力されました!

~ $ php index.php
素晴らしい!
~ $ 

同じコードでも、論理演算子を使うと見やすくなりました ☆

index.php
index.php
<?php
$score = 30;
$name = 'kana';
if ($score >=20) {
  if($name === 'kana') {
    echo '素晴らしい!' . PHP_EOL;
  }
}
index.php
$score = 30;
$name = 'kana';
if ($score >=20 && $name === 'kana') {
   echo '素晴らしい!' . PHP_EOL;
}
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