카테고리 없음

[PHP] include, require

조밈밍 2022. 10. 20. 22:45
<?php
    /* include('불러올 파일 경로')
       오류가 나더라도 뒤에 코드들은 실행된다.
       같은 파일 여러번 실행 가능
    */
    $title = 'include 활용';
    include('inc/header.php');
    // require('불러올 파일 경로'), 오류가 나면 뒤에 있는 코드들은 실행되지 않는다.
    require('inc/functions.php');
?>
    <?php
        $result = sum(10,20);   // 전역변수 gloval variable
        echo $result;
    ?>

    <h2>배열 값 출력하기</h2>
    <?php
        $fruits = [
            'apple',
            'mango',
            'banana',
            'orange'
        ];
        // echo '<pre>';
        // print_r($fruits);
        // echo '</pre>';

        output($fruits);
    ?>

    <hr>
    <h2>정적 변수</h2>
    <!-- 함수가 종료 되면 지역변수는 사라진다
         정적 변수는 함수가 종료되더라도 메모리상에서 사라지지 않는다 -->
    <?php
        increment();
        increment();
        increment();
    ?>
<?php
    include('inc/footer.php');
?>
<?php
    if(!isset($title)){
        $title = '';
    }
?>
<!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><?= $title; ?></title>
</head>
<body>
    <h1><?= $title; ?></h1>
<?php
function sum($x, $y){
    $result = $x + $y; 
    return $result;
}

function output($value){
    echo '<pre>';
    print_r($value);
    echo '</pre>';
}

function increment(){
    static $count = 0; 
    echo "{$count}<br>";
    $count++;
}
?>
</body>
</html>