Introduction to PHP Basic principles and syntax What

Introduction to PHP Basic principles and syntax

What is PHP? l l PHP == ‘Hypertext Preprocessor’ Open-source, server-side scripting language Used to generate dynamic web-pages PHP scripts reside between reserved PHP tags l This allows the programmer to embed PHP scripts within HTML pages

What is PHP (cont’d) l l l Interpreted language, scripts are parsed at run-time rather than compiled beforehand Executed on the server-side Source-code not visible by client l l l ‘View Source’ in browsers does not display the PHP code Plethora of built-in functions allow for fast development Compatible with many popular databases

What does PHP code look like? l l Structurally similar to C/C++ Supports procedural and object-oriented paradigm (to some degree) All PHP statements end with a semi-colon Each PHP script must be enclosed in the reserved PHP tag <? php … ? >

Comments in PHP l Standard C, C++, and shell comment symbols // C++ and Java-style comment # Shell-style comments /* C-style comments These can span multiple lines */

Variables in PHP l l l PHP variables must begin with a “$” sign Case-sensitive ($Foo != $f. Oo) Global and locally-scoped variables l l l Global variables can be used anywher Local variables restricted to a function or class Certain variable names reserved by PHP l l l Form variables ($_POST, $_GET) Server variables ($_SERVER) Etc.

Variable usage <? php $foo = 25; $bar = “Hello”; $foo = ($foo * 7); $bar = ($bar * 7); ? > // Numerical variable // String variable // Multiplies foo by 7 // Invalid expression

Echo l The PHP command ‘echo’ is used to output the parameters passed to it l l The typical usage for this is to send data to the client’s web-browser Syntax l l void echo (string arg 1 [, string argn. . . ]) In practice, arguments are not passed in parentheses since echo is a language construct rather than an actual function

Echo example <? php $foo = 25; $bar = “Hello”; echo echo ? > l l l $bar; $foo, $bar; “ 5 x 5=“, $foo; “ 5 x 5=$foo”; ‘ 5 x 5=$foo’; // Numerical variable // String variable // // // Outputs Outputs Hello 25 Hello 5 x 5=25 5 x 5=$foo Notice how echo ‘ 5 x 5=$foo’ outputs $foo rather than replacing it with 25 Strings in single quotes (‘ ‘) are not interpreted or evaluated by PHP This is true for both variables and character escape-sequences (such as “n” or “\”)

Functions l l Functions MUST be defined before then can be called Function headers are of the format function. Name($arg_1, $arg_2, …, $arg_n) l l Note that no return type is specified Unlike variables, function names are not case sensitive (foo(…) == Fo. O(…))

Functions example <? php // This is a function foo($arg_1, $arg_2) { $arg_2 = $arg_1 * $arg_2; return $arg_2; } $result_1 = foo(12, 3); echo $result_1; echo foo(12, 3); ? > // Store the function // Outputs 36

The Big Picture for Assignment 2 l l Learn about web-servers Learn about Apache l l Learn about PHP l l l Download / Installation Configure Apache l l Download / Installation Modify and save the configuration file, then restart Learn about file I/O without the aid of SQL Design, write, and test scripts to solve Assignment 2

Saving Data in Text Files l PHP has built in functions for File I/O processing l l l fopen (. . ), fwrite(. . ), fclose(. . ), fflush(. . ), file_get_contents(. . ) Using these pre-made functions, File I/O in PHP is similar to that of C General Flow: l l l Open file Read data Modify data Write data Close file

Saving Data in Text Files: Example <? php $filename = 'test. txt'; /* Filename for writing. This is assumed to be in the same directory as the script */ $somecontent = "Add this to the filen"; // String to append to the file // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // Open the file in append mode ( ‘a’ ) so that the string being stored is written at the end of the // file rather than replacing the existing text if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to the opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ? >

For more information… l l l http: //www. php. net/manual/en/ http: //www. w 3 schools. com/php/default. asp http: //www. zend. com/zend/tut/
- Slides: 15