Presentation is loading. Please wait.

Presentation is loading. Please wait.

LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

There are copies: 1
LIS651 lecture 3 numbers, Boolean, control flow Thomas Krichel

Similar presentations


Presentation on theme: "LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19."— Presentation transcript:

1 LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19

2 today Introduction to the course Introduction to PHP Using form data in PHP

3 course resources Course home page is at http://wotan.liu.edu/home/krichel/courses/lis651p08a The course resource page http://wotan.liu.edu/home/krichel/courses/lis651 The class mailing list https://lists.liu.edu/mailman/listinfo/cwp-lis651-krichel Me. –Send me email. Unless you request privacy, I answer to the class mailing list. –Skype me at thomaskrichel. Get skype from skype.com.

4 today We introduce PHP. Understanding PHP is the most difficult aspect of the course. We look at how PHP can be used to show the data that we get from the form. You should think about what data to get and how to show it. Everybody will build an example form and then a PHP script to show it. Finally we build a new PHP script that contains the form. So instead of two files, we only have one.

5 PHP introduction PHP is the PHP Hypertext Processor. It is a tool that allows for server-side scripting. Its predecessor is PHP/FI, Personal Home Page / Forms Interpreter. PHP/FI was released by Rasmus Lerdorf in 1995. It was written in Perl. PHP/FI version 2 was released in 1997. It was written in C. PHP version 5 is the current version.

6 Apache and PHP When a file ends in.php, is not simply sent down the http connection like other files. Instead, apache sends the file to the PHP processor. It sends to the client whatever the PHP processor returns. The PHP processor is a module that lives inside Apache.

7 PHP language PHP is an interpreted language. –You write a series of statements. –Apache hands these statements to the PHP interpreter. –The interpreter executes these statements one by one. –When it find an error, it stops running and signals the error. Compiled languages are different. They read the whole program before starting to execute it.

8 try it out Remember we duplicate validated.html when creating a new new file. Right-click on validated.html, choose duplicate. You may be asked to supply your password again. You erase the contents of the dialog box that suggests a new file name and put your new file name in there. If it contains PHP code, it has to end in.php.

9 first PHP script Create a file with the name info.php, and the following contents <?php phpinfo(); ?> nothing else. This will create a test page that tells you everything PHP knows about. Look at some of the variables.

10 comment on info.php In terms of XML, the " " part is called a processing instruction. It is a type of node that we did not encounter in LIS650. We can call any part of the file between " " a PHP part of the file. The XML file here contains just the processing instruction.

11 output of phpinfo() phpinfo() create a whole web page for you, that validates against a loose HTML specification. That page contains a lot of technical detail. The section we may be interested in is PHP Variables. It contains variables that we may be interested in. These are variables that PHP can understand –from its environment –from the client

12 the magic of PHP The client never sees the PHP code. It only sees what the PHP processor has done with the code. You can write normal HTML code, and you can switch to writing PHP code pretty much at any stage. You can have several PHP parts. PHP parts can not be nested. The contents of the PHP part can be called a PHP script.

13 statements Like a normal text is split into sentences, a PHP script is split into statements. A PHP script contains one or more statements. Each statements tells the interpreter something. Each statement is ended by a semicolon. In our first script there is only one statement. Each statement is ended with a semicolon! Think of a statement like a rule in CSS. But never forget the semicolon!

14 expressions The stuff before the semicolon is called an expression. You can think of an expression as anything anyone may want to write in a computer program. So an expression is just a way to talk about stuff in a program in a more edifying way than just calling it stuff.

15 functions phpinfo() is a function. Functions are one of the most fundamental concepts in computer programming. A function is an expression that does something to something else. The something else is in the parenthesis. It is called the argument of the function. The argument of phpinfo() is empty.

16 second php script: hello.php Normally we write HTML code and then we add PHP parts. Take validated.html, copy to hello.php make the body <?php print("Hello, world!"); ?> Validate the resulting XHTML.

17 comment on hello.php print() is also a function. print() prints its argument. Here the argument is a string. A string is a sequence of characters enclosed by single or double quotes. For print, the () can be omitted. You could have written three statements <?php print " "; print "Hello, world!"; print " "; ?>

18 good style Write each statement on a new line. Add plenty of comments. There are three styles of comments in a PHP program –// the rest of the line is a comment –# the rest of a line is a comment –/* this is a comment */ Only last style can be used over several lines. Do you recognize two of the commenting styles?

19 another way to write hello.php <?php $greeting="Hello, world!"; print " $greeting "; ?> Here $greeting is a variable. The first statement assigns it the string value "Hello, world!". The second statement prints it out. This example is important because it illustrates the concept of a variable. The name of the variable is greeting.

20 variable names Variable name must start with a letter or underscore. They can contain letters, digits and underscores. The following are examples of illegal names –$2drunk –$bottle-content –$brewer@grosswald Variable names are case sensitive. I use lowercase only and add underscores in long names. The variable name "$this" is reserved. It is good to give variables meaningful names.

21 strings a piece of text in PHP is called a string. A string is often surrounded by single quotes. print 'I want beer'; $want='beer'; print 'I want $want'; // prints: I want $want If you want to use the values of variables, use double quotes $want='beer'; print "I want $want"; // prints: I want beer

22 single and double quotes You can use single quotes to quote double quotes print 'She wrote: "I want beer." and sighed.'; // prints: She wrote: "I want beer." and sighed. and vice versa print "She wrote: 'I want beer.' and sighed"; // prints: She wrote: 'I want beer.' and sighed. Sometimes it is not obvious when to put single quotes, double quotes, and when to leave them out. If one thing does not work, try something else.

