Download presentation
Presentation is loading. Please wait.
1
เอกสารประกอบการบรรยายรายวิชา 204329 Web Technology
Basic PHP syntax
2
Web Architecture Web Browser Host Web Server
3
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax
Variables, I/O, String Operations, Control Statements, Forms
4
Web Architecture Apache Web server Apache Tomcat
Internet Information Server Netscape Web Server
5
Web Architecture Web Browser Host Web FTP Mail
6
Web Architecture Client Host Browser Web FTP Mail
7
Common Gateway Interface (CGI)
Web Browser Web Server Request Object
8
Common Gateway Interface (CGI)
Web Browser Web Server Response Object
9
Common Gateway Interface (CGI)
printf(“<HTML>”); printf(“<BODY>”); printf(“Hello CGI<BR>”); printf(“1 + 1 = %d”, 1+1); printf(“</BODY>”); printf(“</HTML>”);
10
Common Gateway Interface (CGI)
11
Common Gateway Interface (CGI)
Web Browser Web Server Response Object
12
Common Gateway Interface (CGI)
printf(“<HTML>”); printf(“<BODY>”); printf(“Hello CGI<BR>”); printf(“1 + 1 = &d”, 1+1); printf(“</BODY>”); printf(“</HTML>”);
13
Common Gateway Interface (CGI)
Web Browser Web Server <HTML> <BODY> Hello CGI<BR> 1+1=2 </BODY> </HTML>
14
Common Gateway Interface (CGI)
15
Common Gateway Interface (CGI)
C/C++ Perl
16
Common Gateway Interface (CGI)
$sum = 1 + 1 print “<HTML>”; print “<BODY>”; print “Hello Perl<BR>”; print “1 + 1 = $sum” print “</BODY>” ; print “</HTML>”;
17
Common Gateway Interface (CGI)
18
หลักการทำงานของ PHP Web Browser Web Server App. Engine
19
หลักการทำงานของ PHP Web Browser Web Server App. Engine
20
หลักการทำงานของ 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>
21
หลักการทำงานของ 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
22
หลักการทำงานของ 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
23
หลักการทำงานของ 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
24
หลักการทำงานของ 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
25
หลักการทำงานของ 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
26
หลักการทำงานของ 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
27
หลักการทำงานของ ASP & PHP
Web Browser Web Server Example Hello Bold Hello Italic Hello ASP 1 + 1 = 2 PHP Engine
28
หลักการทำงานของ 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>
29
หลักการทำงานของ ASP & PHP
Web Browser Web Server Request Object
30
หลักการทำงานของ ASP & PHP
Web Browser Web Server Response Object
31
หลักการทำงานของ 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>
32
หลักการทำงานของ 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>
33
หลักการทำงานของ ASP & PHP
Web Browser Web Server
34
Form & ASP/PHP
35
หลักการทำงานของ ASP & PHP
Web Browser Web Server Request Object
36
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>
37
หลักการทำงานของ ASP & PHP
Web Browser Web Server Request Object
38
หลักการทำงานของ ASP & PHP
<html> <body> <form action="demo_simpleform.asp" … $_POST["fname"]; … </body> </html> Web Browser Web Server Form { action=“demo…” fname=“john” }
39
หลักการทำงานของ 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
40
หลักการทำงานของ 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>
41
หลักการทำงานของ ASP & PHP
Web Browser Web Server <HTML> <BODY> <FORM …> … </FORM> Hello John<BR> How are you today? </BODY> </HTML>
42
หลักการทำงานของ ASP & PHP
Web Browser Web Server
43
หลักการทำงานของ ASP & PHP
Web Browser Web Server Request Object
44
หลักการทำงานของ ASP & PHP
Web Browser Web Server Response Object
45
PHP & Database Web Browser Web Server Database PHP Engine
46
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax
Variables, I/O, String Operations, Control Statements, Forms
54
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax
Variables, I/O, String Operations, Control Statements, Forms
55
Basic PHP Syntax Variables I/O String Operations Control Statements
Forms
56
Basic PHP Syntax <?php ?>
57
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 ?>.
58
Basic PHP Syntax <html> <body> <?php
echo "Hello World"; ?> </body> </html>
59
Basic PHP Syntax Comments in PHP
In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.
60
Basic PHP Syntax <html> <body>
<?php //This is a comment /* This is a comment block */ ?> </body> </html>
61
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;
62
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; ?>
63
Basic PHP Syntax PHP is a Loosely Typed Language
In PHP a variable does not need to be declared before being set.
64
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)
65
Basic PHP Syntax Strings in PHP
String variables are used for values that contains character strings. <?php $txt="Hello World"; echo $txt; ?>
66
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:
67
Basic PHP Syntax <?php $txt1="Hello World"; $txt2="1234";
echo $txt1 . " " . $txt2; ?>
68
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!"); ?>
69
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.
70
Basic PHP Syntax PHP Operators Arithmetic Operators
71
Basic PHP Syntax PHP Operators Assignment Operators
72
Basic PHP Syntax PHP Operators Comparison Operators
73
Basic PHP Syntax PHP Operators Logical Operators
74
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
75
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;
76
<html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> </body> </html>
77
<html> <body> <?php $d=date("D"); if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?> </body> </html>
78
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;
79
<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>
80
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
81
Basic PHP Syntax While Statement <html> <body> <?php
while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } ?> </body> </html>
82
Basic PHP Syntax Do…While Statement <html> <body> <?php
{ $i++; echo "The number is " . $i . "<br />"; } while ($i<5); ?> </body> </html>
83
Basic PHP Syntax The for Statement <html> <body> <?php
for ($i=1; $i<=5; $i++) { echo "Hello World!<br />"; } ?> </body> </html>
84
Basic PHP Syntax The foreach Statement <html> <body>
$arr=array("one", "two", "three"); foreach ($arr as $value) { echo "Value: " . $value . "<br />"; } ?> </body> </html>
85
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.
86
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>
87
welcome.php <html> <body>Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old.</body> </html>
88
Basic PHP Syntax PHP Form Handling $_GET $_POST
89
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>
90
Basic PHP Syntax $_GET welcome.php
Welcome <?php echo $_GET["name"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old!
91
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>
92
Basic PHP Syntax $_POST
Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old!
93
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!
94
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax
Variables, I/O, String Operations, Control Statements, Forms
102
หัวข้อบรรยาย PHP Recap PHP Example Applications Basic PHP Syntax
Variables, I/O, String Operations, Control Statements, Forms
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.