john rowley notes

19
John Rowley PHP and MySQL Course John Rowley 1

Upload: ibat-college

Post on 07-Nov-2014

500 views

Category:

Education


2 download

DESCRIPTION

Lecturer: John RowleyPHP MySQL notes

TRANSCRIPT

Page 1: John Rowley Notes

John Rowley

PHP and MySQL Course

John Rowley

1

Page 2: John Rowley Notes

John Rowley

Part 2: Getting Started with PHP

Hello WorldSyntax RulesEscaping CharactersReserved WordsVariablesData TypesFunctionsFunction ArgumentsMultiple FunctionsVariable ScopeMultiple Arguments

2

Page 3: John Rowley Notes

John Rowley

Exercise 2.1 – Hello World

Version 1<?php

echo “Hello World”;

?>

Version 2<html>

<?phpecho “<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

Page 4: John Rowley Notes

John Rowley

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

Page 5: John Rowley Notes

John Rowley

Escaping Characters

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

‘\n’ used for newline ‘\t’ 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 “\t<textarea rows=\”5\” cols=\”48\”>”; echo “Demonstration Text<br />”; echo “</textarea>”;

echo “</form></p>”; ?>

escape1.php

5

Page 6: John Rowley Notes

John Rowley

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>\n<p>\t<textarea rows=\”5\”

cols=\”48\”>\n Demonstration Text goes here</textarea>\n</form>";

echo $formText;?>

variable1.php

6

Page 7: John Rowley Notes

John Rowley

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

Page 8: John Rowley Notes

John Rowley

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

Page 9: John Rowley Notes

John Rowley

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

Page 10: John Rowley Notes

John Rowley

Functions

<html> <head> <title>PHP Functions</title> </head> <body> <?php

/*A function starts with the keyword functionfollowed by the name of the functionfollowing 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

Page 11: John Rowley Notes

John Rowley

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’

Examplefunction printName($name) {

echo “My name is $name”;

}

11

Page 12: John Rowley Notes

John Rowley

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>

12

functions2.php

Page 13: John Rowley Notes

John Rowley

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>

13

functions3.php

Page 14: John Rowley Notes

John Rowley

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

Page 15: John Rowley Notes

John Rowley

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

Page 16: John Rowley Notes

John Rowley

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

Page 17: John Rowley Notes

John Rowley

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

Page 18: John Rowley Notes

John Rowley

Multiple Arguments

<?php

function addup( $a = 32, $b = 32, $c = 32){ $total = $a + $b + $c; echo("$a + $b + $c = $total");

}?> <html> <head> <title>Function Arguments</title> </head> <body>

<h3> <?php addup(8, 16, 24); ?> </h3>

<h3> <?php addup(8, 16); ?> </h3>

</body></html>

18

functions4.php

Page 19: John Rowley Notes

John Rowley

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();

19

solution_exercise1.php