Introduction to PHP
-
Upload
jussi-pohjolainen -
Category
Education
-
view
68.218 -
download
0
Transcript of Introduction to PHP
![Page 1: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/1.jpg)
Introduction to PHP
Jussi PohjolainenTAMK University of Applied Sciences
![Page 2: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/2.jpg)
WEB PROGRAMMING CONCEPTS
![Page 3: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/3.jpg)
Three-tiered Web Site: LAMPClientUser-agent: Firefox
ServerApache HTTP Server
example requestGET / HTTP/1.1Host: www.tamk.fiUser-Agent: Mozilla/5.0 (Mac..)...
response
DatabaseMySQL
PHPPHP
![Page 4: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/4.jpg)
Java EE Architecture
(x)html / xml
Applet
ClientApplication
J2EE Application Server
Web Container
Servlets JSPJSPsServlets
EJB Container
RM
I/IIOP
JND
I
JTA
JDB
C
JMS
Java
Ma
il
JAF
Session Beans
Entity BeansMsg-Driven Beans
RM
I/IIOP
JND
I
JTA
JDB
C
JMS
Java
Ma
il
JAF
HTTP(S)JDBC
JavaMail
RMI
IIOP
JNDI
JMS
DB
Java-Application
CORBAServer
Message Queue
Directory Service
Client
Mail Server
![Page 5: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/5.jpg)
Server Side Techniques
• Server side scripting requires installation on the server side
• Typically client siis only xhtml and it unaware that the xhtml was produced by a server side script
• Does not require any installations or add-ons on the client
![Page 6: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/6.jpg)
Server Side Techniques
• PHP• Java EE: Servlet, JSP• .NET• CGI / Perl (Very old)• Ruby• …
![Page 7: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/7.jpg)
Client Side Techniques
• Requires that the client supports the technique
• JavaScript, Applet, Flash…
![Page 8: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/8.jpg)
Web Application Frameworks
• A web application framework is a software framework that is designed to support the development of dynamic websites, Web applications and Web services.
• Numerous frameworks available for many languages
![Page 9: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/9.jpg)
Web App vs. Web Site?
• What’s the difference between Web App and Web Site?
• Rich Internet Application?, AJAX?, Thin Client?• Full application running in your browser or
just a web site?
![Page 10: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/10.jpg)
PHP: HYPERTEXT PREPROCESSOR
![Page 11: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/11.jpg)
Introduction to PHP
• PHP is a computer scripting language.• Originally designed for producing dynamic web pages• Appeared in 1995• PHP Group is responsible for the language, no formal
specification• Free software• Runs on most operating systems and platforms• URL: http://www.php.net
![Page 12: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/12.jpg)
Response
![Page 13: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/13.jpg)
Introduction to PHP Syntax
• PHP has quite easy syntax, if you are familiar with any c-type language
• It has all the same structures that you are familiar with other programming languages
• PHP is designed to output to browser, but it is possible to do also CLI apps.
![Page 14: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/14.jpg)
Example
<?php
print "What is your name?\n";
$name = trim(fgets(STDIN));
print "Hello " . $name;
?>
![Page 15: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/15.jpg)
Variables
• Variables in PHP are represented by a dollar sign
• PHP supports eight types:– boolean, integer, float, double, array, object,
resource and NULL
![Page 16: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/16.jpg)
Example (php.net)<?php
$a_bool = TRUE; // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12; // an integer
echo gettype($a_bool); // prints out: boolean
echo gettype($a_str); // prints out: string
// If this is an integer, increment it by four
if (is_int($an_int)) {
$an_int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
echo "String: $a_bool";
}
?>
![Page 17: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/17.jpg)
Naming Variables
• Case-sensitivity• Start with letter or _• After that you can have numbers, letters and _
– $var = 'Bob';– $Var = 'Joe';– print "$var, $Var"; – $4site = 'not yet'; – $_4site = 'not yet';
![Page 18: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/18.jpg)
Constants
• You cannot alter the value of constant after declaration– define(CONSTANT, "value");– print CONSTANT;
![Page 19: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/19.jpg)
Magic Constants
• PHP has lot of predefined variables• Also predefined constants:
– __LINE__– __FILE__– __FUNCTION__– __CLASS__– __METHOD__
• See: http://fi.php.net/manual/en/language.constants.predefined.php
![Page 20: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/20.jpg)
Scope
<?php
$a = "Pekka";
print ”My name is " . $a;
?>
![Page 21: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/21.jpg)
Scope
<?php
$a = "Pekka";
function Test() {
print $a;
}
print ”My name is ”;
Test();
?>
![Page 22: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/22.jpg)
Scope
<?php
$a = "Pekka";
function Test() {
global $a;
print $a;
}
print ”My name is ”;
Test();
?>
![Page 23: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/23.jpg)
Control Structures
• If, else, elseif, switch• while, do-while, for• foreach• break, continue•
![Page 24: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/24.jpg)
PHP BASICS
![Page 25: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/25.jpg)
Strings
• Single quoted: 'this is a $variable'• Double quoted: "this is a $variable"• Heredoc:
– $str = <<<EOD– Example of string– spanning multiple lines– using heredoc syntax.– EOD;
![Page 26: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/26.jpg)
Modifying the String
• $mj = "moi";• print $mj[0];• $mj[0] = 'x';• print $mj;• $mj = $mj . " hei";• print $mj;• $mj .= " terse";• print $mj;
![Page 27: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/27.jpg)
String functions
• A lot of functions…– http://www.php.net/manual/en/ref.strings.php
![Page 28: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/28.jpg)
Statements
• Every statement ends with ;– $a = 5;– $a = function();– $a = ($b = 5);– $a++; ++$a;– $a += 3;
![Page 29: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/29.jpg)
Operators
• Arithmethic: +,-,*,%• Setting variable: =• Bit: &, |, ^, ~, <<, >>• Comparison: ==, ===, !=, !==, <, > <=, >=
![Page 30: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/30.jpg)
Ternary Operator
$variable = (1 < $x) ? 'value1' : 'value2';
Equals
if(1 < $x) {
$variable = 'value1';
}
else {
$variable = 'value1';
}
![Page 31: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/31.jpg)
Execution Operator
• Execute command in shell– $result = `ls -al`;– print $result;
• Does the same than shell_exec() - function
![Page 32: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/32.jpg)
Logical Operators
• $a and $b• $a or $b• $a xor $b• !$a;• $a && $b;• $a || $b;
![Page 33: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/33.jpg)
String Operators
• Two operators for strings: '.' and '.='• '.' – combining strings.• '.=' – appends string to the end.• Example:• $v= "Hello" . $b;• $v.= "Hello";
![Page 34: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/34.jpg)
Arrays
• See – http://php.tpu.fi/~pohjus/lectures/php/php-
arrays.html
![Page 35: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/35.jpg)
CONTROL STRUCTURES
![Page 36: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/36.jpg)
IF<?php
if ($a > $b) {
echo "a is bigger than b";
} else {
echo "a is NOT bigger than b";
}
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
![Page 37: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/37.jpg)
While and Do-While
<?php
$a=0;
while($a<10){
print $a; $a++;
}
$i = 0;
do {
print $i;
} while ($i > 0);
?>
![Page 38: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/38.jpg)
For
for ($i = 1; $i <= 10; $i++) {
print $i;
}
![Page 39: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/39.jpg)
Foreach
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
echo $value;
}
![Page 40: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/40.jpg)
Switch
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
![Page 41: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/41.jpg)
PHP COMBINED WITH XHTML
![Page 42: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/42.jpg)
Response
![Page 43: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/43.jpg)
Example: spaghetti-way<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>xhtml-doku</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Title</h1>
<?php
print "<p>Hello from PHP!</p>";
?>
</body>
</html>
![Page 44: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/44.jpg)
Better ways
• Use functions• Use OO
![Page 45: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/45.jpg)
PHP AND USER INPUT
![Page 46: Introduction to PHP](https://reader036.fdocuments.net/reader036/viewer/2022062418/555ada31d8b42a024a8b4edc/html5/thumbnails/46.jpg)
PHP and User Input via Forms
• See– http://php.tpu.fi/~pohjus/lectures/php/forms-
and-php.html