[PHP] empty() / isset() / include

2022. 5. 26. 09:08·Stack/PHP

empty() 빈 값인지 아닌지를 체크 - 비어있으면 true 비어있지 않으면 false 반환

isset() 존재하는지 아닌지를 체크 - 존재한다면 true 존재하지 않는다면 false 반환

 


ex08_empty_isset.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
        $testStr = 0;         // int
        $testStr2 = 100;      // int
        $testStr3 = "0";      // str
        $testStr4 = "100";    // str
        $testStr5 = "abcd";   // str
        $testStr6 = "";       // str
        $testStr7 = null;     // null
        $testStr8 = true;     // bool
        $testStr9 = false;    // bool

        // 1. int 0
        echo empty($testStr) ? "int 0 empty는 Y <br/>" : "int 0 empty는 N <br/>";   // Y
        echo isset($testStr) ? "int 0 isset은 Y <br/>" : "int 0 isset은 N <br/>";   // Y

        // 2. int 0 외의 숫자
        echo empty($testStr2) ? "int 100 empty는 Y <br/>" : "int 100 empty는 N <br/>";   // N
        echo isset($testStr2) ? "int 100 isset은 Y <br/>" : "int 100 isset은 N <br/>";   // Y

        // 3. str "0"
        echo empty($testStr3) ? "str 0 empty는 Y <br/>" : "str 0 empty는 N <br/>";   // Y
        echo isset($testStr3) ? "str 0 isset은 Y <br/>" : "str 0 isset은 N <br/>";   // Y
    
        // 4. str "100"
        echo empty($testStr4) ? "str 100 empty는 Y <br/>" : "str 100 empty는 N <br/>";   // N
        echo isset($testStr4) ? "str 100 isset은 Y <br/>" : "str 100 isset은 N <br/>";   // Y

        // 5. str "abcd"
        echo empty($testStr5) ? "str abcd empty는 Y <br/>" : "str abcd empty는 N <br/>";   // N
        echo isset($testStr5) ? "str abcd isset은 Y <br/>" : "str abcd isset은 N <br/>";   // Y

        // 6. str ""
        echo empty($testStr6) ? "str empty는 Y <br/>" : "str empty는 N <br/>";   // Y
        echo isset($testStr6) ? "str isset은 Y <br/>" : "str isset은 N <br/>";   // Y

        // 7. null
        echo empty($testStr7) ? "null empty는 Y <br/>" : "null empty는 N <br/>";   // Y
        echo isset($testStr7) ? "null abcd isset은 Y <br/>" : "null isset은 N <br/>";   // N

        // 8. true
        echo empty($testStr8) ? "true empty는 Y <br/>" : "true empty는 N <br/>";   // N
        echo isset($testStr8) ? "true isset은 Y <br/>" : "true isset은 N <br/>";   // Y

        // 9. false
        echo empty($testStr9) ? "false empty는 Y <br/>" : "false empty는 N <br/>";   // Y
        echo isset($testStr9) ? "false isset은 Y <br/>" : "false isset은 N <br/>";   // Y
    ?>
</body>
</html>

 


 

include

다른 PHP 파일을 코드 안으로 불러와서 사용

include "파일경로"

 

index.php

<?php
    include "header.php";
?>
        <section>
            메인페이지
        </section>
<?php
    include "footer.php";
?>

 

 

header.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="wrap">
        <header>
            <h1><a href="index.php">green</a></h1>
            <nav>
                <ul>
                    <li><a href="sub.php">menu1</a></li>
                    <li><a href="#">menu2</a></li>
                    <li><a href="#">menu3</a></li>
                    <li><a href="#">menu4</a></li>
                </ul>
            </nav>
        </header>

 

footer.php

<footer>
        푸터입니다.
</footer>
</div>
</body>
</html>

 

sub.php

<?php
    include 'header.php';
?>
<section>
    서브페이지입니다.
</section>
<?php
    include 'footer.php';
?>

'Stack > PHP' 카테고리의 다른 글

[PHP] MySQL 연동하기  (0) 2022.05.26
[PHP] file 관련 함수를 이용한 Blog  (0) 2022.05.26
[PHP] file  (0) 2022.05.25
[PHP] get / post 전송 방식  (0) 2022.05.25
[PHP] 제어문  (0) 2022.05.25
'Stack/PHP' 카테고리의 다른 글
  • [PHP] MySQL 연동하기
  • [PHP] file 관련 함수를 이용한 Blog
  • [PHP] file
  • [PHP] get / post 전송 방식
7ingout
7ingout
  • 7ingout
    Hello, 7ingout world!
    7ingout
  • 전체
    오늘
    어제
    • 분류 전체보기 (205)
      • Project (5)
      • Stack (173)
        • React (40)
        • JavaScript (50)
        • TypeScript (14)
        • HTML (11)
        • CSS (31)
        • Spring (9)
        • PHP (15)
        • SQL (3)
        • Python (0)
      • ETC (9)
      • Design (13)
        • Illustrator (6)
        • Photoshop (7)
      • Articloid (4)
        • 7ingout (4)
  • 공지사항

    • ☻
  • 인기 글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
7ingout
[PHP] empty() / isset() / include
상단으로

티스토리툴바