Week 5 Dr Ken Cosh Introducing PHP 1 Last Week Intro to Javascript This week Introducing PHP 2 Introduction PHP Hypertext Preprocessor Serverside script language Supported by ApacheIIS on various platform MSLinuxMac OS ID: 633935
Download Presentation The PPT/PDF document "269200 Web Programming Language" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
269200 Web Programming Language
Week 5Dr. Ken CoshIntroducing PHP
1Slide2
Last WeekIntro to JavascriptThis week
Introducing PHP!2Slide3
IntroductionPHP : Hypertext PreprocessorServer-side script languageSupported by Apache/IIS on various platform (MS/Linux/Mac OS)
Opensource (PHP License v3.01)
3Slide4
Introduction – cont.Extension: .phpFile contentHtml codePHP code
4
PHP
Request
HTMLSlide5
InstallationLAMP (or MAMP, or WAMP…)TestingCreate a file and name it “
test.php”The content is “<?php phpinfo(); ?>”
5Slide6
OutlinePHP StructureComments, Syntax, Variables
OperatorsConditionalsLooping
6Slide7
Basic SyntaxScript blockings start with <?php ?>Can be anywhere in the code
It can be <? ?>Inside the block are html-code, php-code, or textComment the code by // or /* */ as in C
7Slide8
Basic SyntaxPut the script in
test.php<html>
<body>
<?
php
echo “hello, world”;
?>
</body>
</html>
8Slide9
VariablesOne type, just variablesCan store numbers, strings, or array
Conversion between type is automatically done.Variables begin with $, e.g. $var1 = 1; or $var2 = “alice”;No declaration is required.
9Slide10
VariablesNaming rulesBegins with letter or _ (after $)
Contains only A-Z, a-z, 0-9, and _
10Slide11
Variables - Array
11
Numeric
$cars=array("Saab","Volvo","BMW","Toyota");
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";Slide12
Variables - Array
12
Associative
$
ages
=
array
("Peter"=>32, "
Quagmire
"=>30, "Joe"=>34
);
Or
$
ages
['Peter'] = "32";
$
ages
['
Quagmire
'] = "30";
$
ages
['Joe'] = "34
";
Example,
<?
php
$
ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>Slide13
StringsFunctions
Concatenate: .Length: strlen
()
Search/Position:
strpos
(text, pattern)
More information:
http://
www.w3schools.com/php/php_ref_string.asp
13Slide14
StringsPut the script in test2.php
<html><body>
<?
php
$h1 = “hello”;
$h2 = “world”;
echo $h1.”, “.$h2.”<
br
/>”;
echo “h1 has: “.strlen
($h1).” letters in it <br/>”;?></body></html>
14Slide15
Operators - Arithmetic
15Slide16
Operators - Assignment
16Slide17
Operators - Comparison
17Slide18
Operators - Logical
18Slide19
Control Flowif/while/do…while/for
All the same as C.for each
Syntax
foreach
($array as $value)
{
code to be executed;
}
19Slide20
Control Flow
Example<html><body>
<?
php
$x=array("
one","two","three
");
foreach
($x as $value)
{
echo $value . "<
br
/>";
}
?></body>
</html>
20Slide21
FormPut the script in
test_form.php<html>
<body>
<form action="
welcome.php
" method="post">
Name
: <input type="text" name="
fname
" />
Age
: <input type="text" name="age" />
<
input type="submit" />
</form>
</body></html>
21Slide22
FormPut the script in
welcome.php<html>
<body>
Welcome <?
php
echo $_POST["
fname
"]; ?>!<
br
/>
You are <?
php
echo $_POST["age"]; ?> years old.
</body>
</html>
22Slide23
FormGet, put the script in
test_form.php<html>
<body>
<form action="
welcome.php
" method
=“get">
Name
: <input type="text" name="
fname
" />
Age
: <input type="text" name="age" />
<
input type="submit" />
</form></body>
</html
>
http
://localhost?fname=Peter&age=37
23Slide24
FormGetVisibility
Though, it can be bookmarkedNot suitable for large variables (>2000 characters)
24Slide25
AssignmentForm Validation A continuation from the previous
Javascript exercise. This time validate the form using PHP. Use the form to send data to a new page, and use the new page to display an appropriate message, e.g.;"Password must have 8 characters""Age must be >18″
“All data correct”
25