PHP λ΄μ©μ 리 - 2
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>