เอกสารประกอบการบรรยายรายวิชา 204329 Web Technology Basic PHP syntax
Web Architecture Web Browser Host Web Server
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax Variables, I/O, String Operations, Control Statements, Forms
Web Architecture Apache Web server Apache Tomcat Internet Information Server Netscape Web Server
Web Architecture Web Browser Host Web FTP Mail
Web Architecture Client Host Browser Web FTP Mail
Common Gateway Interface (CGI) Web Browser Web Server Request Object
Common Gateway Interface (CGI) Web Browser Web Server Response Object
Common Gateway Interface (CGI) printf(“<HTML>”); printf(“<BODY>”); printf(“Hello CGI<BR>”); printf(“1 + 1 = %d”, 1+1); printf(“</BODY>”); printf(“</HTML>”);
Common Gateway Interface (CGI)
Common Gateway Interface (CGI) Web Browser Web Server Response Object
Common Gateway Interface (CGI) printf(“<HTML>”); printf(“<BODY>”); printf(“Hello CGI<BR>”); printf(“1 + 1 = &d”, 1+1); printf(“</BODY>”); printf(“</HTML>”);
Common Gateway Interface (CGI) Web Browser Web Server <HTML> <BODY> Hello CGI<BR> 1+1=2 </BODY> </HTML>
Common Gateway Interface (CGI)
Common Gateway Interface (CGI) C/C++ Perl
Common Gateway Interface (CGI) $sum = 1 + 1 print “<HTML>”; print “<BODY>”; print “Hello Perl<BR>”; print “1 + 1 = $sum” print “</BODY>” ; print “</HTML>”;
Common Gateway Interface (CGI)
หลักการทำงานของ PHP Web Browser Web Server App. Engine
หลักการทำงานของ PHP Web Browser Web Server App. Engine
หลักการทำงานของ ASP & PHP <HTML> <BODY> <H1>Example</H1> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> <?php echo "Hello ASP<BR />"; echo "1 + 1 = " . (1+1); ?> </BODY> </HTML>
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <H1>Example</H1> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> <?php echo "Hello ASP<BR />"; echo "1 + 1 = " . (1+1); ?> </BODY> </HTML> PHP Engine
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <H1>Example</H1> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> <?php echo "Hello ASP<BR />"; echo "1 + 1 = " . (1+1); ?> </BODY> </HTML> PHP Engine
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <H1>Example</H1> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> <?php echo "Hello ASP<BR />"; echo "1 + 1 = " . (1+1); ?> </BODY> </HTML> PHP Engine
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <H1>Example</H1> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> <?php echo "Hello ASP<BR />"; echo "1 + 1 = " . (1+1); ?> </BODY> </HTML> PHP Engine
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <H1>Example</H1> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> Hello ASP<BR> 1 + 1 = 2 </BODY> </HTML> PHP Engine
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <H1>Example</H1> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> Hello ASP<BR> 1 + 1 = 2 </BODY> </HTML> PHP Engine
หลักการทำงานของ ASP & PHP Web Browser Web Server Example Hello Bold Hello Italic Hello ASP 1 + 1 = 2 PHP Engine
หลักการทำงานของ ASP & PHP <HTML> <BODY> <H1>Example</H1> <B>Hello Bold</B><BR> <B>Hello Italic</B><BR> <?php echo "Hello ASP<BR />"; echo "1 + 1 = " . (1+1); ?> </BODY> </HTML>
หลักการทำงานของ ASP & PHP Web Browser Web Server Request Object
หลักการทำงานของ ASP & PHP Web Browser Web Server Response Object
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <H1>Example</H1> <IMG SRC="images/SUTLogo.JPG"> <BR> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> Hello ASP <BR> 1+1=2 </BODY> </HTML> <HTML> <BODY> <H1>Example</H1> <IMG SRC="images/SUTLogo.JPG"> <BR> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> <?php echo "Hello ASP<BR />"; echo "1 + 1 = " . (1+1); ?> </BODY> </HTML>
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <H1>Example</H1> <IMG SRC="images/SUTLogo.JPG"> <BR> <B>Hello Bold</B><BR> <I>Hello Italic</I><BR> Hello ASP <BR> 1+1=2 </BODY> </HTML>
หลักการทำงานของ ASP & PHP Web Browser Web Server
Form & ASP/PHP
หลักการทำงานของ ASP & PHP Web Browser Web Server Request Object
Form & ASP/PHP <html> <body> <form action="demo_simpleform.php" method="post"> Your name: <input type="text" name="fname" size="20"> <input type="submit" value="Submit"> </form> <?php if($_POST["fname"] != null) { echo "Hello " . $_POST["fname"]; echo "<BR />How are you today?"; } ?> </body> </html>
หลักการทำงานของ ASP & PHP Web Browser Web Server Request Object
หลักการทำงานของ ASP & PHP <html> <body> <form action="demo_simpleform.asp" … $_POST["fname"]; … </body> </html> Web Browser Web Server Form { action=“demo…” fname=“john” }
หลักการทำงานของ ASP & PHP Web Browser Web Server <html> <body> <form> … </form> <?php if($_POST["fname"] != null) { echo "Hello " . $_POST["fname"]; echo "<BR />How are you today?"; } ?> </body> </html> Response Object
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <FORM …> … </FORM> Hello John<BR> How are you today? </BODY> </HTML> <html> <body> <form> … </form> <?php if($_POST["fname"] != null) { echo "Hello " . $_POST["fname"]; echo "<BR />How are you today?"; } ?> </body> </html>
หลักการทำงานของ ASP & PHP Web Browser Web Server <HTML> <BODY> <FORM …> … </FORM> Hello John<BR> How are you today? </BODY> </HTML>
หลักการทำงานของ ASP & PHP Web Browser Web Server
หลักการทำงานของ ASP & PHP Web Browser Web Server Request Object
หลักการทำงานของ ASP & PHP Web Browser Web Server Response Object
PHP & Database Web Browser Web Server Database PHP Engine
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax Variables, I/O, String Operations, Control Statements, Forms
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax Variables, I/O, String Operations, Control Statements, Forms
Basic PHP Syntax Variables I/O String Operations Control Statements Forms
Basic PHP Syntax <?php ?>
Basic PHP Syntax A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document. On servers with shorthand support enabled you can start a scripting block with <? and end with ?>.
Basic PHP Syntax <html> <body> <?php echo "Hello World"; ?> </body> </html>
Basic PHP Syntax Comments in PHP In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.
Basic PHP Syntax <html> <body> <?php //This is a comment /* This is a comment block */ ?> </body> </html>
Basic PHP Syntax Variables in PHP Variables are used for storing a values, like text strings, numbers or arrays. When a variable is set it can be used over and over again in your script All variables in PHP start with a $ sign symbol. The correct way of setting a variable in PHP: $var_name = value;
Basic PHP Syntax New PHP programmers often forget the $ sign at the beginning of the variable. In that case it will not work. Let's try creating a variable with a string, and a variable with a number: <?php $txt = "Hello World!"; $number = 16; ?>
Basic PHP Syntax PHP is a Loosely Typed Language In PHP a variable does not need to be declared before being set.
Basic PHP Syntax Variable Naming Rules A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ) A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
Basic PHP Syntax Strings in PHP String variables are used for values that contains character strings. <?php $txt="Hello World"; echo $txt; ?>
Basic PHP Syntax The Concatenation Operator There is only one string operator in PHP. The concatenation operator (.) is used to put two string values together. To concatenate two variables together, use the dot (.) operator:
Basic PHP Syntax <?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2; ?>
Basic PHP Syntax Using the strlen() function The strlen() function is used to find the length of a string. Let's find the length of our string "Hello world!": <?php echo strlen("Hello world!"); ?>
Basic PHP Syntax Using the strpos() function The strpos() function is used to search for a string or character within a string. If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE. Let's see if we can find the string "world" in our string: <?php echo strpos("Hello world!","world"); ?> Etc.
Basic PHP Syntax PHP Operators Arithmetic Operators
Basic PHP Syntax PHP Operators Assignment Operators
Basic PHP Syntax PHP Operators Comparison Operators
Basic PHP Syntax PHP Operators Logical Operators
Basic PHP Syntax Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. if...else statement - use this statement if you want to execute a set of code when a condition is true and another if the condition is not true elseif statement - is used with the if...else statement to execute a set of code if one of several condition are true
Basic PHP Syntax The If...Else Statement If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement. Syntax if (condition) code to be executed if condition is true; else code to be executed if condition is false;
<html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> </body> </html>
<html> <body> <?php $d=date("D"); if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?> </body> </html>
Basic PHP Syntax The ElseIf Statement If you want to execute some code if one of several conditions are true use the elseif statement Syntax if (condition) code to be executed if condition is true; elseif (condition) else code to be executed if condition is false;
<html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?> </body> </html>
Basic PHP Syntax Looping Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this. In PHP we have the following looping statements: while - loops through a block of code if and as long as a specified condition is true do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true for - loops through a block of code a specified number of times
Basic PHP Syntax While Statement <html> <body> <?php while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } ?> </body> </html>
Basic PHP Syntax Do…While Statement <html> <body> <?php { $i++; echo "The number is " . $i . "<br />"; } while ($i<5); ?> </body> </html>
Basic PHP Syntax The for Statement <html> <body> <?php for ($i=1; $i<=5; $i++) { echo "Hello World!<br />"; } ?> </body> </html>
Basic PHP Syntax The foreach Statement <html> <body> $arr=array("one", "two", "three"); foreach ($arr as $value) { echo "Value: " . $value . "<br />"; } ?> </body> </html>
Basic PHP Syntax PHP Form Handling The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
form.php <html> <body><form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form></body> </html>
welcome.php <html> <body>Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old.</body> </html>
Basic PHP Syntax PHP Form Handling $_GET $_POST
Basic PHP Syntax $_GET <form action="welcome.php" method="get"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form>
Basic PHP Syntax $_GET welcome.php Welcome <?php echo $_GET["name"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old!
Basic PHP Syntax $_POST <form action="welcome.php" method="post"> Enter your name: <input type="text" name="name" /> Enter your age: <input type="text" name="age" /> <input type="submit" /> </form>
Basic PHP Syntax $_POST Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old!
The $_REQUEST Variable The PHP $_REQUEST variable contains the contents of both $_GET, $_POST Example Welcome <?php echo $_REQUEST["name"]; ?>.<br /> You are <?php echo $_REQUEST["age"]; ?> years old!
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax Variables, I/O, String Operations, Control Statements, Forms
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax Variables, I/O, String Operations, Control Statements, Forms