PHP (pptx)

advertisement
Introduction to Web Applications
With PHP
Jeff Offutt
http://www.cs.gmu.edu/~offutt/
SWE 432
Design and Implementation of
Software for the Web
Origins of PHP
• Rasmus Lerdorf – 1994
– Track visitors to his web site
• Open source
• Originally an acronym for Personal Home Page
– Now : Hypertext Preprocessor
• PHP is used for
– Form handling
– File processing
– Database access
1 July 2016
© Offutt
2
Overview of PHP
• Server-side scripting language
• Scripts are embedded in HTML tags
• Syntax looks similar to JavaScript
– But runs on the server, not the client
• Dynamically typed
– Variables take on the type of their current value
• Interpreted
1 July 2016
© Offutt
3
Where Does PHP Go?
• PHP documents look like HTML with code inside
– But these are NOT HTML and will not render inside a browser
• Internal to the file:
– <?php … ?>
• Or external :
– include (“myPHPStuff.inc”)
– Include files can have HTML and PHP
– The include file must put PHP statements in <?php … ?>
1 July 2016
© Offutt
4
PHP Syntax Basics
• Every variable name begins with a $
• Three syntax styles for comments
– // this is a comment
– # this is a comment (like Perl)
– /* this is a comment */
• Compound statements are formed with braces
• Compound statements cannot declare variables
Examples:
http://www.cs.gmu.edu/~offutt/classes/432/examples/php/
1 July 2016
© Offutt
5
Variables and Expressions
<?php
$age = 15;
$name = “George P. Burdell”;
$gpa = 3.14;
$age = $age + 1;
?>
1 July 2016
© Offutt
6
Arrays
<?php
$states [0] = “Virginia”;
$states [1] = “Kentucky”;
$states [2] = “Georgia”;
$matrix [7] [11] = 2.718;
$capitals [‘VA’] = “Richmond”;
$capitals [‘KY’] = “Frankfort”;
?>
1 July 2016
© Offutt
7
Functions and Control Structures
<?php
$len = strlen ($input); // function call
phpinfo (); // function call
while ($not_done)
{ …}
for ($i = 0; $i < 10; $i++)
{…}
?>
1 July 2016
© Offutt
8
Output Statements
<?php
print “Hi y’all doing?”;
printf (“%5.2f”, $price); // C syntax
?>
1 July 2016
© Offutt
9
Mixing PHP and HTML
<?php
if (strstr ($_SERVER [‘HTTP_USER_AGENT’],
“MSIE”))
{
?>
<b>You are using Internet Explorer</b>
<?php } else { ?>
<b>You are not using Internet Explorer</b>
<?php } ?>
1 July 2016
© Offutt
10
Handling Data From the Client
• These are often called “form variables” or “form data”
• Stored in arrays : $_GET, $_POST, $_COOKIE, $_REQUEST
<?php
Form data sent to
if (isset ($_POST [‘submit’])) {
print “Hello, ”.$_POST [‘name’]; this same PHP
} else {
?>
<form method=“post”
action=“<?php echo $_SERVER [‘PHP_SELF’]; ?>“>
Your name: <input type=“text” name=“name”> <br/>
<input type=“submit” name=“submit”>
</form>
String catenation
<?php
}
?>
1 July 2016
© Offutt
11
C-Like Syntax
<?php
for ($loop = -5; $loop < 5; $loop++) {
if ($loop < 0) {
echo “-”;
} elseif ($loop > 0) {
echo “+”;
}
echo “$loop<br/>\n”;
}
?>
http://hermes-web.vse.gmu.edu/~offutt/php/testslides.php
1 July 2016
© Offutt
12
C-Like Syntax (2)
<?php
$loop = 5;
while (--$loop) {
switch ($loop % 2) {
case 0:
echo “Even<br/>\n”;
break;
case 1:
echo “Odd<br/>\n”;
break;
}
}
?>
1 July 2016
© Offutt
13
Indexed and Associated Arrays
<?php
$a[0] = 1;
$a[1] = “hello”;
$isa [‘furniture’] = “chair”;
$isa [‘computer’] = “laptop”;
?>
• Sorting : sort(), rsort(), ksort(), usort(),
array_multisort()
• Traversing : reset(), end(), next(), each(), current(),
key(), array_walk()
1 July 2016
© Offutt
14
Functions
<?php
function logError ($user, $type=“error”)
{ print “User $user; $problem.\n”; }
?>
Function parameters can have default values
<?php
function head ($title=“Default Title”) { ?>
<html><head><title>
<?php echo $title ?>
</title></head></body>
<?php }
?>
1 July 2016
© Offutt
15
Dates and Times
<?php
print strftime (‘%A %b %e: day %j of %Y’);
?>
Output : Tuesday Oct 16: day 290 of 2012
<?php
$t = strtotime (‘now + 3 weeks’);
print strftime (‘%c, $t);
?>
Output : Tue Nov 6 15:32:49 2012
1 July 2016
© Offutt
16
String Manipulation
<?php
$str = “Fast String Manipulation”;
echo substr ($str, 0, 4) . substr ($str, -9);
?>
Output : Fastipulation
<?php
$a = explode (“:”, “This:string:has:delimiters.”);
while (list (,$value) = each ($a)) {
if (strcmp ($value, “has”) == 0) {
echo “had ”;
} else echo $value.” “; }
?>
Output : This string had delimiters
1 July 2016
© Offutt
17
Regular Expressions
<?php
if (preg_match (‘/^\d{5}(-\d{4})?$/’,
$_POST [‘zip’]))
{…}
?>
<?php
$text = “Hello, /George/. How are you?”;
print preg_replace (‘{/([^/]+)/}’, ‘<b>\\1</b>’,
$text);
?>
1 July 2016
© Offutt
18
Reading and Writing Files
<?php
$file = fopen (“inputdata.txt”, “r”);
while (!feof ($file)) {
echo fgets ($file), “<br/>”;
}
?>
<?php
$file = fopen (“outputdata.txt”, “a”);
fputs ($file, $HTTP_USER_AGENT.”\n”);
?>
Note : File must be readable and writable
by PHP, not you as a user.
Use the complete path.
1 July 2016
© Offutt
19
PHP Summary
• Easiest way to get started with Web software
• Some disadvantages, especially for building large-scale,
high-quality web applications
• Very hard to debug
– Syntax errors do not result in messages, just a blank screen
– The syntax is very unforgiving … some structures will not run
with a space, and some won’t run without
– Hard to separate mistakes in PHP, HTML, and deployment
• These slides are just a brief overview … lots more details
in the book
Home : http://www.php.net
1 July 2016
© Offutt
20
Download