ait 616 fall 2002 php. ait 616 fall 2002 php special scripting language used to dynamically...
TRANSCRIPT
AIT 616 Fall 2002
PHP
AIT 616 Fall 2002
PHP Special scripting language used to dynamically
generate web documents Open source – Free!!! Performs common server-side tasks Platform independent Similar in concept to ColdFusion and ASP
Instructions embedded with HTML template Instructions are parsed out and processed by a special
program running on the Web server Output from the instruction are incorporated back into
the HTML template and sent to the client’s browser
AIT 616 Fall 2002
PHP A relative newcomer in the server-side
development arena Gained a large following with its simple syntax
and comprehensive function library Attractive alternative to ASP for new developers Dubbed by some to be the ‘Perl Killer’ of the
server-side scripting world
AIT 616 Fall 2002
Support Servers Windows environment
Personal Web Server Internet Information Server (IIS) Apache
Unix Environment Apache (pre-configured on linux)
AIT 616 Fall 2002
Writing PHP Code PHP code coexists with HTML code in the same
file Must enclose PHP code with special delimiters
… HTML code ….<?php
… php code …?>… HTML code …
AIT 616 Fall 2002
Writing PHP Code
<html>….
<body>
<?phpecho “<h1>Hello, World</h1>”
?>
</body></html>
Server-side script
AIT 616 Fall 2002
Writing PHP Code
<html>….
<body>
<h1>Hello, World</h1>
</body></html>
Client-side source code delivered to a browser
AIT 616 Fall 2002
Writing PHP Code Comments
Double forward slash (//)
echo command Prints out and include a \n character
Comma Separates commands
AIT 616 Fall 2002
PHP Variables Stores literal values Two types
Scalar Holds one value String, integer, floating
Array Holds multiple values
Case sensitive
<?php$lang=‘PHP’$num=1
?>
AIT 616 Fall 2002
PHP Variables Predefined
Provides a large number to any script Are dependent upon which server is running
Version, setup, other factors
Scope Is the context within which it is defined For the most part, all variables have single scope
AIT 616 Fall 2002
PHP Variables
<?php
$a=1function Test() {
$b=$aecho $b
}
?>
Global Scope
Local Scope
AIT 616 Fall 2002
PHP Operators Common mathematical functions Concatenation
Dot (.) operator
Comparison operators Returns true or false
Boolean arithmetic
<?php$x=‘<h1>Hello,’$y=‘World!</h1>’$z=$x.$yecho $z
?>
Hello, World!
AIT 616 Fall 2002
PHP Decision Structures Decision making during course of scripting
processing if statement
<?php$age=26if($age>=18) {
echo ‘Register to vote!’}
?>
AIT 616 Fall 2002
PHP Looping Structure Iterative execution of a set of instructions for loop while loop<?php
$num=7$square=$num*$numwhile($num<$square) {
if($num%2==0) {echo ‘<h1>$num</h1>’
}$num=$num+1
}?>
AIT 616 Fall 2002
PHP Functions Performs many common tasks Large number of built-in functions Define your own function Functions
Apache-specific Database specific Network service Mathematical String PDF File system XML parser Session maintenance
AIT 616 Fall 2002
PHP Header() Function Useful built-in function Used to add a line to the HTTP header Functionality
Type of file being sent to client Cookies Redirect Host of other bits of information
AIT 616 Fall 2002
Built-in Function<?php
function myFactorial($x) {$f=1for($k=1;$k<=$x;$k++) {
$f=$f*$x;}return $f
}
$yourNumber=4echo ‘factorial of $yourNumber is: ‘echo myfactorial($yourNumber)
?>
AIT 616 Fall 2002
Common Web Tasks Responding to a form submission
PHP engine automatically stores the form data into an associative array
Using environmental variables Stores information about a client Function getenv()
Sending email Function mail()
Placing a cookie Function setcookie()
Database access
AIT 616 Fall 2002
Common Web Tasks Implementation of basic HTTP authentication to
protect resources on the server File access on the server (r/w) Handle file uploads from a client Session tracking
AIT 616 Fall 2002
PHP Summary Advantages
Fairly easy to learn Everything is built right into the language Cross platform Its Free!! Active user community
Disadvantages Language was designed to be programmer-friendly,
which unfortunately makes it a little less friendly to non-programmers
AIT 616 Fall 2002
PHP Summary Target Developer
Web developers on a budget who enjoy simple programming
PHP gives you a great deal of power with relatively simple code structure
May not be suited to very high end applications that aren’t supported by its built-in features
AIT 616 Fall 2002
PHP vs Perl Biggest advantage of PHP over Perl is that PHP
was designed for scripting for the web PHP is less confusing and a stricter format
without losing flexibility PHP is easier to integrate into existing HTML then
Perl PHP has pretty much all the ‘good’ functionality
of Perl Perl is a very tried and true language, its been
around since the late 80s, but PHP is maturing very quickly