PHP의 변수

 

<!DOCTYPE html>
<html>
  <body>
    <h1>Variable</h1>
    <?php
    $a = 10;
    echo $a+1;
     ?>
  </body>
</html>

결과는 11이 나온다

 

이 때 $a는 변수이다

 

PHP의 URL 파라미터

 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    안녕하세요. <?php echo $_GET['name']; ?>님
  </body>
</html>

 

이 코드는 주소창에 따라 인사 상대를 바꾸는 코드이다

 

만약 안녕하세요. A님을 출력하려고 한다면

 

주소창에

 

http://127.0.0.1/parameter.php?name=A

 

라고 입력해야 한다 (파일명은 parameter.php)

 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    안녕하세요. <?php echo $_GET['address']; ?>에 사시는 <?php echo $_GET['name']; ?>님
  </body>
</html>

이렇게 여러개를 할 경우에는

 

http://127.0.0.1/parameter.php?name=A&address=서울

 

&로 붙혀주면 된다

 

<!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>
    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.
  </body>
</html>

위 코드처럼 echo $_GET['id']; 로

 

주소에 따라서 부제목 또한 바뀌는 코드도 만들 수 있게 되었다

'휴지통 > PHP' 카테고리의 다른 글

공부(6)  (0) 2021.09.06
공부(5)  (0) 2021.09.06
공부(4)  (0) 2021.09.06
공부(3)  (0) 2021.09.06
공부(1)  (0) 2021.09.05

+ Recent posts