Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ch. 3. PHP (이 강의 내용의 대부분 예들은 w3schools. com/php/default

Similar presentations


Presentation on theme: "Ch. 3. PHP (이 강의 내용의 대부분 예들은 w3schools. com/php/default"— Presentation transcript:

1 Ch. 3. PHP (이 강의 내용의 대부분 예들은 http://www. w3schools. com/php/default

2 PHP? PHP?: “PHP: Hypertext Preprocessor “ PHP
First version in 1994 by Rasmus Lerdorf Server-sided scripting language integrats with popular DBs & supports number of major protocols (POP3, IMAP, LDAP, etc.) fast forgiving C like free

3 내려받기와 설치 내려 받기: http://php.net 설치: Web server가 먼저 설치 되어야 함
(지세한 과정은 생략)

4 Syntax Basic <?php {php coding} ?>
<?php echo "Hello World!"; ?>

5 Comments <?php // This is a single-line comment # This is also a single-line comment /* This is a multiple-lines comment block that spans over multiple lines */ // You can also use comments to leave out parts of a code line $x = 5 /* + 15 */ + 5; echo $x; ?>

6 Variables $로 시작: $name 대소문자 구분 이름을 숫자로 시작할 수 없음 이름에 A-z, 0-9, _ 만 사용
<?php $txt = "Hello world!"; $x = 5; $y = 10.5; ?>

7 <?php $txt = "W3Schools.com"; echo "I love $txt!"; ?>
<?php $x = 5; $y = 4; echo $x + $y; ?>

8 <?php $x = 5; // global scope function myTest() {     $x=8; // local scope     echo "<p>Variable x inside function is: $x</p>"; } myTest(); echo "<p>Variable x outside function is: $x</p>"; ?>

9 <?php $x = 5; $y = 10; function myTest() {     global $x, $y;     $y = $x + $y; } myTest(); echo $y; // outputs 15 ?>

10 <?php function myTest() {     static $x = 0;     echo $x;     $x++; } myTest(); echo “<br>”; myTest(); echo “<br>”; myTest(); echo “<br>”; ?>

11 Data Types Types 선언할 필요 없이 상황에 따라 자동으로 지정 String Integer
Float (floating point numbers - also called double) Boolean Array Object NULL Resource 선언할 필요 없이 상황에 따라 자동으로 지정

12 <. php $x = 5985; $y=10. 36; $z=“hello
<?php $x = 5985; $y=10.36; $z=“hello!”; var_dump($x); echo “<br>”; var_dump($y); echo “<br>”; var_dump($z); ?>

13 Strings <?php echo strlen("Hello world!"); // outputs 12 echo str_word_count("Hello world!"); // outputs 2 echo strrev("Hello world!"); // outputs !dlrow olleH echo strpos("Hello world!", "world"); // outputs 6 echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly! ?>

14 Constants define(name, value, case-insensitive) global
<?php define("GREETING", "Welcome to W3Schools.com!", true); echo greeting; ?>

15 Operators Arithmatic Operator Name Example Result + Addition $x + $y
Sum of $x and $y - Subtraction $x - $y Difference of $x and $y * Multiplication $x * $y Product of $x and $y / Division $x / $y Quotient of $x and $y % Modulus $x % $y Remainder of $x divided by $y ** Exponentiation $x ** $y Result of raising $x to the $y'th power (Introduced in PHP 5.6)

16 Assignment Assignment Same as... Description x = y
The left operand gets set to the value of the expression on the right x += y x = x + y Addition x -= y x = x - y Subtraction x *= y x = x * y Multiplication x /= y x = x / y Division x %= y x = x % y Modulus

17 Comparison Operator Name Example Result == Equal $x == $y
Returns true if $x is equal to $y === Identical $x === $y Returns true if $x is equal to $y, and they are of the same type != Not equal $x != $y Returns true if $x is not equal to $y <> $x <> $y !== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type > Greater than $x > $y Returns true if $x is greater than $y < Less than $x < $y Returns true if $x is less than $y >= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y <= Less than or equal to $x <= $y Returns true if $x is less than or equal

18 Increment/decrement Operator Name Description ++$x Pre-increment
Increments $x by one, then returns $x $x++ Post-increment Returns $x, then increments $x by one --$x Pre-decrement Decrements $x by one, then returns $x $x-- Post-decrement Returns $x, then decrements $x by one

19 Logical Operator Name Example Result and And $x and $y
True if both $x and $y are true or Or $x or $y True if either $x or $y is true xor Xor $x xor $y True if either $x or $y is true, but not both && $x && $y || $x || $y ! Not !$x True if $x is not true

20 Array Operator Name Example Result + Union $x + $y Union of $x and $y
== Equality $x == $y Returns true if $x and $y have the same key/value pairs === Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types != Inequality $x != $y Returns true if $x is not equal to $y <> $x <> $y !== Non-identity $x !== $y Returns true if $x is not identical to $y

21 if <?php $t = date("H"); if ($t < "10") {     echo "Have a good morning!"; } elseif ($t < "20") {     echo "Have a good day!"; } else {     echo "Have a good night!"; } ?>

22 switch <?php $favcolor = "red"; switch ($favcolor) {     case "red":         echo "Your favorite color is red!";         break;     case "blue":         echo "Your favorite color is blue!";         break;     case "green":         echo "Your favorite color is green!";         break;     default:         echo "Your favorite color is neither red, blue, nor green!"; } ?>

23 while <?php $x = 1; while($x <= 5) {     echo "The number is: $x <br>";     $x++; } ?> <?php $x = 1; do {     echo "The number is: $x <br>";     $x++; } while ($x <= 5); ?>

24 for <?php for ($x = 0; $x <= 10; $x++) {     echo "The number is: $x <br>"; } ?> <?php $colors = array("red", "green", "blue", "yellow"); foreach ($colors as $value) {     echo "$value <br>"; } ?>

25 functions <?php function sum($x, $y) {     $z = $x + $y;     return $z; } echo " = " . sum(5, 10) . "<br>"; echo " = " . sum(7, 13) . "<br>"; echo "2 + 4 = " . sum(2, 4); ?> <?php function familyName($fname, $year) {     echo "$fname Refsnes. Born in $year <br>"; } familyName("Hege", "1975"); familyName("Stale", "1978"); familyName("Kai Jim", "1983"); ?>

26 Arrays $cars = array("Volvo", "BMW", "Toyota");
$cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota"; $age['Peter'] = "35"; $age['Ben'] = "37"; $age['Joe'] = "43"; <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?>

27 Sorting arrays sort() - sort arrays in ascending order
rsort() - sort arrays in descending order asort() - sort associative arrays in ascending order, according to the value ksort() - sort associative arrays in ascending order, according to the key arsort() - sort associative arrays in descending order, according to the value krsort() - sort associative arrays in descending order, according to the key

28 <?php $numbers = array(4, 6, 2, 22, 11); sort($numbers); $arrlength = count($numbers); for($x = 0; $x <  $arrlength; $x++) {      echo $numbers[$x];      echo "<br>"; } ?>

29 Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_ENV
$_COOKIE $_SESSION

30 forms <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> <input type="text" name=" "><br> < input type="submit"> < /form> <html> < body> Welcome <?php echo $_POST["name"]; ?><br> Your address is: <?php echo $_POST[" "]; ?> < /body> < /html>

31 Include menu.php <?php echo '<a href="/default.asp">Home</a> - <a href="/html/default.asp">HTML Tutorial</a> - <a href="/css/default.asp">CSS Tutorial</a> - <a href="/js/default.asp">JavaScript Tutorial</a> - <a href="default.asp">PHP Tutorial</a>'; ?> <html> < body> <div class="menu"> <?php include 'menu.php';?> </div> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> < /body> < /html>

32 Include menu.php <?php echo '<a href="/default.asp">Home</a> - <a href="/html/default.asp">HTML Tutorial</a> - <a href="/css/default.asp">CSS Tutorial</a> - <a href="/js/default.asp">JavaScript Tutorial</a> - <a href="default.asp">PHP Tutorial</a>'; ?> <html> < body> <div class="menu"> <?php include 'menu.php';?> </div> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.</p> < /body> < /html>

33 Session <?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION["favcolor"] = "green"; $_SESSION["favanimal"] = "cat"; echo "Session variables are set."; ?>

34 PHP-MySQL Connection <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) {     die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>

35 Create DB <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) {     die("Connection failed: " . mysqli_connect_error()); } // Create database $sql = "CREATE DATABASE myDB"; if (mysqli_query($conn, $sql)) {     echo "Database created successfully"; } else {     echo "Error creating database: " . mysqli_error($conn); } mysqli_close($conn); ?>

36 Create Table <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) {     die("Connection failed: " . mysqli_connect_error()); } // sql to create table $sql = "CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, VARCHAR(50), reg_date TIMESTAMP )"; if (mysqli_query($conn, $sql)) {     echo "Table MyGuests created successfully"; } else {     echo "Error creating table: " . mysqli_error($conn); } mysqli_close($conn); ?>

37 Insert data <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) {     die("Connection failed: " . mysqli_connect_error()); } // sql to create table $sql = "CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, VARCHAR(50), reg_date TIMESTAMP )"; if (mysqli_query($conn, $sql)) {     echo "Table MyGuests created successfully"; } else {     echo "Error creating table: " . mysqli_error($conn); } mysqli_close($conn); ?>

38 Select data <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) {     die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) {     // output data of each row     while($row = mysqli_fetch_assoc($result)) {         echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";     } } else {     echo "0 results"; } mysqli_close($conn); ?>


Download ppt "Ch. 3. PHP (이 강의 내용의 대부분 예들은 w3schools. com/php/default"

Similar presentations


Ads by Google