사자자리
[웹기초] 생활코딩 PHP 4 ~ PHP 8 본문
웹브라우저와 서버의 상호작용
1. 웹브라우저의 주소창에 index.html 입력
2. 웹서버가 확장자가 html임을 확인하고, 본인이 직접 처리할 수 있다는 것을 알게 됨
3. 웹서버가 서버 컴퓨터의 HDD나 SSD에 설치된 htdocs 디렉토리에서 index.html 파일을 읽어서 웹브라우저에게 전송
4. 웹브라우저가 전송받은 코드의 내용을 해석해서 화면에 표시
1. 웹브라우저의 주소창에 index.php 입력
2. 웹서버가 확장자가 php임을 확인하고, 본인이 직접 처리할 수 없다는 것을 알게 됨
3. 웹서버가 index.php 파일에 대한 처리를 PHP에게 위임
4. PHP가 htdocs 디렉토리 내의 index.php 파일을 열어서 php의 문법에 따라서 해석하고 html 파일을 생산
5. 만들어진 html 파일을 웹서버를 통해 웹브라우저에 전송
php 파일 예시
<!DOCTYPE html>
<html>
<body>
<?php //php의 시작
echo date('Y-m-d H:i:s'); //현재 시간
?> //php의 끝
</body>
</html>
- reload할 때마다 시간이 동적으로 변화한다.
- <?php와 ?>의 안쪽은 php 문법에 따라 처리되고, 기호의 바깥쪽은 php 문법이 아니다.
PHP 데이터 타입
https://www.php.net/manual/en/language.types.intro.php
PHP: Introduction - Manual
www.php.net
echo
- 데이터의 형식을 막론하고 화면에 무언가를 표시하는 방식
<!DOCTYPE html>
<html>
<body>
<?php
echo 1;
echo 2.2;
print(1);
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
echo 1 + 1;
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>String</h1>
<?php
echo 'Hello World';
echo "Hello World";
?>
<h2>Concatenation Operator</h2>
<?php
echo "Hello"." World"; //문자열을 결합하는 연산자
?>
<h2>String Length</h2>
<?php
$str = "Hello World";
echo strlen($str); //문자열의 길이를 출력하는 함수
?>
</body>
</html>
PHP 변수
<!DOCTYPE html>
<html>
<body>
<h1>Variable</h1>
<?php
$name = "Black"; //변수
echo "Lorem ipsum dolor sit amet, consectetur ".$name." adipisicing elit, sed do eiusmod tempor incididunt ut labore"
?>
</body>
</html>
PHP URL 파라미터
- $_GET[' '];
- 어플리케이션의 입력값을 URL을 통해 준다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
안녕하세요. <?php echo $_GET['name']; ?>님.
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
안녕하세요, <?php echo $_GET['name']; ?>님.
배정된 기숙사는 <?php echo $_GET['house']; ?>입니다.
</body>
</html>
PHP 함수
strlen
- 문자열의 길이를 출력하는 함수
<?php
$str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
echo strlen($str); //446 출력
?>
nl2br
- 문자열의 모든 줄바꿈 앞에 HTML 줄바꿈 태그를 삽입하는 함수
<?php
$str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
echo nl2br($str);
?>
file_get_contents
- 전체 파일을 문자열로 읽어들이는 함수
- 보안에 문제가 있다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<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>
<li><a href="index.php?id=PHP">PHP</a></li>
</ol>
<h2>
<?php
echo $_GET['id'];
?>
</h2>
<?php
echo file_get_contents("data/".$_GET['id']);
?>
</body>
</html>
'웹기초 > 생활코딩 WEB 2 - PHP' 카테고리의 다른 글
[웹기초] 생활코딩 PHP 18 ~ PHP 30 (0) | 2022.07.16 |
---|---|
[웹기초] 생활코딩 PHP 9 ~ PHP 17 (0) | 2022.07.11 |
[웹기초] 생활코딩 PHP 1 ~ PHP 3 (0) | 2022.07.11 |