WEB/Server

PHP ๋‚ด์šฉ์ •๋ฆฌ - 2

kite707 2021. 1. 14. 17:19

https://opentutorials.org/course/3130/19335

 

PHP์˜ URL ํŒŒ๋ผ๋ฏธํ„ฐ - ์ƒํ™œ์ฝ”๋”ฉ

์ˆ˜์—…์†Œ๊ฐœ ์—ฌ๊ธฐ์„œ๋Š” php ์—ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ž…๋ ฅ์œผ๋กœ์„œ URL parameter ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค.  ์•ˆ๋…•ํ•˜์„ธ์š”. ์— ์‚ฌ์‹œ๋Š” ๋‹˜ WEB HTML CSS JavaScript Lorem ipsum dolor sit amet, consectet

opentutorials.org

์ด๋ฒˆ ๊ณต๋ถ€๋Š” ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ ์‹œ์ž‘์ด๋‹ค.

PHP์˜ URLํŒŒ๋ผ๋ฏธํ„ฐ

๋จผ์ € index.php๋’ค์— ?๋ณ€์ˆ˜๋ช…=๋ณ€์ˆ˜๊ฐ’ ์„ ์ž…๋ ฅํ•œ๋‹ค.

์ฝ”๋“œ์—์„œ ์ด๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•œ๋‹ค.

<?php $_GET['๋ณ€์ˆ˜๋ช…'];?>

์‹ค์ œ ์‚ฌ์šฉ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

<!doctype html>
<html>
    <body>
        ์•ˆ๋…•ํ•˜์„ธ์š”. <?php echo $_GET['name'];?>๋‹˜ <!--๊ฐ€์ ธ์˜จ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด echo๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.-->
    </body>
</html>

์œ„์— ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์— ๋”ฐ๋ผ ์ถœ๋ ฅ๊ฐ’๋„ ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

PHP์˜ ํ•จ์ˆ˜

nl2brํ•จ์ˆ˜

nl2br ( string $string , bool $use_xhtml = true )
//๋‘๋ฒˆ์งธ ์ธ์ž๋Š” XHTML ํ˜ธํ™˜ ์ค„๋ฐ”๊ฟˆ์˜ ์‚ฌ์šฉ์—ฌ๋ถ€. ์•„๋ฌด๊ฒƒ๋„ ์•ˆ์“ฐ๋ฉด True

์‚ฌ์šฉ ์˜ˆ์‹œ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $str="HTML is the simplest form of technology is the development and use of basic tools. The prehistoric discovery of how to control fire and the later Neolithic Revolution increased the available sources of food, and the invention of the wheel helped humans to travel in and control their environment.
    
    Developments in historic times, including the printing press, the telephone, and the Internet, have lessened physical barriers to communication and allowed humans to interact freely on a global scale.";
    echo nl2br($str);
    ?>
    
</body>
</html>

file-get-contentํ•จ์ˆ˜

file_get_contents("ํŒŒ์ผ ๊ฒฝ๋กœ")

 

์‚ฌ์šฉ์˜ˆ์‹œ

์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž.

<!doctype html>
<html>
    <body>
        <h1>WEB</h1>
        <ol>
            <li><a href="index.php?id=HTML">HTML</a></li>
            <li><a href="index.php?id=CSS">CSS</a></li>
            <li><a href="index.php?id=JavaScript">JavaScript</a></li>
        </ol>
        <h2>
            <?php
                echo $_GET['id'];
            ?>
        </h2>
    </body>
</html>

ํ˜„์žฌ ํŒŒ์ผ์ด ์•„๋ž˜์™€ ๊ฐ™์ด ์กด์žฌํ•˜๋ฏ€๋กœ ํŒŒ์ผ ๊ฒฝ๋กœ๋Š” "data/id๊ฐ’(HTML or CSS or JavaScript)"๊ฐ€ ๋œ๋‹ค. 

์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์ž.

<!doctype html>
<html>
    <body>
        <h1>WEB</h1>
        <ol>
            <li><a href="index.php?id=HTML">HTML</a></li>
            <li><a href="index.php?id=CSS">CSS</a></li>
            <li><a href="index.php?id=JavaScript">JavaScript</a></li>
        </ol>
        <h2>
            <?php
                echo $_GET['id'];
            ?>
        </h2>
        <?php
        echo file_get_contents("data/".$_GET['id']);
        ?>
    </body>
</html>

์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

var_dumpํ•จ์ˆ˜

var_dump(์ถœ๋ ฅํ•  ๋‚ด์šฉ);

์‚ฌ์šฉ ์˜ˆ์‹œ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>var_dump(1)</h3>
    <?php
    var_dump(1);
    ?>
    <h3>var_dump("H")</h3>
    <?php
    var_dump("H");
    ?>
    <h3>var_dump(1==1)</h3>
    <?php
    var_dump(1==1);
    ?>
</body>
</html>

 

์กฐ๊ฑด๋ฌธ์˜ ํ™œ์šฉ

์กฐ๊ฑด๋ฌธ ๋ฌธ๋ฒ•์€ ๊ฐ™์•„์„œ ๋ฐ”๋กœ ํ™œ์šฉ์œผ๋กœ ๋„˜์–ด์™”๋‹ค.

์ด๋ฒˆ ์‹ค์Šต์—๋Š” issetํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.

issetํ•จ์ˆ˜

isset(๋ฌธ์ž์—ด);
$a='';
$b='hihi';
var_dump(isset($a)); //TRUE
var_dump(isset($b));  //TRUE

unset($a);
var_dump(isset($a)); //FALSE

์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์•„๊นŒ ๋งŒ๋“  ํŽ˜์ด์ง€๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

<!doctype html>
<html>
    <body>
        <h1><a href="index.php">WEB</a></h1>
        <ol>
            <li><a href="index.php?id=HTML">HTML</a></li>
            <li><a href="index.php?id=CSS">CSS</a></li>
            <li><a href="index.php?id=JavaScript">JavaScript</a></li>
        </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>