1. PHP의 원리

  • [A] Web browser

  • [B] Web server (Apache) & PHP

이 둘은 어떻게 상호작용하는가?

[A]——index.html—–> [B] Web server

[A]——index.php—–> [B] Web server ——index.php—–> [B] PHP


2. PHP의 Data Type

숫자 & 문자 & (문자 길이)

<!doctype html>

<html>
    <body>
        <?php
        echo 1+1;
        echo 2/2;
        ?>
        
        <?php
        echo "Hello World";
        echo "Hello "."World";
        echo "Hello \"W\"orld";
        ?>
        
		<?php
        echo strlen("Hello World");
        ?>
    </body>
</html>


3. 변수 ( Variable )

<!doctype html>

<html>
    <body>
        <?php
        $name = "LEE";
        echo "abcdefg ".$name." dfjdsfadj ".$name." dfdaf ".$name." fdas";
        ?>
    </body>
</html>


4. URL 파라미터

parameter.php

<!doctype html>

<html>
    <head>
        <meta charset='utf-8'>
    </head>
    <body>
        안녕하세요. <?php echo $_GET['address']; ?>에 사는 <?php echo $_GET['name']; ?>님
    </body>
</html>
  • 127.0.0.1/parameter.php?name=lee&address=서울
  • 127.0.0.1/parameter.php?name=park&address=부산
  • 127.0.0.1/parameter.php?name=kim&address=대전


<!doctype html>

<html>
    <head>
        <meta charset='utf-8'>
    </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>
        abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdev
    </body>
</html>


5. 함수

php new line to <br>

  • string nl2br
<!doctype html>

<html>
    <head>
        <meta charset='utf-8'>
    </head>
    <body>
        <h1>WEB</h1>
        <?php 
        $str = "abcd efadsjfkll 


        akldsjfladsj";
        echo $str;
        ?>
        
        <h2>strlen()</h2>
        <?php 
        echo strlen($str);
        ?>
        
        <h2>nl2br</h2>
        <?php 
        echo nl2br($str);
        ?>
    </body>
</html>


본문 내용이 바뀌게끔!

<!doctype html>

<html>
    <head>
        <meta charset='utf-8'>
    </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('data/.$GET_['id']');
        ?>

    </body>
</html>


6. var_dump

  • print(1)의 output : 1
  • var_dump(1) 의 output : int(1)

  • var_dump(“11”) 의 output : string(2) “11”


7. 조건문

  • isset : 값이 존재한다면 TRUE 아니면 FALSE
<!doctype html>

<html>
    <head>
        <meta charset='utf-8'>
    </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
            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>


<!doctype html>

<html>
    <head>
        <meta charset='utf-8'>
        <title>Loop</title>
    </head>
    <body>
        <h1>while</h1>
        <?php 
        echo '1<br>';
        $i=0;
        while($i<3){
            echo '2<br>';
            $i = $i+1;
        }
		echo '3<br>'
        ?>
    </body>
</html>


8. Array

<!doctype html>

<html>
    <head>
        <meta charset='utf-8'>
        <title>Array</title>
    </head>
    <body>
        <h1>Arrays</h1>
        <?php 
        $coworkers = array('a','b','c','d');
        var_dump(count($coworkers));
        echo $workers[3].'<br>';
        echo $workers[1].'<br>';
        echo $workers[0].'<br>';
        echo $workers[2].'<br>';
        ?>
        
        <?php
        $colors = array('red','green');
        array_push($colors,'blue','yellow');
        print_r($colors);
		?>
        
        
    </body>
</html>

scandir = directory that will be scanned

<!doctype html>

<html>
    <head>
        <meta charset='utf-8'>
    </head>
    <body>
        <h1>WEB</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]</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>


9. 함수

function2.php

