2030 Engineer
[PHP] PHP 함수 활용 본문
간단한 계산을 하는 것을 이미 만들어져 있는 함수를 이용해서 활용한다면 편리하다.
구글에다 "php strlen" 이런 식으로 검색을 하면 필요한 함수를 쉽게 찾을 수 있다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>function</title>
</head>
<body>
<h1>function</h1>
<?php
$str = "A function is a process or a relation that associates each element x of a set X, the domain of the function, to a single element y of another set Y (possibly the same set), the codomain of the function.";
echo $str;
?>
<h2>strlen()</h2>
<?php
echo strlen($str);
?>
</body>
</html>
위는 strlen()에 대한 간단한 활용 예이다.
만약 코드를 입력할 때 엔터 두 번으로 줄을 띄워놓고 싶은데 <br>을 사용해서 띄워놔도 되지만
php를 활용하고 싶다면
"php new line" 이런 식으로만 쳐도 함수가 나온다.
구글에 치면 nl2br이라는 명령어가 나올텐데 이 명령어를 사용하면
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>function</title>
</head>
<body>
<h1>function</h1>
<?php
$str = "A function is a process or a relation that associates each element x of a set X,
the domain of the function, to a single element y of another set Y (possibly the same set), the codomain of the function.";
echo $str;
?>
<h2>strlen()</h2>
<?php
echo strlen($str);
?>
<h2>nl2br</h2>
<?php
echo nl2br($str);
?>
</body>
</html>
이렇게 자동으로 <br>을 활용하주는 결과를 얻을 수 있다.
또 한가지 예를 보자
전의 코드에서 파일의 제목대로 내용이 바뀌는 코드를 작성한 바 있다.
이를 PHP를 활용한다면 어떨까?
<!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>
</ol>
<h2>
<?php
echo $_GET['id'];
?>
</h2>
<?php
echo file_get_contents($_GET['id']);
?>
</body>
</html>
file_get_contents 라는 명령어를 구글링을 통해 알았다.
구글링을 하면 사용법까지 자세하게 나와있어서 활용하기 좋다.
위의 경우에는 이 함수안에 경로를 지정해주면 자동으로 연결을 해준다.
각 HTML, CSS, Javascript 라는 파일에는 텍스트만 존재하며 결과 적으로는
이렇게 된다.
함수를 활용하면 이런 기능들을 활용하기 좋다.
'WEB > PHP' 카테고리의 다른 글
[PHP] Cross site scripting (XSS) (0) | 2020.05.18 |
---|---|
[PHP] 리팩토링 (require) (0) | 2020.05.14 |
[PHP] CRUD (0) | 2020.05.13 |
[PHP] 제어문, 조건문, 반복문 (0) | 2020.05.08 |
[PHP] PHP의 원리 (0) | 2020.04.23 |
Comments