이안의 평일코딩

[PHP] 함수의 활용 (return) 본문

Back-end/PHP

[PHP] 함수의 활용 (return)

이안92 2021. 4. 2. 11:52
반응형

 PHP 함수

 

return

return이 나오면 함수가 종료되고 나머지 코드는 무시된다.

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>function2</title>
</head>
<body>
    <h1>Function</h1>
    <h2>Basic</h2>
    <?php
        function basic(){
            print("Lorem ipsum dolor1<br>");
            print("Lorem ipsum dolor2<br>");
        }
        basic();
        basic();
    ?>
    <h2>parameter &amp; argument</h2>
    <?php
        function sum($left, $right){
            print($left+$right);
            print("<br>");
        }
        sum(2,4);
        sum(4,6);
    ?>
    <h2>return</h2>
    <?php
        function sum2($left, $right){
            return $left+$right;
        }
        print(sum2(2,4));
        file_put_contents('result.txt', sum2(2,4));
        // sum2()의 결과값이 result.txt 파일에 저장되어 생성된다.
    ?>
</body>
</html>

 

 함수의 활용

 

Before
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>
    <?php
    	if(isset($_GET['id'])){
    	    echo $_GET['id'];
    	} else {
    	    echo "Welcome";
    	}
    ?>
    </title>
</head>
<body>
    <h1><a href="index.php">WEB</a></h1>
    <ol>
        <?php
            $list = scandir('./data');
            $i = 0;
            while($i < count($list)){
                if($list[$i] != '.') {
                    if($list[$i] != '..') {
                        echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
                    }
                }
                $i = $i + 1;
            }
        ?>
    </ol>
    <h2>
        <?php
            if(isset($_GET['id'])){
                echo $_GET['id'];
            } else {
                echo "Welcome";
            }
        ?>
    </h2>
    <?php
    if(isset($_GET['id'])){
        echo file_get_contents("data/".$_GET['id']);
    } else {
        echo "Hello, PHP!";
    }
    ?>
</body>
</html>

위의 코드를 함수를 통해 가독성 있게 바꿔보자.

 

After
<?php
function print_title(){
    if(isset($_GET['id'])){
        echo $_GET['id'];
    } else {
        echo "Welcome";
    }
}

function print_description(){
    if(isset($_GET['id'])){
        echo file_get_contents("data/".$_GET['id']);
    } else {
        echo "Hello, PHP!";
    }
}

function print_list(){
    $list = scandir('./data');
    $i = 0;
    while($i < count($list)){
        if($list[$i] != '.') {
            if($list[$i] != '..') {
                echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
            }
        }
        $i = $i + 1;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>
        <?php
        print_title();
        ?>
    </title>
</head>
<body>
    <h1><a href="index.php">WEB</a></h1>
    <ol>
        <?php
        print_list();
        ?>
    </ol>
    <h2>
        <?php
        print_title(); 
        ?>
    </h2>
    <?php
    print_description();
    ?>
</body>
</html>

함수를 이용하면 정리정돈되어 훨씬 가독성이 좋아지며 재사용도 가능해진다.

반응형
Comments