比較演算子と論理演算子
比較演算子(comparison operator)とは
- 大小関係あるいは同値関係を比較して真偽値を返す演算子
- 左辺と右辺を比較し、その条件が成立すれば「真」を、成立しなければ「偽」を返す働きをしまする
比較演算子 | 意味 |
---|---|
== | 左辺と右辺は等しい |
!= | 左辺と右辺は等しくない |
< | 左辺より右辺のほうが大きい |
> | 左辺より右辺のほうが小さい |
<= | 左辺より右辺のほうが大きいか、または等しい |
>= | 左辺より右辺のほうが小さいか、または等しい |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>比較演算子</title> </head> <body> <?php $a = 1; if ( $a == 1 ) { print ( '等しい' ); } else { print ( '等しくない' ); } ?> </body> </html>
elseifで記述
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>比較演算子</title> </head> <body> <?php $a = 1; if ( $a == 2 ) { print ( '2と等しい' ); } elseif ( $a == 3 ) { print ( '3と等しい' ); } else { print ( '2でも3でもない' ); } ?> </body> </html>