Presentation is loading. Please wait.

Presentation is loading. Please wait.

LIS651 lecture 3 taming PHP Thomas Krichel 2005-11-12.

Similar presentations

Presentation on theme: "LIS651 lecture 3 taming PHP Thomas Krichel 2005-11-12."— Presentation transcript:

1 LIS651 lecture 3 taming PHP Thomas Krichel

2 functions The PHP function reference is available on its web site It shows the impressive array of functions within PHP. But one of the strengths of PHP is that you can create your own functions as you please. If you recreate one of the built-in functions, your own function will have no effect.

3 example Stephanie Rubino was an English teacher and objects to sentences like You have ordered 1 bottles of Grosswald Pils. Let us define a function rubino_print(). It will take three arguments –a number to check for plural or singular –a word for the singular –a word for the plural

4 function and parameters use the keyword "function" and declare your parameters, as in: function rubino_print ($number, $singular,$plural) { if($number == 1) { print "one $singular"; } else { print "$number $plural"; }

5 default arguments Sometimes you want to allow a function to be called without giving all its arguments. You can do this by declaring a default value. For the previous example function thomas_need($thing='beer') { print "I need $thing\n"; } thomas_need(); // prints: I need beer thomas_need('sex'); // prints: I need sex

6 rubino_print using common plurals function rubino_print ($num, $sing,$plur=1) { if($num == 1) { print "one $sing"; } elseif($plur ==1) { print "$num $sing"."s"; } else { print "$num $plur"; }

7 return value Up until now we have just looked at the effect of a function. return is a special command that return a value. When return is used, the function is left.

8 rubino_print with return function rubino_print ($number, $singular,$plural) { if($number == 1) { return "one $singular"; } return "$number $plural"; } $order=rubino_print(2,"beer","beers"); print "you ordered $order\n"; // prints: you ordered 2 beers.

9 utility function from function mysql_fetch_all($query) { if($err=mysql_error()) { return $err;} if(mysql_num_rows($r)) { while($row=mysql_fetch_array($r)) {$result[]=$row; } return $result;}} if(is_array($rows=mysql_fetch_all($query)) { // do something } else { if (! is_null($rows)) { die("Query failed!");} }

10 visibility of variables variables used inside a function are not visible from the outside. Example $beer="Karlsberg"; function yankeefy ($name='Sam Adams') { $beer=$name; } yankeefy(); print $beer;// prints: Karlsberg the variable inside the function is something different than the variables outside.

11 accessing global variables. There are two ways to change a global variable, i.e. one that is defined in the main script. One is just to call it as $GLOBAL['name'] where name is the name of the global variable. function yankeefy ($name="Sam Adams") { $GLOBAL['beer']="name"; }

12 brewer_quiz.php: introduction

13 brewer_quiz.php: form building function build_form($answer,$comment) { print " Take the Saarland brewery challenge \n"; print $GLOBALS['form_top']; print " "; print $GLOBALS['form_submit']; print $GLOBALS['submit_check']; print $GLOBALS['form_end']; print $comment; }

14 brewer_quiz.php: form processing function process_form($answer,$brewers) { $r[]=$answer; foreach($brewers as $brew) { if($answer == "$brew") { $r[]=' Congradulation! This is correct! '; return $r; } $r[]=' This is a bad answer, try again! '; return $r; }

15 brewer_quiz.php main part if($_GET['submitted']) { $from_form=process_form($_GET['guess'],$brewers); } build_form($from_form[0],$from_form[1]) ; ?>

16 working with many source files Many times it is useful to split a PHP script into several files. PHP has two mechanisms. require(file) requires the to be included. If the file is not there, PHP exits with an error. include(file) includes the file.

17 require() and include() Both assume that you leave PHP. Thus within your included file you can write simple HTML. If you want to include PHP in your included file, you have to surround it by, just like in a PHP script. Here is an example to use include to build the basic web page.

18 top.html $title

19 bottom.html Valid XHTML 1.0!

20 validated.php

21 trouble $title in the top.html is not understood as the title. It reads as $title, which means "idiot" for your web user. Even if you replace $title with $title is empty. The definition from the outer file is not seen in the included file. So you have to split into three files, and print the title in the main file. I leave that to you to figure out.

22 login.php & create_account.php Both require a database that has three fields –id which is an auto_increment int acting as a handle –username is the username of the account. it must be unique and this is enforced by mySQL –password is a varchar(41) because the sha1 of the password is stored. This is 40 chars long.

23 login.php function show_form($message) { print " $message Login Username Password Not yet a member? Create an account ! "; }

24 function process_form() { $username=trim($_POST['username']); $pass=trim($_POST['pass']); $sha_pass=sha1($pass); $db=mysql_connect('localhost','krichel','laempel'); $query="SELECT * FROM beer_shop.users WHERE username='$username' AND password = '$sha_pass'"; $result=mysql_query($query); $error=mysql_error(); if($error) { return "Sorry: $query gives an error $error"; } $affected=mysql_affected_rows(); if(! ($affected)) {return "Invalid username or password";} }

25 login.php (end) if($_POST['submitted']) { $error=process_form(); if($error) { show_form($error); } else { $user=$_POST['username']; print " Welcome to $user "; } else { show_form(''); }

26 create_account.php function show_form($message) { print " $message Create Account Please complete the form below to create your account.

It must be more than 5 characters and cannot be your username. ";

Download ppt "LIS651 lecture 3 taming PHP Thomas Krichel 2005-11-12."

Similar presentations

Ads by Google