본문 바로가기

Webstoryboy

Category

Explanation

REFERENCE/PHP

[PHP] PHP 조건문

PHP 조건문

조건문


PHP 조건문

조건문

PHP if문

PHP if문 사용방법 예제입니다.

Script
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>php</title>
    <script>
        const num = 100;
        if(num % 2 == 0){
            document.write("짝수입니다.<br>")
        } else {
            document.write("홀수입니다.<br>")
        }
    </script>
</head>
<body>
    <?php
        $num = 100;
        if( $num % 2 == 0 ){
            echo "짝수입니다.";
        } else {
            echo "홀수입니다.";
        }
    ?>
</body>
</html>

 

PHP for문

PHP for문 사용방법 예제입니다.

Script
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>php</title>
    <script>
        //가장 기본적인 for문
        for( let i=1; i<=5; i++){
            document.write( i + ". for문이 실행되었습니다.<br>")
        }
        document.write("<br><br>");

        //배열을 이용한 for문
        const for1 = [1,2,3,4,5];

        for( let i=0; i<for1.length; i++){
            document.write( for1[i] + ". for문이 실행되었습니다.<br>");
        }
        document.write("<br><br>");

        //배열을 이용한 for문2
        const for2 = [1,2,3,4,5,6];
        const for2New = [];

        for( let i=0; i<for2.length; i++){
            if( for2[i] % 2 == 0){
                for2New.push(for2[i]);
            }
        }
        document.write(for2,"<br>")
        document.write(for2New,"<br><br>")

        //forEach
        const for3 = [1,2,3,4,5,6];
        for3.forEach(function(num){
            document.write( num + ". for문이 실행되었습니다.<br>");
        });
        document.write("<br><br>");

        //forEach
        const for4 = [1,2,3,4,5,6];
        const for4New = [];
        for4.forEach(function(num){
            if( num % 2 == 0){
                for4New.push(num);
            }
        });
        document.write(for4,"<br>")
        document.write(for4New,"<br><br>")

        //forEach
        const for5 = [1,2,3,4,5,6];
        const for5New = [];
        for5.forEach( (num) => { if(num % 2 ==0) for5New.push(num)} );

        document.write(for5,"<br>")
        document.write(for5New,"<br><br>")

        //for in
        const for6 = {a:100, b:200, c: 300};
        for( let prop in for6){
            document.write(prop, for6[prop]);
        }
        document.write("<br><br>")

        //for of
        const for7 = [1,2,3];
        for( let value7 of for7){
            document.write(value7);
        }
        document.write("<br><br>")
    </script>
</head>
<body>
    <?php
        for( $i=1; $i<=5; $i++ ){
            echo $i. ". for문 실행되었습니다.<br>";
        }
        echo "<br><br>";

        //forEach
        $for2 = array();

        $for2[0] = array();
        $for2[0]['name'] = '웹스';
        $for2[0]['usrID'] = 'webs';
        $for2[0]['usrPW'] = '1234';

        $for2[1] = array();
        $for2[1]['name'] = '김길록';
        $for2[1]['usrID'] = 'kim';
        $for2[1]['usrPW'] = '1234';

        $for2[2] = array();
        $for2[2]['name'] = '박유미';
        $for2[2]['usrID'] = 'park';
        $for2[2]['usrPW'] = '1234';

        foreach($for2 as $data){
            echo $data['name']."<br>";
        }
    ?>
</body>
</html>

 

PHP for문

PHP for문 사용방법 예제입니다.

Script
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>php</title>
</head>
<body>
    <select name="birthMonth" id="birthMonth">
        <!-- <option value="1" selected>1월</option> -->
        <!--<?php
            for( $i=1; $i<=12; $i++){ ?>
                <option value="<?=$i?>"><?=$i?>월</option>
        <?php } ?> -->

        <?php
            $selectMonth = 4;

            for( $i=1; $i<=12; $i++){ 
                $selected = "";
                if( $i == $selectMonth ){
                    $selected = "selected";
                }
        ?>
            <option value="<?=$i?>" <?=$selected?> ><?=$i?>월</option>
        <?php } ?>
    </select>
</body>
</html>

AD

comments