intro to php.ppt [read-only]englishonlineclub.com/pdf/php - hypertext preprocessor... · microsoft...
TRANSCRIPT
![Page 1: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/1.jpg)
PHP
PHP: Hypertext Preprocessor
![Page 2: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/2.jpg)
The Plan
Get familiar with php, and how it works together with html
Make a fun projectDo some research
![Page 3: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/3.jpg)
PHP is an interpreted language
The interpreter reads and executes commands
![Page 4: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/4.jpg)
print statements� Basic construct for output� Double quotes or single quotes� Semi-colon finishes the
statementprint “Double Quotes”;print ‘Single Quotes’;
![Page 5: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/5.jpg)
Print (cont)
•Certain characters must be escaped• Double quotes - \”• Single quotes - \’• Dollar sign - \$• Backslash - \\
print “She said, \”The coat cost \$79.95\””;
![Page 6: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/6.jpg)
Try it!
• Application -> Accessories-> Terminal• Open a terminal and typephp -r 'print "hello\n";'
and hit Enter• type multiple print statements inside the
single quotes.
How can we run it without typing it in over and over?
![Page 7: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/7.jpg)
Write it in a file
• In a terminal, type gedit hello.php &• Open with <?php and close with ?>, put
print in between.<?php print “Hello!\n”; ?>
• Save the file• In a terminal, type php hello.php
How do we get it into a web page?
![Page 8: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/8.jpg)
Motivation for PHPon the web
1. We want many people to be able to run our program
2. Our program uses lots of data and does lots of computation.
Computation happens on server side. Server sends html to client
ClientServer
![Page 9: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/9.jpg)
<html><head><title>My First PHP Script</title>
</head><body>This web page contains html and php.
<?phpprint “<h1>Hello!</h1>”;?></body></html>
![Page 10: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/10.jpg)
Comments??
• It’s a good idea to add comments to your code
• A comment is text that humans can read and computers ignore
• Helps you remember what your code does
• Helps other people who want to use your code understand it.
![Page 11: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/11.jpg)
Comments
• Single line// undetected errors are ignored
• Multi-line/* Either the program is exiting or the world is coming to an end. Take your pick. */
![Page 12: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/12.jpg)
Exercise
1. In your terminal type cd /var/www/workshop
2. Type gedit hello.php &3. Open browser to
http://localhost/workshop4. Click hello.php
![Page 13: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/13.jpg)
Variables
Variables hold data All variables are represented with a dollar sign
and a name: $var Variables names represent memory
locations Variable names contain letters, numbers and underscores
only Cannot start with a number
Seven types of data: bool, string, int, float, array, object, resource
$var
5
![Page 14: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/14.jpg)
Variables Assignment
Assignment is done with the assignment operator =
$var = data;
$var gets set to data;
10$var
5
10$var
![Page 15: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/15.jpg)
Bad Variable Names
$123_go! = 4;
$data = 3;
$IsNotCanNotUnDisable = 1;
$l = 1; Good variable names describe the data they contain
average_rainfall = 32;
![Page 16: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/16.jpg)
Variables – integer
Whole numbers.
Some Operations$c = $a + $b;
$c = $a - $b;
$c = $a * $b; Multiplication$c = $a / $b; Assigns quotient$c = $a % $b; Assigns remainder$c++; $c = $c + 1;
increment $c
![Page 17: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/17.jpg)
Variables - string Composed of letters,
numbers, characters on the keyboard
Surrounded by quotes Certain characters must
be escaped The escape character is
the forward slash Single quotes inside
single quotes must be escaped
Double quotes inside double quotes must be escaped
Single quotes \’
Double quotes \”
Dollar sign \$
Backslash \\Carriage Return(new line)
\n
![Page 18: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/18.jpg)
Why $ is escaped
When within quotes, if $ is not escaped, php treats it as a variable, evaluates it and prints the value//$name is evaluated inside “ ”$name = “Spot”;print “My name is $name”;
![Page 19: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/19.jpg)
String operator Concatenation
Different types of data can be merged together into a single string
$var = “Hello”;print $var . “ World”;
![Page 20: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/20.jpg)
php -r
‘print “The coat cost \$79.95”;’
php –r ‘print “\\0/\n \$\n/ \\”;’
php –r ‘$location = “Buffalo”; print “I live in ” . $location;’
Concatenation or period?
Try these in a terminal
![Page 21: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/21.jpg)
Variables – float
float represents rational (real) numbers
$price = 7.95;
What operations can we do on floats?
$c = $a + $b;
$c = $a - $b;
$c = $a * $b;
$c = $a / $b;
$c = $a % $b;
$c++;
![Page 22: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/22.jpg)
Exercise
1. In your terminal type cd /var/www/workshop
2. Type gedit variables.php &3. Open browser to
http://localhost/workshop4. Click variables.php
![Page 23: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/23.jpg)
Variables boolean
Most basic type of data in any computer language
The only values it can have are true and false
$I_chopped_down_the_cherry_tree = true;
$I_am_over_five_feet = false;
George Boole
![Page 24: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/24.jpg)
Comparison Operators
(Left-hand side operator right-hand side) Evaluate to true or false (booleans!!)(50 > 5) (5 < 1) (10 <= 12)(10.2 >= 10)
![Page 25: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/25.jpg)
Comparison OperatorsEqual ==
Identical === Equal value, and same type
Not equal !=
Not Identical !== Not equal value, or not same type
Greater than >
Less than <
Greater than or equal >=
Less than or equal <=
![Page 26: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/26.jpg)
What is it?
$price = 5.60;
$five_greater_than_three = ( 5 > 3);
$data = 10;
$data = false;
$print = “print”;
print $print.$price;
![Page 27: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/27.jpg)
Variables - array Arrays are a collection of data with
memory addresses All data types can be stored in an array$names = array(“Matt”, “Ann”, “Jim”);
$numbers = array(2, 4, 6, 8);
Elements are accessed with a keyusing the array operatorsprint $names[0]; // Matt
2 4 6 80 1 2 3
Matt Ann Jim0 1 2
![Page 28: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/28.jpg)
Variables –associative array By default, keys are integers starting at 0 Instead of integers, keys can be strings$ages = array(“Matt” => 20, “Ann” =>
19, “John” => 21);
Alternate way to build an array, using single statements:
$ages[“Alisa”] = 30; // I wish
Elements are accessed the same way
print $ages[“Ann”]; // 19
![Page 29: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/29.jpg)
Associative Array Example: Form Data• Sometimes a web application requires input from a user• HTML form tags for input
-textbox-checkbox
• Data sent to the server as associative array named GET or POST-POST: input data hidden from the user
-GET: input data is part of the URL string
![Page 30: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/30.jpg)
Form Data<html>
<head>
<title>PHP and Form Data</title>
</head>
<body>
<form action="formdata.php" method=”GET">
Number: <input type="text" name="number" /> <br />
Letter: <input type="text" name="letter" /> <br />
<input type="submit" name="submit" value="Submit Data" />
</form>
<?php //If form is submitted echo Data
if ( isset($_GET['submit']) ){
echo "<b> Number: </b>" . $_GET['number'] . "<br />";
echo "<b> Letter: </b>" . $_GET['letter'];}
?>
</body>
</html>
![Page 31: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/31.jpg)
Loops: motivation
• In a program, there’s often a sequence of actions that need to be done over and over (a loop!)
• Arrays and loops go together like peanut butter and jelly
Next: need some volunteers..
![Page 32: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/32.jpg)
CodeBlock
Loops - for Three parts to a for loop Variable Initialization Sentinel Condition Variable Modification (iteration)
$booknames = array(“Calculus”,”Algorithms”, “High Performance Computing”, “Statistics”);
for( $i = 0; $i < 4; $i++ )
{
say “add ” . $booknames[$i] . “ please”;
add a book;
jump over pile;
}
![Page 33: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/33.jpg)
Loops - foreach
Used to iterate over arrays
foreach($array as $value){print $value;
}
foreach($array as $key => $value){
print $array[$key]; //valueprint $key;print $value;
}
![Page 34: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/34.jpg)
Exercise
1. In your terminal type cd /var/www/workshop
2. Type gedit arrays.php &3. Open browser to
http://localhost/workshop4. Click arrays.php
![Page 35: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/35.jpg)
If and Else Control structure – changes the flow of the
script A way to choose between possible actions Tests whether an expression is true and if so,
does the code immediately following Multiple if statements that are mutually
exclusive, can be combined with else ifand else
if ( condition is true ){// do something
}
![Page 36: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/36.jpg)
$age = 21;
if ($age >= 17){print “You can drive!”;
}else if ($age >= 16){print “You are almost old enough to drive!”;
}else{print “You are not old enough to drive.”
}
![Page 37: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/37.jpg)
If and Else Flowchart
If ($age >= 17)If ($age >= 17)
Else if ($age >= 16)Print “You are old enough to drive!”
Print “You are almost old enough to drive!”
Else
Print “You are too young to drive.”
True False
True False
![Page 38: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/38.jpg)
Logical Operators Multiple conditionals can be
combined using logical operators AND and OR
Conditionals can be negated using the not logical operator - !
$age = 20;
if ($age > 18 AND $age < 21) {
// go straight to college OR
// get a job
}
![Page 39: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/39.jpg)
Boolean Algebra
Input Outputtrue AND true true
true AND false false
false AND false false
true OR true true
false OR true true
false OR false false
(true AND true) OR false true
![Page 40: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/40.jpg)
Exercise
1. In your terminal type cd /var/www/workshop
2. Type gedit boolean.php &3. Open browser to
http://localhost/workshop4. Click boolean.php
![Page 41: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/41.jpg)
ReviewWhat’s wrong with it?$radius = 5
if($radius >= 5); {print $radius;}
$myarray == array(6,7,8,9);
foreach ($myarray as $value {
print $myarray;}
if ($age = 18) { print $age }
print “She said “hi”;
for(i=0; i<5; i++){print “*”;}
![Page 42: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/42.jpg)
Functions
A function ƒ takes an input, x, and returns an output ƒ(x). Like a "machine" or "black box"
that converts the input into the output.
http://www.youtube.com/watch?v=_ve4M4UsJQo&feature=related
![Page 43: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/43.jpg)
Functions
Piece of code that performs a task you want to do multiple times, or share with others
A function is called by specifying function name Any inputs it may require
It may return a value (output)exit(); // simple function callprint “This won’t show up”;
![Page 44: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/44.jpg)
Functions – User Defined//function declaration
function name( parameters )
{
// do something // return a variable or value
}
Function names follow the same rules as variables Letters, numbers, underscore Must start with a letter or underscore
![Page 45: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/45.jpg)
Input Arguments In the function code, you use
“formal” parameters. Variable-like names, start with $
In function call you use “actual”parameters Can be a variable or a value
Let’s take a look.
![Page 46: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/46.jpg)
// arg: $number a number to square
// return: the square of the $number
function square($input)
{
$number_squared = $input*$input;
return $number_squared;
}
$number = square(10);
print $number;$number = square($number); //what happens?
print $number;
![Page 47: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/47.jpg)
Computer Architecture
• Why do formal parameters and actual parameters have different names?
$y = array(2, 4, 6);$x = average($y);
Command Stack
Memory
+ - * / % Operator hardware
Operands
![Page 48: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/48.jpg)
Function Calls
1) Save your place in the program.2) Fetch the parameter values.3) Fetch the function commands.4) Run the function using local memory
addresses$x = average($y);
$sum = 0;2 4 6
$y
![Page 49: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/49.jpg)
Function Call
5) Put the return value in memory6) Do the assignment statement7) Continue in program
$x = average($y);
print $x;
2 4 6 4
$y $ave
![Page 50: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/50.jpg)
Sharing Your Functions
1. Copy your function into a separate php file, inside <?php ?>, e.g. myfunction.php
2. In an HTML file, just within <?php
require_once (‘myfunction.php’);
![Page 51: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/51.jpg)
Exercise:Summary Statistics
You’ll be writing functionsto calculate average, and, if you wish standard deviation.
Normal Bell Curve
What is this useful for?
![Page 52: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/52.jpg)
Formulas
Average = (1/number_of_things) * (sum_of_all_things)
Stdev =√ [(1/number_of_things) *sum_of_all ((thing – average)2 )]
http://en.wikipedia.org/wiki/Standard_devtion
![Page 53: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/53.jpg)
What is it?if ($color == “blue”)
{ print $color; }
$blue = “001”;
$number = square(10);
foreach($line as $key => $val) {print $key;}
print_r($array);
for($i=0; $i<4; $i++ ) {$i = $i- 1;}
function silly($thing) { return “silly $thing”;}
$silliness = silly(“wabbit”);
![Page 54: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/54.jpg)
Parsing
• Scientific research produces data• Numbers• Strings describing RNA
sequences• The data gets saved in files• If we parse the files we can do
analysis or visualization
intransitive verb1 : to give a grammatical description of a word or a group of words
![Page 55: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/55.jpg)
Some helpful functions for parsing in PHP
// open and read a file into an array of lines
$lines = file('playlist.tsv‘,FILE_SKIP_EMPTY_LINES);
//turn tab-separated string into array
$arr = explode(“\t",$mystring);
// turn array into a comma-separated string
$line = implode(",",$myarray);
![Page 56: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/56.jpg)
Helpful Array functions
print_r($my_array); // no return value
$array_length = count($my_array);
// returns length of array
![Page 57: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/57.jpg)
Jukebox!!
1. In your terminal type cd /var/www/workshop
2. Type gedit parsing.php &3. Open browser to
http://localhost/workshop/4. Click parsing.php
![Page 58: intro to php.ppt [Read-Only]englishonlineclub.com/pdf/PHP - Hypertext Preprocessor... · Microsoft PowerPoint - intro_to_php.ppt [Read-Only] [Compatibility Mode] Author: djm29 Created](https://reader035.vdocument.in/reader035/viewer/2022070107/6021e22261e7956124190fa5/html5/thumbnails/58.jpg)
11 2 3
4 5 6
foreach($row as $rkey => $rvalue){$cols = $rvalue;
foreach($cols as $ckey => $cvalue){print $cvalue;
}}
Doubly Nested Loops and Tables