CSCU9B3 An Introduction to PHP (1) CSCU9B3 1 Web Processing in a Picture Server Side HTTP Database for persistence Content Client Side Browser Data Scripting on the server Content Data Scripting on the client Access server scripts from the client Cookies store some data Not Persistent About the PHP Language • PHP: Hypertext Preprocessor (earlier called Personal Home Page) – An HTML-embedded, server-side general purpose scripting language for web development • Syntax inspired by C – Curly braces, semicolons • Syntax inspired by perl – Dollar signs to start variable names, associative arrays • Extends HTML to add segments of PHP within an HTML file CSCU9B3 3 PHP Introduction • PHP is a scripting language – PHP is interpreted by the server at run time, so doesn’t need a compiler • Doesn’t need a development environment like Eclipse • You can use a simple text editor like TextPad – Create files: e.g. mydemo.php CSCU9B3 4 Running a PHP Program • The PHP program is run on the server, so no code is downloaded to the client • Only the output from the PHP program is sent to the client • The easiest way to run a PHP program and see its output is to put it on a (web) server and open it in a browser CSCU9B3 5 Getting Started • On Windows, you can download and install WAMP. With one installation and you get an Apache webserver, MySQL database server and PHP. http://www.wampserver.com • On Mac, you can download and install MAMP. http://www.mamp.info/en/index.html CSCU9B3 6 Starting a PHP Program <?php ?> • Anything inside these delimiters is interpreted as php • Anything outside them is sent straight to the client, usually the browser – Typically HTML / CSS / Javascript code CSCU9B3 7 Example 1 <HTML> <head></head> <body> <H1>This is a PHP demo</H1> <?php print(“This is the output from the PHP program”); ?> </body> </HTML> http://wamp0.cs.stir.ac.uk/jli/example1.php CSCU9B3 8 Example 2 <h1>Hello from Dr. Chuck's HTML Page</h1> <p> <?php echo "Hi there.\n"; $answer = 6 * 7; echo "The answer is $answer, what "; echo "was the question again?\n"; ?> </p> <p>Yes another paragraph. </p> PHP Program Structure • In common with many programming languages, PHP: – Ends each line with a semi-colon ; – Uses {} braces to delimit code blocks • PHP can be used in either a procedural or object-oriented way • PHP denotes variables with the dollar sign $a CSCU9B3 Autumn 2019 10 Comments • In PHP, we use // to make a single-line comment • or /* and */ to make a large comment block CSCU9B3 11 Naming convention of Variable • A variable name must start with a letter or an underscore ‘_’, not a number • A variable name can only contain alpha-numeric characters, underscores (a-z, A-Z, 0-9, and _ ) • A variable name should not contain spaces – If it is more than one word, it should be separated with an underscore ($my_string) or with capitalization ($myString) CSCU9B3 12 Operators • Operators are used to operate on values. There are four classifications of operators – – – – CSCU9B3 Arithmetic Assignment Comparison Logical 13 Operators CSCU9B3 14 Operators CSCU9B3 15 Operators CSCU9B3 16 Operators CSCU9B3 17 Variables • You assign variables like this: $a=5; • And compare them like this: if($a==5)… • You can use them in strings like this: print(“$a is the value of variable a”); CSCU9B3 18 Variable Types • PHP is pretty relaxed about variable types • You do not need to declare variables as being of a certain type, just start using them: <?php> $a=5; $a=“Hello”; ?> • PHP automatically converts the variable to the correct data type, depending on its value CSCU9B3 19 Checking Variables • You do not have to declare a variable before you use it, but you should • PHP will issue a warning (but still run) if you access a variable that is undeclared: $a=$b; // $b never declared • Use isset($var) to check if a variable has been set CSCU9B3 20 Conditional Statement: if • To execute some code only if a specified condition is true. • CSCU9B3 "Have a nice weekend!" if the current day is Friday 21 Conditional Statement: if…else • To execute some code if a condition is true and another code if a condition is false. CSCU9B3 22 Conditional Statement: if...elseif...else • The following example will output • • • "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!" CSCU9B3 23 Conditional Statement: switch • To select one of many blocks of code to be executed CSCU9B3 24 Arrays • If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: • However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? CSCU9B3 25 Arrays • An array is a special variable, which can store multiple values in one single variable • An array can hold all your variable values under a single name. You can access the values by referring to the array name • Each element in the array has its own index, so that it can be easily accessed CSCU9B3 26 Arrays • In PHP, there are three kind of arrays: • Numeric array - An array with a numeric index. Syntax: array(value1, value2, value3,...) • Associative array - An array where each ID key is associated with a value Syntax: array(key=>value, key=>value, key=>value,...) • Multidimensional array - An array containing one or more arrays CSCU9B3 27 Numeric Arrays • Two methods to create a numeric array • The index is automatically assigned (the index starts at 0) • The index is assigned manually: Numeric Arrays • Access the variable values by referring to the array name and index • The code above will output: Associative Arrays • With an associative array, each ID key is associated with a value • When storing data about specific named values, a numerical array is not always the best way to do it • With associative arrays we can use the values as keys and assign values to them CSCU9B3 30 Associative Arrays • In this example we use an array to assign ages to the different persons: • This example is the same as the one above, but shows a different way of creating the array: CSCU9B3 31 Associative Arrays CSCU9B3 32 Multidimensional Arrays • Sometimes you want to store values with more than one key • The dimension of an array indicates the number of indices you need to select an element • In a multidimensional array, each element in the main array can also be an array • And each element in the sub-array can be an array, and so on CSCU9B3 33 Two-dimensional Arrays • It is an array of arrays (a three-dimensional array is an array of arrays of arrays). $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array(“Audi",17,15) ); CSCU9B3 Name Stock Sold Volvo 22 18 BMW 15 13 Saab 5 2 Audi 17 15 34 Two-dimensional Arrays <?php echo $cars[0][0].": In stock: sold: ".$cars[0][2].".<br>"; echo $cars[1][0].": In stock: sold: ".$cars[1][2].".<br>"; echo $cars[2][0].": In stock: sold: ".$cars[2][2].".<br>"; echo $cars[3][0].": In stock: sold: ".$cars[3][2].".<br>"; ?> ".$cars[0][1].", ".$cars[1][1].", ".$cars[2][1].", ".$cars[3][1].", • The code will output Volvo: In stock: 22, sold: 18. BMW: In stock: 15, sold: 13. Saab: In stock: 5, sold: 2. Audi: In stock: 17, sold: 15. Name Stock Sold Volvo 22 18 BMW 15 13 Saab 5 2 Audi 17 15 Summary • Basic concepts • Operators • arithmetic, assignment, comparison, logical • Variables • Conditional statements • if, if…else, if…elseif…else, switch • Array • CSCU9B3 numeric array, associative array, multidimensional array 36