1 / 21

LIS651 lecture 3 numbers, Boolean, control flow

Thomas Krichel 2010-02-04. LIS651 lecture 3 numbers, Boolean, control flow. numbers. Numbers are set without the use of quotes. You can +, -, * and / for the the basic calculations.

Download Presentation

LIS651 lecture 3 numbers, Boolean, control flow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thomas Krichel 2010-02-04 LIS651 lecture 3numbers, Boolean, control flow

  2. 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

  3. 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

  4. 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!

  5. 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

  6. 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!!

  7. 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.

  8. 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

  9. 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"; }

  10. 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.

  11. 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"; }

  12. 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"; }

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

  14. 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.

  15. control name and PHP variable • When the form is passed to the PHP script named with the action= of the the <form> 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'].

  16. example • HTML file greet.html has <form action="greet.php" method="get"><p> your last name: <input type="text" name="lastname"/></p></form> • PHP file greet.php has <?php print "Hello "; print $_GET['lastname']; ?> in addition to the usual HTML stuff.

  17. 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.

  18. check for submission • We include a hidden element in the form to see if it was submitted <input type="hidden" name="submitted" value="1"/> • We start the script we check for submission if(isset($_GET['submitted'])) { // work on the data that was submitted } else { // print form }

  19. minimal example greet.php <?php if(count($_GET)) { print "<div>Hello ".$_GET['name']."!</div>\n"; } else { print '<form action="'.$_SERVER['PHP_SELF']. '" method="get">'; print '<div>Your name <input type="text" name="name"/>'; print "</div></form>\n"; } ?>

  20. 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.

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

More Related