Download presentation
Presentation is loading. Please wait.
Published byBryan Manning Modified over 9 years ago
1
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan
2
Some PHP Fundamentals PHP 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).
3
PHP (cont.) To start writing a PHP script, surround the code with: <?php // insert code here ?> Whatever is written between the will be executed by the PHP interpreter running on the web server. Comments are specified using // and are ignored by the PHP interpreter.
4
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.
5
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 ' '; echo ' Test Page '; echo ' '; $var1 = 'hello'; $var2 = 'world'; $var3 = 1; $var4 = 2; echo $var1. ' '. $var2; echo ' '; echo $var3 + $var4; echo ' '; ?>
6
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
7
PHP (cont.) PHP also has support for arrays: $array1 = array(1 => 2, 'hello' => 'world'); echo ' '; echo $array1[1]; echo ' '; echo $array1['hello']; $array1[5] = 'something'; echo ' '; echo $array1[5]; $array1[] = 'end of array'; echo ' '; echo $array1[6];
8
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
9
PHP (cont.) While loops: while (expression) { // do something } do { // do something } while (expression)
10
PHP (cont.) for (expression 1; expression 2; expression 3) { // do something } foreach ($array1 as $item) { // do something with $item }
11
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 " Retrieved the following data from the MySQL Database based on last name = $lastname: "; $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)) {
12
PHP (cont.) print $item['last_name']; print " "; print $item['first_name']; print " "; print $item['age']; print " "; } ?>
13
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.