php intro/overview squirrel book pages 33-87. server-side scripting everything you need to know in...
Post on 20-Dec-2015
214 views
TRANSCRIPT
![Page 1: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/1.jpg)
PHP Intro/Overview
Squirrel Book pages 33-87
![Page 2: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/2.jpg)
Server-side Scripting
Everything you need to know in one slide1.Web server (with PHP “plug-in”) gets a URL
request: http://www.site.com/page.php
2.Web server see’s that the page is .php
3.PHP Pre-processor searches the page for PHP code and executes the code.
4.Web server sends back the output of the PHP code, not the code itself.
![Page 3: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/3.jpg)
PHP Basics
• PHP code can go anywhere in an HTML document as long as its in a PHP tag
• Example
<h1><?php echo “Hello World”; ?></h1>
![Page 4: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/4.jpg)
PHP Variables
• Variables do NOT have strict typing • Unless math operations are used, variables are
strings by default• Variables must start with $
<?php
$x = “Hello World”;
echo $x;
?>
![Page 5: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/5.jpg)
Commenting
• 3 Types
// Comment
# Comment
/* CommentComment */
![Page 6: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/6.jpg)
Print and Echo
• Print can print strings
print “42”;
• Echo is more robust than print, it can print numbers and more than one parameter, separated by commas
echo 42; // This will actually print 42$x = “is”echo “The answer ”, $x, “ ”, 42;
![Page 7: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/7.jpg)
Single Quotes vs. Double
• These are the same…
print “This works.”;print ‘This works also.’;
• This makes it easy to print quotes
print “Here is a ‘single quote’ ”;print ‘Here is a “double quote” ’;
![Page 8: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/8.jpg)
New lines
• The string variable $var contains a new line character “Hello there\n sir!”
$var = “Hello there
sir!”;
• Introducing new lines breaks makes it easer to write SQL queries that work…
![Page 9: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/9.jpg)
SQL Example
$query = “ SELECT max(orderid)FROM orders
WHERE custid = $ID”;
• If you send this query string to some database servers it is important that the new line characters are in the string.
![Page 10: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/10.jpg)
Variables in strings
$x = 256
// “My computer has 256 MB ram.”$message = “My computer has $x MB ram.”;
// “My computer has.” $message = “My computer has $xMB ram.”;
Why?
![Page 11: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/11.jpg)
Variables in strings
$x = 256
// “My computer has.” $message = “My computer has $xMB ram.”;
// “My computer has 256MB ram.” $message = “My computer has {$x}MB ram.”;
![Page 12: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/12.jpg)
Variables in strings
Using { } is very important when trying to include complex variables into other strings.
$message = “Mars has a diameter of {$planets[‘Mars’][‘dia’]}.”;
![Page 13: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/13.jpg)
Variables
• Integers
$var1 = 10;
• Floats
$var2 = 6.1;
• Boolean
$var3 = true;
• String
$var4 = “true”;
$var5 = ‘true’;
$var6 = ‘10’;
$var7 = “6.1”;
![Page 14: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/14.jpg)
Constants
define(“PI”, 3.14159);
print PI;
// outputs 3.14159
Notice that constants don’t have $Convention: Use all CAPS for constantsBTW, PHP is case sensitive
![Page 15: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/15.jpg)
Expression and Operators
• Same as most high-level languages$z = 3;$x = 1;$y = 5.3;$z = $x + $y;$z++;$x += $y + 10;$z = $x * 10;
![Page 16: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/16.jpg)
String concatenation
• Not the same as JavaScript!
$var1 = “Hello”;
$var2 = “ world”;
$var3 = $var1 + $var2; //This won’t work
$var3 = $var1 . $var2;
![Page 17: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/17.jpg)
Conditionals (if statements)
• Same as other high-level languages
if ($var < 5) {
print “The variable is less than 5”;
}
![Page 18: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/18.jpg)
Compound conditionals
if ($x == 1) {print “x is 1”;
}elseif ($x == 2) { // Notice elseif is one word
print “x is 2”;}else {
print “x is not 1 and not 2”;}
![Page 19: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/19.jpg)
Loops
• While
$c = 0;
while ($c < 10) {
print $c . “ ”;
$c++;
}
• For
for ($c = 0; $c < 10; $c++) {
print $c . “ ”;
}
![Page 20: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/20.jpg)
Loops: break
for ($c = 0; $c < 10; $c++) {
print $c . “ ”;
if ($c == 5)
break;
}
![Page 21: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/21.jpg)
Type Conversion
$year = 2003; // This is an integer
$yearString = strval($year);
// $yearString is “2003” not an integer
![Page 22: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/22.jpg)
Type Conversion
• string strval(any other datatype)
• integer intval(any other datatype)
• float floatval(any other datatype)
![Page 23: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/23.jpg)
Implicit type conversion
• $var = “100” + 15;
• $var = 100 + 15.0;
• $var = 39 . “ Steps”;
• $var = 39 + “ Steps”;
![Page 24: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/24.jpg)
Implicit type conversion
• $var = “100” + 15; // $var becomes int
• $var = 100 + 15.0; // $var becomes float
• $var = 39 . “ Steps”; // $var becomes string
• $var = 39 + “ Steps”; // error
![Page 25: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/25.jpg)
Functions
// Function definitionfunction fname ($parameter1, $parameter2) {
code;code;…return $returnvalue;
}
// Function call$x = fname(1,2);
![Page 26: PHP Intro/Overview Squirrel Book pages 33-87. Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a](https://reader035.vdocuments.us/reader035/viewer/2022062714/56649d415503460f94a1b73c/html5/thumbnails/26.jpg)
Variable Scope (visibility)
• Same as Java• Same as C++
function fun($x) {$temp = $x + 1;
}fun(5);print “temp is: $temp”;