<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></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>
- 파라미터 = 매개 변수
- 함수를 만들 때는 하나의 함수는 하나의 기능만 가지고 있는 것이 좋다.
- return을 만나는 순간 함수는 종료되고 아래 있는 코드들은 무시된다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>function</title>
</head>
<body>
<h1>Function</h1>
<h2>Basic</h2>
<?php
function basic(){
print("good good good<br>");
print("good good good<br>");
print("good good good<br>");
print("good good good<br>");
}
basic();
?>
<h2>parameter & argument</h2>
<?php
function sum($letf, $right){
print($letf+$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));
?>
</body>
</html>
<?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 pinrt_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
pinrt_list();
?>
</ol>
<h2>
<?php
print_title();
?>
</h2>
<?php
print_description();
?>
</body>
</html>
728x90
'Programming > PHP' 카테고리의 다른 글
PHP - 3 (0) | 2022.06.12 |
---|---|
PHP - 2 (0) | 2022.06.11 |
PHP - 1 (0) | 2022.06.11 |