Professional Documents
Culture Documents
02 Lab
02 Lab
02 Lab
Overview
HTTP Communication
HTTP Communication for Web
Static
Dynamic
Dynamic Content
PHP
PHP Block
PHP Programming Style
Database
MySQL
HTTP Communication
HTTP Communication for Web
HTML + JavaScript
HTTP Communication for Web – Dynamic
HTML + PHP
HTTP Communication for Web – Dynamic
HTML + PHP
HTTP Communication for Web – Dynamic Content
HTTP Communication for Web
URL request
client server
HTML
HTML Script request
PHP
PHP Block
Rules of thumb
Variable name must start with dollar sign ($)
Combination of strings, numbers and the underscore
First character after dollar sign cannot be a number
Case sensitive
Assigned value using equals sign (=)
String
Arithmetic operators
+ addition
- subtraction
* multiplication
/ division
% modular
++ increment
-- decrement
Numbers
Functions
round()
$j = 3.14;
$k = round( $j);
number_format()
$p =20980;
$g=number_format($p);
$g=number_format($p,2);
Constant
$var =‘Hello’;
“”
replace variables name with its value and a special
character’s code (\$) with its represented value
‘’
display exactly what you type, except for the escaped
single quote (\’) and the escape backslash(\\).
PROGRAMMING WITH PHP
1. Step 1:
Create HTML form with any text editor
• HTML form (.htm/.html) is created using the HTML form tags and
various input types.
2. Step 2:
Create PHP scripts that receives form data
• PHP script (.php) is created to receives the submitted form data
and handle it.
Step 1:
Handling HTML form create html form
Step 2:
create php scripts that
receive form data
Step 1: create html form
form.html
which page the form
data will be send
If you have a text box in html form with name attribute age, PHP
will store the text entered there in a variable called $age
(registered global variable)
$age is similar to $_POST[‘age’] (superglobal variables)
attributes
eg : ( in HTML form)
<p><b>Age: </b><input type ="text" name=“age" size="20" maxlength="40"/></p>
eg : ( in PHP scripts)
echo “<p>Thank you, {$_POST[‘age’]} for the following comments”;
Conditional and Operators
TRUE
Conditional and Operators
switch
Used in place of a long if–elseif–else conditional
Syntax :
switch ($variable) {
case ‘value1’ :
// do this
break;
case ‘value2’ :
// do this
break;
default :
// do this
break;
}
Validating Form Data
appended!
Example – associative
$states
Key Value
MD Maryland
PA Penssylvania
IL Illinois
MO Missouri
What are Arrays?
To access Maryland
$abbr[‘US’][‘MD’]
To print out
echo “The US state whose abbreviation is MD is {$abbr[‘US’][‘MD’]}.”;
Array and strings
While loop
If the condition is still true, the loop will be executed
Syntax :
while (condition) {
// do something
}
For loop
first the initial expression is run, checked the condition and if true,
content of the loop is executed. After execution, checked closing
expression with condition again.
Syntax :
for (initial expression; condition; closing expression)
{
// do something
}