NMED 3850 AAdvanced Online Design
January 26, 2010V. Mahadevan
Some PHP FundamentalsPHP code is always written in a plain text file
saved with the extension “.php”.The web server will execute this file and
return the results to the client.Database connectivity and business logic are
implemented in the PHP code.Look and feel elements are typically
implemented using Cascading Style Sheets (CSS).
CSS can be static or combined with PHP to dynamically set look and feel elements (more on this later).
PHP (cont.)
To start writing a PHP script, surround the code with:<?php// insert code here?>
Whatever is written between the <?php and ?> will be executed by the PHP interpreter running on the web server.
Comments are specified using // and are ignored by the PHP interpreter.
PHP (cont.)
Variables: an identifier for storing and accessing data items within a PHP script.
Variable are declared using the “$” sign and an identifier name:$var1;$var2;
Variables can be assigned values as follows:$var1 = 'hello';$var2 = 'world';$var3 = 1;
The keyword “echo” will print out the value of the variable.
PHP (cont.) Remember that anything you print out (echo) using a PHP script will be rendered by
the web browser, so it must be properly formatted HTML code.<?php
echo '<html>';echo '<head>';echo '<title> Test Page </title>';echo '</head>';
$var1 = 'hello';$var2 = 'world';$var3 = 1;$var4 = 2;
echo $var1 . ' ' . $var2;echo '<br>';echo $var3 + $var4;
echo '</html>';
?>
PHP (cont.)
Variables can have different types: Strings: sequences of text / characters Numbers: integer, float Boolean: TRUE, FALSE
PHP determines the type at runtime; there is no need to explicitly predefine the type. $var1 = ‘hello’; // string type $var2 = 1; // number (integer) type $var3 = TRUE; // Boolean type
PHP (cont.) PHP also has support for arrays:
$array1 = array(1 => 2, 'hello' => 'world');echo '<br>';echo $array1[1];echo '<br>';echo $array1['hello'];$array1[5] = 'something';echo '<br>';echo $array1[5];$array1[] = 'end of array';echo '<br>';echo $array1[6];
PHP (cont.) If (expression) {
// do something} else if (expression) {
// do something else} else {
// do something else}
Expressions can be of the form:($a == $b) // test equality($a > $b) // test greater than
PHP (cont.) While loops:
while (expression) {// do something
}
do {// do something
} while (expression)
PHP (cont.) for (expression 1; expression 2; expression 3)
{// do something
}
foreach ($array1 as $item) { // do something with $item}
PHP (cont.)<?php$hostname = 'localhost';$username = '';$password = '';
$connection = mysql_connect($hostname, $username, $password) or die ('Connection error!!!');
$database = 'peopledb';mysql_select_db($database);
$lastname = $_POST["last_name"];
print "<h1> Retrieved the following data from the MySQL Database based on last name = $lastname: </h1>";
$execute_statement = "SELECT * FROM person WHERE last_name='$lastname'";
$results = mysql_query($execute_statement) or die ('Error executing SQL statement!!!');
while($item = mysql_fetch_array($results)){
PHP (cont.) print $item['last_name']; print "<br>"; print $item['first_name']; print "<br>"; print $item['age']; print "<br><br>";}
?>
Lab Write 2 PHP scripts:
1. This script should generate a web page with a color background and text of various sizes / styles.
2. This script should read a database table and output the data using a HTML table.