웹기초/생활코딩 WEB 2 - PHP

[웹기초] 생활코딩 PHP 9 ~ PHP 17

renne 2022. 7. 11. 21:40

var_dump();

 - 입력값과 데이터타입을 같이 출력하는 함수

<?php
  print('<h2>1 == 1</h2>');
  var_dump(1 == 1);
  print('<h2>1 > 2</h2>');
  var_dump(1 > 2);
?>

isset();

 - 변수가 설정되었는지 확인하는 함수

<?php
$a = '';
$b = 'test';
var_dump(isset($a));	//TRUE
var_dump(isset($b));	//TRUE

unset($a);
$c = NULL;
var_dump(isset($a));	//FALSE
var_dump(isset($c));	//FALSE
?>

 

조건문 if

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <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>
      <li><a href="index.php?id=PHP">PHP</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 "Welcome to WEB";
      }
    ?>
  </body>
</html>

 

반복문 while

<?php
  echo '1 ';
  $i = 0;
  while ($i < 3){
    echo '2 ';
    $i += 1;
  }
  echo '3 ';
?>

 

배열 array

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <?php
      $black = array('Bellatrix', 'Narcissa', 'Regulus');	//배열 생성
      echo $black[0].'<br>';	//배열의 원소 출력
      var_dump(count($black));	//배열의 원소의 개수 출력
      print('<br>');
      array_push($black, 'Sirius'); //배열에 원소 추가
      var_dump($black);
    ?>
  </body>
</html>

 

scandir();

 - List files and directories inside the specified path

<?php
    $list = scandir('./data');
    var_dump($list);
?>

 

<!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>";
            }
          }
          $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 "Welcome to WEB";
      }
    ?>
  </body>
</html>