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>