<!doctype html>
<html>
    <head>
        <meta charset='utf-8'>
        <title>function</title>
    </head>
    
    <body>
        <h1>Function</h1>
        
        <h2>Basic</h2>
        <?php
        function basic(){
            print("abcdfdsjfasdfadsf");
            print("dfajdklsjfdkjflsf");
        }
        basic();
        ?>
        
        <h2> parameter &amp; argument </h2>
        <?php
        function sum($left, $right){
            print($left,$right);     
            print("<br>");
        }
		sum(2,4);
		sum(4,8);
        ?>
        
        <h2> return </h2>
        <?php
        function sum2($left, $right){
            return $left+$right;
        }        
        print(sum2(2,4));
        file_put_contents('result.txt',sum2(2,4));
        //email('egoing@egoing.net', sum2(2,4));
        //upload('egoing.net', 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 print_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]</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
            print_list();
            ?>
        </ol>
        
        <h2>
            <?php
            print_title();
            ?>
        </h2>

		<?php
        print_description();
        ?>
    </body>
</html>


10. form과 POST

  • 전송 방식 : (default) GET vs POST


form.html

<!doctype html>
<html>
    <body>
        <form action='form.php' method='post'>
	        <p> <input type='text' name='title' placeholder='Title'> </p>
            <p> <textarea name='description'> 여러 내용 입력 가능! </textarea> </p>
    	    <p> <input type='submit'> </p>           
        </form>
    </body>
</html>


form.php

<?php
file_put_contents('data/'.$_POST['title'], $_POST['description']);
?>


11. 글 생성 (C)

<!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
            print_list();
            ?>
        </ol>
        <a href='create.php'>create</a>
        
        <form action='create_process.php' method='post'>
            <p><input type='text' name='title' placeholder='Title'></p>
            <p><textarea name='description' placeholder='Description'></textarea></p>
            <p><input type='submit'></p>
        </form>
        
        <h2>
            <?php
            print_title();
            ?>
        </h2>

		<?php
        print_description();
        ?>
    </body>
</html>


create_process.php

<?php
file_put_contents('data/.'$_POST['title'], $_POST['description']);
header('Location : /index.php?id='.$_POST['title']);
?>


12. 글 수정 (U)

<!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
            print_list();
            ?>
        </ol>
        <a href='create.php'>create</a>
        
        <?php if(isset($_GET['id'])) { ?>
	        <a href="update.php?id=<?php echo $_GET['id']; ?>">update</a>
        <?php } ?>
        
        <h2>
             <form action='update_process.php' method='post'>
                 <input type ='hidden' name='old_title' value="<?=$_GET['id']?>">
                 <p> <input type='text' name='title' placeholder='Title' value="<?php print_title();?>"> </p>
     	         <p> <textarea name='description' placeholder='Description'><?php print_description();?></textarea> </p>
            	 <p> <input type='submit'> </p>
        </form>
        
    </body>
</html>


update_process.php

<?php
rename("data/".$_POST['old_title'],"data/".$_POST['title']);
file_put_contents('data/'.$_POST['title'], $_POST['description']);
header('Location : /index.php?id='.$_POST['title']);
?>


13. 글 삭제 (D)

<!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
            print_list();
            ?>
        </ol>
        <a href='create.php'>create</a>
        
        <?php if(isset($_GET['id'])) { ?>
	        <a href="update.php?id=<?php echo $_GET['id']; ?>">update</a>
	        <a href="delete_process.php?id=<?php echo $_GET['id']; ?>">delete</a>
        	<form action='delete_process.php' method='post'>
                <input type='hidden' name='id' value='<?=$_GET['id']?>'>
                <input type='submit' value='delete'>
	        </form>
        <?php } ?> 
        
        <h2>
            <?php
            print_title();
            ?>
        </h2>

		<?php
        print_description();
        ?>
         
    </body>
</html>


delete_process.php

<?php
unlink('data/'.$_GET['id']);
header('Location: /index.php');
?>

Tags:

Categories:

Updated: