What is PHP?
- PHP code is embedded within HTML pages for server side execution.
- Commonly used to extract data out of a database and present it on the Web page.
- A server-side interpreted scripting language used to enhance the capabilities of Web Pages.
- Open source product.
- Can be installed to run on Unix and Windows.
- Can run with either the Apache web server or the Microsoft IIS Web server.
- Derives it’s syntax from the C programming language.
- Syntax such as for loops and conditional tests is identical to that in javascript and java.
- PHP variable names begin with the $ symbol (similar as Perl).
Printing in PHP
<?php
print (
"Hello World");
echo ("Hello World");
$test = "Hello World";
printf("%s<br>",$test);
?>
printf Function
- Similar to the C function
- Facilitates the formatted output of data
- The format of variable types is set by means of a conversion specification or format string.
- Useful for printing floating point numbers to a specific number of decimal places.
- Pad out strings with spaces.
Functions in PHP
<?php
function printoutput($data)
{
echo $data."<br>";
}
$name="John Smith";
printoutput($name);
?>
Generating HTML using PHP
<html>
<body>
<?php
echo "<p style="color:blue”;>";
echo "Testing";
echo "</p>";
?>
</body>
</html>
Arrays in PHP
- Flexible
- Arrays can be multidimensional.
- Can store different data types.
- Can be set up so elements are accessed by an index number – indexed array.
- Can be set up so elements are accessed by a string – associative array
Indexed Array
- Indexed arrays have an integer subscript.
<?php
$characters=array("Andre", "Benny", "Christopher");
echo $characters[0]."<br>";
echo $characters[1]."<br>";
echo $characters[2]."<br>";
?>
- Contents of the array can have different datatypes
<?php
$data=array("Test",123,99.999);
echo $data[0]."<br>";
echo $data[1]."<br>";
echo $data[2]."<br>";
?>
Associative Arrays
- Allow access to the array elements using a string rather than an index number.
- Useful when processing the results of an SQL enquiry.
<?php
$person["name"]="Catherine";
$person["occupation"]="waitress";
echo "Name = ".$person["name"]."<br>";
echo "Occupation = ".$person["occupation"]."<br>";
?>
Passing Data from the Browser to the Server
- User data is passed from HTML web pages to the server using forms.
- The method used to send the data is specified as an attribute of the <form> tag.
- Two methods available – get and post.
- Get method – Appends the names of the variables after the URL.
- Post method – passes the data separately from the URL.
Sending Data via a Form
<form method="get" action="procform.php">
<input type="text" name="var1">
<input type="text" name="var2">
<input type="submit" value="Submit data">
</form>
Passing Variables into Server-side Scripts
- Data passed using the get method is stored in an associative array called $_GET. The index into the array is the variable name.
- A similar array ($_POST) is used for data submitted by the post method.
- The $_REQUEST array can be used instead of $_GET or $_POST. It contains both GET and POST data.
<?php
echo $_GET["var1"]."<br>";
$v1=$_GET["var1"];
echo $v1."<br>";
?>
Using $_REQUEST
<?php
echo $_REQUEST["var1"]."<br>";
$v1=$_REQUEST["var1"];
echo $v1."<br>";
?>
Hyperlink with GET variables
- When the PHP script is called by the web server, it receives the 2 GET variables.
<a href="procform1.php?var1=Test&var2=Me">Click me</a>
Selecting MySQL table data by using PHP
<?php
// connect to the server with the credentials else print the error.
$link = mysql_connect("john", "nancy","password")
or die ("Unable to connect to server: ".mysql_error());
// connect to the database else pring the error.
mysql_select_db("testdb",$link);
or die("Unable to connect to database: ".mysql_error();
$result = mysql_query("select * from tblroutes where price < 250");
$num_rows=mysql_num_rows($result);
echo $num_rows;
mysql_close($link);
?>
Sessions
- Stateless Protocol – When a page is retrieved through a web server, once the page has been sent, the Web Server does not retain any knowledge about the retrieval.
- When a PHP script that retrieves information from a database ends, the retrieved data as well as the script’s own variables are lost forever.
PHP Methods to Create Persistent Data
- Two main methods available whereby data can be saved:
Sessions
- Store variable names and their values
- Store the data in a file on the server.
- The default place where the data is stored is in /tmp for Unix. The location is set by the php.ini file.
- Sessions are created and resumed by using the session_start() function.
- Sessions are destroyed using the session_destroy() function.
- Variables (including arrays) are registered by using the session_register(variable_name) function.
Testing Variables
- isset($x) – test whether the variable exists – return true if it exists.
- empty($x) – test if the variable is empty – return true if it’s empty.