introduction to php. php php is the hypertext pre-processor –script language –embedded into html...
TRANSCRIPT
![Page 1: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/1.jpg)
Introduction to PHP
![Page 2: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/2.jpg)
PHP
• PHP is the Hypertext Pre-processor– Script language– Embedded into HTML– Runs as Apache module– Can use DB (MySQL, Oracle, Microsoft SQL,
PostgreSQL)– Rich features: XML, PDF, IMAP, LDAP
![Page 3: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/3.jpg)
PHP Origins
Rasmus Lerdorf (born Greenland, ed Canada)
PHP originally abbreviates for ‘Personal Home Pages’, now ‘PHP Hypertext Processor’
Other key developers: Zeev Surashi and Andi Gutmans (Israel)
Open Source
PHP version 4.4.3 current at UWE Due to upgrade to PHP 5
![Page 4: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/4.jpg)
Scripting languages
• A scripting language is:– often evolved not designed– cross-platform since interpreter is easy to port– designed to support a specific task – PHP -> Web
support– un-typed variables (but values are typed) – implicit variable declaration – implicit type conversion – stored only as script files– compiled on demand– may run on the server (PHP) or the client (Javascript)
![Page 5: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/5.jpg)
PHP details
• Procedural language– Compare with Javascript which is event-driven
• C-like syntax - { } ;• Extensive Function Library• Good Web-server integration
– Script embedded in HTML– Easy access to form data and output of HTML pages
• Not fully object-oriented – Java is fully object oriented – all functions have to be
in a class– In PHP, classes are additional but quite simple to use
![Page 6: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/6.jpg)
PHP and HTML
• HTML-embedded– PHP scripts are essentially HTML pages with the
occasional section of PHP script.– PHP script is enclosed in the tag pair:
<?php echo “hello” ?>
![Page 7: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/7.jpg)
• Free format - white space is ignored • Statements are terminated by semi-colon ; • Statements grouped by { … } • Comments begin with // or a set of comments /* */ • Assignment is ‘=’ $a=6• Relational operators are ,< , > == ( not a single equal)
• Control structures include if (cond) {..} else { }, while (cond) { .. } , for(startcond; increment; endcond) { }
C-like language
![Page 8: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/8.jpg)
• Arrays are accessed with [ ] : $x[4] is the 5th element of the array $x – indexes start at 0
• Associative Arrays (hash array in Perl, dictionary in Java) are accessed in the same way: $y[“fred”]
• Functions are called with the name followed by arguments in a fixed order enclosed in ( ) : substr(“fred”,0,2)
• Case sensitive - $fred is a different variable to $FRED
![Page 9: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/9.jpg)
Function library
• Basic tasks– String Handling – Mathematics – random numbers, trig functions.. – Regular Expressions – Date and time handling – File Input and Output
• And more specific functions for-– Database interaction –
• MySQL, Oracle, Postgres, Sybase, MSSQL .. – Encryption – Text translation – Image creation – XML
![Page 10: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/10.jpg)
String Handling
• String literals (constants) enclosed in double quotes “ ” or single quotes ‘ ’
• Within “”, variables are replaced by their value: – called variable interpolation. “My name is $name, I think”
• Within single quoted strings, interpolation doesn’t occur
• Strings are concatenated (joined end to end) with the dot operator “key”.”board” == “keyboard”
![Page 11: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/11.jpg)
• Standard functions exist: strlen(), substr() etc
• Values of other types can be easily converted to and from strings – numbers implicitly converted to strings in a string context.
• Regular expressions be used for complex pattern matching.
![Page 12: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/12.jpg)
Learning PHP
• Start with just the basics, installing a script to output an HTML page
• Understand how PHP supports interaction with the Browser or other clients
• Understand how PHP supports integration with databases – Postgress
![Page 13: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/13.jpg)
Basic PHP Syntax
<?php ?>
![Page 14: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/14.jpg)
Datatypes
• String– 'My name is Taro Keio.'– "My name is $name."
• Boolean– true– false
• Integer– 100– 0x1c
• Floating point• Array
– array("tokyo", "hanoi", "london")– array("japan" => "tokyo", "vietnam" => "hanoi",
"england" => "london")– $a[2]– $a["vietnam"]
![Page 15: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/15.jpg)
Variables in PHP
• All variables in PHP start with a $ sign symbol. • Variables may contain strings, numbers, or
arrays.
<html><body><?php $txt="Hello World"; echo $txt; ?></body>
</html>
![Page 16: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/16.jpg)
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 should be more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
![Page 17: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/17.jpg)
Comments in PHP
<html><body><?php
//This is a comment/*This is a comment block*/
?></body> </html>
![Page 18: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/18.jpg)
The If...Else Statement
Syntax
if (condition) code to be executed if condition is true;
else
code to be executed if condition is false;
![Page 19: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/19.jpg)
Example
<html><body><?php
$d=date("D");if ($d=="Fri")
echo "Have a nice weekend!";else
echo "Have a nice day!"; ?></body> </html>
![Page 20: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/20.jpg)
The Switch Statement
Syntaxswitch (expression){
case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break;default: code to be executed if expression is different from both label1 and label2;
}
![Page 21: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/21.jpg)
Looping
• 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
• foreach - loops through a block of code for each element in an array
![Page 22: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/22.jpg)
The while Statement
Syntax
while (condition) code to be executed;
![Page 23: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/23.jpg)
The do...while Statement
Syntax
Do {
code to be executed;}
while (condition);
![Page 24: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/24.jpg)
The for Statement
Syntax
for (initialization; condition; increment) { code to be executed; }
![Page 25: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/25.jpg)
The foreach Statement
Syntax
foreach (array as value) { code to be executed; }
![Page 26: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/26.jpg)
PHP Function
<html><body><?php
function writeMyName() {
echo "Kai Jim Refsnes"; }
writeMyName();?></body> </html>
![Page 27: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/27.jpg)
PHP Form Handling
<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>
![Page 28: Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft](https://reader036.vdocuments.net/reader036/viewer/2022062304/56649de45503460f94adbbad/html5/thumbnails/28.jpg)
welcome.php
<html><body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.</body> </html>