Upload
phillip-lamb
View
218
Download
3
Tags:
Embed Size (px)
Citation preview
PHP Basics
This presentation covers the key features of PHP without getting into the way we use it in collaboration with MySQL
Early in semester two there will be an online quiz covering the material presented here.
The questions will be mainly multiple choice.
You will complete the quiz as an in-class test.
It will contribute 10% towards the coursework marks.
TopicsWhat is PHP?
How does it work? What does a PHP script look like? Passing data to and from PHP
Programming with PHP Variables, Loops, Conditionals,
Arrays, Functions etc.
What is PHP?PHP Hypertext Pre-processor
Project started in 1995 by Rasmus Lerdorf as "Personal Home Page Tools"
Taken on by Zeev Suraski & Andi Gutmans
Current version PHP 5 using Zend 2 engine (from authors names)
Very popular web server scripting application Cross platform & open source Built for web server interaction
How does it work? PHP scripts are called via an incoming HTTP request
from a web client Server passes the information from the request to PHP
The PHP script runs and passes web output back via the web server as the HTTP response
Extra functionality can include file writing, db queries, emails etc.
HTMLHTML
HTTP
Web serverClient
What does a PHP script look like?
A text file (extension .php) Contains a mixture of content and programming
instructions Server runs the script and processes delimited blocks
of PHP code Text (including HTML) outside of the script delimiters
is passed directly to the output e.g.<html><head><title>Simple PHP</title></head><body> <? for ($i=0; $i<10; $i++) { print("<p>Hello World!! </p>"); } ?></body></html>
Code inside <? ... ?> delimiters(or <?php ... ?>)
Programming with PHPPHP uses common language structures
Similar syntax to Perl, JavaScript etc
PHP is interpreted (not compiled) No special tools needed
Each programming statement must end with a semi-colon e.g. <?
$name="Paul"; print("<p>$name</p>");?>
VariablesPlaceholders to store data values
Strings (text), numbers, Boolean (true/false) etc No need to declare type or name before use
$myname = "Paul";
$myAge = 21;
Variable names are case-sensitive
All variable names start with a dollar sign $
String values assigned in quotes
OperatorsOperator Performs Example Output
+ Addition print(2+2); 4
- Subtraction print(10-4); 6
* Multiplication print(4*2); 8
/ Division print(12/4); 3
% Modulus print(12%5); 2
++ Increment $value = 10;
print(++$value);
11
-- Decrement $value = 10;
print(--$value);
9
Evaluation ExpressionsExpression Performs Example Output
< Less than if(2<4){
print("ok");}ok(returned true)
> Greater than if(2>4){
print("ok");}
Nothing(returned false)
<= Less than or equal to
if(4<=4){
print("ok");}ok(returned true)
>= Greater than or equal to
if(2>=2){
print("ok");}ok(returned true)
== Equal to if(2==4){
print("ok");}
Nothing(returned false)
!= Not equal to if(2!=4){
print("ok");}ok(returned true)
Loops3 basic ways for repeating code statements
For Execute for a defined
number of repetitions
While If condition is true,
run code and check again. Stop when condition fails
Do While Execute code once, then
check if condition is true. If not, run code again
for ($i=0; $i<10; $i++){ print("Hello World"); }
while ($age < 18) { print("Try again son"); }
do { print("Please log in"); }while ($loggedIn == "no");
Conditional StatementsUsed to evaluate a condition
and act on the outcome if
Condition returns true or false. Use with else to offer alternative
switchCheck against a seriesof possible outcomes
if ($stockLevel<$numRequired){ print("Try ordering less"); }else{ print("Now you can pay!"); }
switch($loggedIn){ case "Yes": print("Welcome!"); break; case "Failed": print("Please try again"); break; default: print("Please login");}
Single or multi-dimensional data structure for storing related values
Built-in arrays used to collect web data $_GET, $_POST etc.
Many different ways to manipulate arrays You will need to master some of them
Arrays
$cars = array("Ford","Reliant","Toyota","Chrysler");
Index/Key 0 1 2 3
Ford Reliant Toyota Chrysler
Array
$cars ……..
Accessing Array DataBy key/index
Loop through
Use print_r()
<p><?=$cars[0]; ?></p> Ford
for ($i=0; $i<sizeof($cars); $i++){ print("<p>$cars[$i]</p>");}
FordReliantToyotaChrysler
print_r($cars);
Array ( [0] => Ford [1] => Reliant [2] => Toyota [3] => Chrysler )
FunctionsNamed blocks of re-usable code called when
needed
function welcomeTo($thisUser){
if($loggedIn == "Yes"){ $outPut = "Welcome $thisUser"; } else { $outPut = "Sorry $thisUser is not logged in"; } return $output;
}
... ...
<p><?=welcomeTo("Dave"); ?></p>
Function name Input parameter
Function called by name
Return statement
Statements to run
Built-in FunctionsPHP has hundreds of predefined functions
ready to use e.g. String manipulation
e.g. Change "Hello World" to "Hello Springfield"
Mathematical functions e.g. Print the square root of 25
Date and time functions (can get complex) e.g.
www.php.net/manual/en/funcref.php
$text = str_replace("World","Springfield","Hello World");
print(sqrt(25));
print (date("l jS F Y"));
Monday 3rd December 2007
Handling Text StringsStrings can be double or single quoted
Single quoted strings – simplest Escape characters needed, variables must be
concatenated
Double quoted strings More versatile, can include more special characters,
variables can be parsed and expanded
$thisUser = "Dave";
print('<p>The username '.$thisUser.' isn\'t on the list</p>');
$thisUser = "Dave";
print("The username $thisUser isn't on the list");
Passing data to a PHP scriptA PHP script can receive data from the HTTP
request that called it via either GET or POST Includes data supplied by web forms
The script can also extract data from the server environment at run-time
e.g. dates, filenames, IP addresses etc.
Once running a script can also access data from many other sources
e.g. databases, text files, XML documents etc.
A PHP script does not need to have data passed to it in order to run
Output from PHP scriptsBy default output is returned to the web server
as the response to the initial HTTP request Content outside delimiters returned automatically
Other output is programmatically returned e.g. via the echo statement or print() function
A PHP script can also output data to other locations
e.g. databases, text files, XML documents, graphics formats etc
print("<p>server side scripting</p>");
echo "Hello";
Further TopicsPHP and the web environment
Superglobal variables HTTP input (GET and POST) Environment variables phpinfo()
Persistent information Session variables
Working with date and time
Superglobal VariablesEvery PHP script has automatic access to a
range of values at run-time e.g. Web request information
Collected from the HTTP headers e.g. from input, remote IP address etc.
Info about the PHP/server environment Generated at run-time e.g. date/time, file system info
Stored in pre-defined arrays – initially most important are:
$_GET $_POST $_SERVER
$_GETEvery HTTP request is a GETGET can also be used to pass data to server
in querystring Name/Value pairs appended to URL requested Hard coded /generated from HTML form input
PHP puts data into superglobal array $_GET
$_POSTExtra data can be sent as an HTTP POST
Name/Value pairs passed in request body Only generated from HTML form input
PHP puts data into superglobal array $_POST
$_SERVERArray generated by the server at run time
Request information from HTTP headers Referrer, accept headers, user agent info etc.
Server environment data IP address, file/script names & paths etc
Access by name or iteration e.g.$possBrowser = $_SERVER['HTTP_USER_AGENT'];
foreach ($_SERVER as $key => $value) { print("<b>$key</b> $value<br />\n"; }
phpinfo()Exact makeup of PHP environment depends
on local setup & configurationBuilt-in function phpinfo() will return the
current set up Not for use in scripts
Useful for diagnostic work & administration May tell you why something isn't available on
your server
print(phpinfo());
Persistent InformationWeb requests are stateless and anonymous
Information cannot be carried from page-to-page
Client-side cookies can be used Not everyone will accept them
Server-side session variables offer an alternative
Temporarily store data during a user visit/transaction
Access/change at any point
PHP handles both
Session VariablesNot automatically created
Script needs to start session using session_start()
Data stored/accessed via superglobal array $_SESSION
Data can be accessed from any PHP script during the session
session_start();$_SESSION['yourName'] = "Paul";
Script1: Start session and set value
print("Hello $_SESSION['yourName']");
Script 2: Access session and retrieve value
Date and TimeBuilt-in functions to extract, format and
calculate dates & times – can be trickyFirst step usually to obtain the current date
using getdate()
Returns array with current date/time info
Other date/time functions for format and display e.g.
$currDate = getdate();
date("l dS of F Y h:i:s A");
Monday 3rd of December 2007 09:51:38 AM