2. Part 2: Getting Started with PHP Hello World Syntax Rules Escaping Characters Reserved Words Variables Data Types Functions Function Arguments Multiple Functions Variable Scope Multiple Arguments 2
3. Exercise 2.1 – Hello World Version 1 <?php echo “Hello World”; ?> Version 2 <html> <?php echo “<h1>Hello World</h1>”; ?> </html> Notes: Use of <?php opening tag instead of <? ; // semi-colon at end of statements Mixing of html and php code generally frowned upon in professional programming but ok for small projects helloworld.php 3
4. Syntax Rules - Comments PHP code is generally case insensitive Use of semi-colon at the end of statements // used for single line comments # used for single line comments /* ..... */ used for multi-line comments Notice HTML tags embedded in .php file Example <html> <?php // Single Line Comment # echo “<h1>Hello World</h1>”; ?> </html> helloworld2.php 4
5. Escaping Characters escape1.php The ‘ backslash character can be used to escape special characters Commonly used when you want to use quotation marks within a text string being printed out with the echo statement (“) within quotes ‘’ used for newline ‘’ used for tabs Note – if you are outputting a web page the above can be used to format the souce code – you need to use <p> and <br> tags to format text. <?php echo “<p><form>”; echo “<textarea rows=5 cols=48>”; echo “Demonstration Text<br />”; echo “</textarea>”;echo “</form></p>”; ?> 5
6. Variables A variable is a place in which to store data for manipulation within a script. All php variables begin with $ character. Variables can contain _, letters and digits but cannot start with a number $myFirstvar $_demo $var345 Data is assigned using the = operator <?php $formText = "<form><p><textarea rows=5 cols=48> Demonstration Text goes here</textarea></form>"; echo $formText; ?> variable1.php 6
7. Data Types String – strings of spaces, text, numeric characters, specified within double quotes (“...”) or single quotes (‘...’); Integer – numbers without decimal places, like 1000 Floating-point – numbers with decimal places, like 3.142 Boolean – a truth value which can be either TRUE or FALSE (also true, false) NULL – no value at all 7
8. Data Types <?php $str = "Here is a string"; $int = 77; $flt = 3.142; $non = NULL; echo("String:$str<br>"); echo("Integer:$int<br>"); echo("Floating-point:$flt<br>"); echo("Null:$non<br>"); ?> datatypes.php 8
9. Functions A function is a piece of code that can be executed once or many types by the script. Functions and variables form the heart of PHP programming PHP has lots of functions for manipulating strings, dates, maths, databases etc. but you will want to create your own as well Example function printName() { echo “My name is Jack”; } 9
10. Functions <html> <head> <title>PHP Functions</title> </head> <body> <?php /* A function starts with the keyword function followed by the name of the function following by opening bracket and a closing bracket then an opening brace The content of the function and then the closing brace */ function go(){ echo("PHP adds dynamic content<hr>"); } ?> <?php go(); ?> <p>*** HTML is great for static content ***</p> <?php go(); ?> </body></html> functions1.php
11. Function Arguments The plain brackets that follow function names can be used to provide data for use in the code that can be executed by that function. This data is known as an ‘argument’ and a function can take several ‘arguments’ Example function printName($name) { echo “My name is $name”; } 11
12. Function Arguments <html> <head> <title>PHP Arguments</title> </head> <body> <?php function go($arg) { echo("<b><u><i>$arg</i></u></b>"); } ?> <p>This is the regular text style of this page.</p> <?php go("This text has added style"); ?> <p>This is the regular text style of this page.<p> <?php go("PHP makes this so easy"); ?> </body></html> functions2.php 12
13. Multiple Functions PHP functions can call other functions during the script processing, just like the echo statement. <?php function show_number($num) { $new_number = make_double($num); echo("The value is $new_number"); } function make_double($arg) { return $arg + $arg; } ?> <html> <head> <title>PHP Functions</title> </head> <body> <h3> <?php show_number(4); ?> </h3> </body></html> functions3.php 13
14. Variable scope Scope defines which parts of a PHP script have access to a variable. Variables declared inside a function are known as ‘local’ variables and can only be used in the function within which it is declared. If you want to use a variable between different functions, one way is to declare it with the keyword ‘global’ within the function to access it and change it Good programming practice to declare and initialise it it outside the function first 14
15. Variable scope Scope defines which parts of a PHP script have access to a variable. Variables declared inside a function are known as ‘local’ variables and can only be used in the function within which it is declared. If you want to use a variable between different functions, one way is to declare it with the keyword ‘global’ within the function to access it and change it Good programming practice to declare and initialise it it outside the function first 15
16. Variable scope <?php $num=0; function make_triple($arg) { global $num; $num = $arg + $arg +$arg; thrice(); } function thrice() { global $num; echo("The value is $num"); } ?> <html> <head> <title>Variable Scope</title> </head> <body> <h3> <?php make_triple(4); ?> </h3> </body></html> scope1.php 16
17. Multiple Arguments Functions make specify multiple arguments within their plain brackets to allow several values to be passed to the function code. The argument variable names are separated by commas in a list When you specify multiple arguments to a function, all those arguments must be passed to the funcition However, you can supply default values for the arguments in the declaration so the default value is used if you do not pass it. 17
19. Exercise Write a series of functions to generate a table and a number of rows and cells. The cell function should take an argument which specifies what should be output in the cell. Then call the functions in order so that the table is generated. These functions are useful when writing programs as they help keep the html separate from the coding so you can see what is going on in your code, and easy to change if you have modify css styles etc. You just change them them within the function. Example output_table_header(); output_start_row(); output_cell(“Test”); output_cell(“Testing”); output_cell(“Final Test”); output_end_row(); output_table_footer(); solution_exercise1.php 19