LinkedNestBlog

PHP 문법 : 논리연산자

by 이진원Dev
PHP 문법 : 논리연산자


ref.] 생활코딩 > PHP > PHP 기본수업 > 조건문
https://opentutorials.org/course/62/5122

  • 논리연산자의 종류
    •  AND
      • 설명 
        • 좌항과 우항이 참(true)일 때 참.
        • '&&'과 같다.
      • 소스코드
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        <?php
        if (1 and 1) {
            echo 1;
        }
        if (1 and 0) {
            echo 2;
        }
        if (0 and 1) {
            echo 3;
        }
        if (0 and 0) {
            echo 4;
        }
        ?>
        cs
      • 결과
        1
      • 응용예제(condition.php)
        1
        2
        3
        4
        5
        6
        7
        <?php
        if($_GET['name'] === 'jwlee' && $_GET['pwd'] === '1234'){
            echo 'matched';
        }else{
            echo 'not matched';
        }
        ?>
        cs
    •  OR
      • 설명 
        • 좌항과 우항 중 하나라도 참(true)이면 참
        • '||'과 같다.
      • 소스코드
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        <?php
        if(true or true){
            echo 1;
        }
        if(true or false){
            echo 2;
        }
        if(false or true){
            echo 3;
        }
        if(false or false){
            echo 4;
        }
        ?>
        cs
      • 결과
        2
        3
        4
      • 응용예제(condition.php)
        1
        2
        3
        4
        5
        6
        7
        <?php
        if($_GET['name'] === 'jwlee' || $_GET['pwd'] === '1234'){
            echo 'matched';
        }else{
            echo 'not matched';
        }
        ?>
        cs
    •  !(NOT)
      • 설명 
        • not
        • 부정의 의미.  boolean 의 값을 역전. !true는 false의 의미
      • 소스코드
      • 결과
  • Ref.] Boolean 의 대체
    • true == 0이상의 숫자
    • false == 0
      • 0외에 값이 없는 배열, 빈 문자열, null, 문자 '0' 도 0으로 간주
    • 예제
      • 소스코드
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        <?php
        if (1 and 1) {
            echo 1;
        }
        if (1 and 0) {
            echo 2;
        }
        if (0 and 1) {
            echo 3;
        }
        if (0 and 0) {
            echo 4;
        }
        ?>
        cs
      • 결과
        1