23 the backslash escape The backslash is used to quote characters that otherwise are special. print 'Don\'t give me bad beer!'; $kind='bock'; $beer='Festbock'; print " $beer "; // prints: Festbock The backslash itself is quoted as \\ print "a \\ against beer consumption"; // prints: a \ against beer consumption

24 more backslash escapes \n makes the newline character \r make the carriage return (no use in Unix) \t makes the tab (seldomly used in HTML) \$ makes the dollar (used in the shop) –$amount='1.50'; –print "you owe \$$amount per bottle."; –// prints: you owe $1.50 per bottle. If the backslash was not there $ would be considered to be a variable.

25 concatenation This is done with the. operator. It puts two strings together, the second one after the first one. $cost='5.23'; $message='This costs '. $cost; print $message; // prints: This costs 5.23

26 numbers Numbers are set without the use of quotes. You can +, -, * and / for the the basic calculations. There also is the modulus operator %. It gives the remainder of the division of the first number by the second print 10 % 7; // prints 3 Use parenthesis for complicated calculations $pack=2 * (10 % 7); print "a $pack pack";// prints: a 6 pack

27 geeky increment/decrement ++ is an operator that adds one. The value of the resulting expression depends on the position of the operator $a=4; print ++$a;// prints: 5 print $a;// prints: 5 $b=4; print $b++;// prints 4 print $b; // prints 5 -- works in the same way

28 type conversion In some circumstance, PHP converts numbers to strings and back. It works like magic. It converts numbers to strings when required $one_in_three=1/3; print $one_in_three; // prints: 0.333333333333 and numbers to $string="1.500"; $number=3*$string; print $number; Sometimes it converts to Boolean!

29 Boolean value Every expression in PHP has a Boolean value. It is either 'true' or 'false'. In certain situation, an expression is evaluated as a Boolean For example if(expression) expression1 or expression2

30 what is truth? All strings are true except –the empty string –the string "0" All numbers are true except –0 –0.0 example $a=5-4-1; // $a is false Note that variables that you have not assigned contents are false. This includes misspelled variables!!

31 isset() isset() is a function that returns true if a variable is set. Under strict coding rules, that are enforced by PHP running on wotan, the PHP processor will issue a notice when you use a variable that has not been set to a value. isset($variable) can be used to find out if the variable variable was set before using it.

32 comparison operators Expressions that are evaluated in Boolean often use comparison operators. $beer == 'grosswald' ; // checks for equality Note difference from $beer='grosswald' ; // this is always true Other comparisons are < smaller than<= smaller or equal than > larger than >= larger or equal than

33 logical operators and is logical AND. or is logical OR. if($brand=='Budweiser' or $brand="Sam Adams") { print "Commiserations for buying a lousy beer\n"; } # where is the mistake in this piece of code? ! is Boolean NOT These can be combined. Use parenthesis if((($pints) > 2 and ($vehicle=='car')) or (($pints > 6) and ($vehicle=='bicycle'))) { print "order a cab!\n"; }

34 if( condition ) { } if( condition ) evaluates an expression condition as Boolean, and executes a block of code surrounded by curly brackets if the expression is true. if($drunk) { print "Don't drive!\n"; } Note you don't need to indent the block as done above, but the way Thomas has done it there is pretty much standard, so do it in the same way.

35 if( condition ) {} else {} if you have an if() you can add an else block of code to execute when the condition is false if($sober) { print "You can drive\n"; } else { print "Check if you are fit to drive\n"; }

36 elseif( condition ) { } You can build chain of conditions if($pints_drunk==0) { print "You are ok to drive\n"; } elseif($pints_drunk<3) { print "Don't use the car, get on your bike\n"; } elseif($pints_drunk<=6) { print "Take a cab home\n"; } else { print "Call the local hospital!\n"; }

37 while( condition ) { } while( condition ) { } executes a piece of code while the condition condition is true $count=0; while($count < 100) { print "Пиво без водки -- деньги на ветер! "; $count=$count+1; # don't forget to increment $count! }

38 getting back to forms Forms deliver data to the server. The server can then process the data and deliver a response. If the server process uses PHP, each control is visible to PHP as a PHP variable. It can be read into the script.

39 control name and PHP variable When the form is passed to the PHP script named with the action= of the the the controls are accessible as PHP variables. If name is the name of the control, and if the method is POST, the control is read as the variable $_POST['name']. If name is the name of the control, and if the method is GET, the control is read as the variable $_GET['name'].

40 example HTML file greet.html has your last name: PHP file greet.php has <?php print "Hello "; print $_GET['lastname']; ?> in addition to the usual HTML stuff.

41 iterative form input When users start to use your site, the shit hits the fan. Users have many ways to do things wrong. Many times you will have to print the form again, with values already filled in. In such circumstance a static HTML file for the form is unsuitable. Therefore we need a PHP file that writes out the form and processes the form.

42 We include a hidden element in the form to see if it was submitted We start the script we check for submission if($_GET['submitted']) { // work on the data that was submitted } else { // print form } check for submission

43 master example greet.php <?php if($_GET['submitted']) { $name=$_GET['name']; print " Hello $name! \n"; } else { print " \n"; print " <input type=\"hidden\" "; print "name=\"submitted\" value=\"1\" /> \n"; print " Your name <input type=\"text\" "; print "name=\"name\" value=\"$name\" />"; print " \n"; } ?>

44 PHP calling itself One cool thing to help with that is $_SERVER[PHP_SELF] It gives the file name of your script in the form. As you change your script file name, you do not need to change the name of the form submitted. So in the previous slide, if you replace action=\"greet.php\" with action=\"".$_SERVER['PHP_SELF']."\" you can change the name of the PHP file. It will still find the itself as the PHP file.

45 http://openlib.org/home/krichel Thank you for your attention! Please switch off computers when you are done!


Download ppt "LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19."

Similar presentations


Ads by Google