Output: Equal Unequal"> Output: Equal Unequal">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05.

Similar presentations


Presentation on theme: "COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05."— Presentation transcript:

1 COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05

2 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP PHP's Operators Very similar to those used in C and Java with a few exceptions. The logical operators &&, || and ! can be written as and, or and not. xor is also available as a logical operator PHP has the normal == and != equality operators Also has the operators === and !== which compare both value and type.

3 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP PHP's Operators Example: <?php $x = 1; $y = "1"; if ($x == $y)print "Equal\n"; elseprint "Unequal\n"; if ($x === $y)print "Equal\n"; elseprint "Unequal\n"; ?> Output: Equal Unequal

4 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Switch structure: Format: switch ( expression ) { case expression-1 : statements break; … case expression-n : statements break; default: statements break; }

5 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Switch structure: Example: switch ( $name) { case “John”: echo “Hello Sir Nickleby!” break; case “Mary”: echo “Hello Maid Mariam!” break; case “Tim”: echo “Hello Master Timothy!” break; default:echo “Who the heck are you?” break; }

6 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Functions PHP has functions that are very similar to C's functions and Java's methods. Syntax of a function declaration is slightly different. Format: function ( ) { code }

7 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Functions Function calls are exactly as you would expect. Parameters are normally passed by value. Prepending an ampersand to the argument name in the declaration forces passing by reference. Also possible to give default values to parameters that are not mentioned in the actual call. Default parameters are: very widely used by PHP library functions significantly simplify the use of many such library functions. A functional value can be returned via a return statement.

8 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Functions PHP also has the notion of variable function. Variable functions are really PHP variables that hold the name of a function When the variable is invoked in a function like fashion, what actually happens is that named function is actually invoked. This is similar to C's pointers to functions. In the right circumstances this is a powerful and useful feature.

9 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Functions Function definitions can occur before or after call. Function definitions can also be nested (unlike C and Java). Nested defnitions have restricted "scope". The "main-line" code of a PHP programme can be thought of as being a separate function PHP does not have a notion of global variables.

10 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Functions Example: <?php function sum($x, $y, $z) { return $x+$y+$z; } print sum(23,31,44). "\n"; ?>

11 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Functions - BEWARE Any valid PHP code may appear inside a function, even other functions and class definitions. In earlier PHP, functions had to be defined before they are referenced. No such requirement exists since PHP 4. Exception: When a function is conditionally defined. When a function is defined in a conditional manner, its definition must be processed prior to being called.

12 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Conditional functions Function’s definition depends on selection <?php $makefoo = true; /* We can't call foo() from here since it doesn't exist yet, but we can call bar() */ bar(); if ($makefoo) { function foo() { echo "I don't exist until execution reaches me.\n"; } } /* Now we can call foo() since $makefoo evaluated to true */ if ($makefoo) foo(); function bar() { echo "I exist immediately upon program start.\n"; } ?>

13 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Passing arrays to functions <?php …. function takes_array( $input ) { echo "$input[0] + $input[1] = ", $input[0]+$input[1]; } ?>

14 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Passing function parameters by reference <?php function add_some_extra ( &$string ) {$string.= 'and something extra.'; } $str = 'This is a string, '; add_some_extra ( $str ); echo $str; ?> Output: 'This is a string, and something extra.'

15 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Default argument values <?php function makecoffee ( $type = "cappuccino” ) { return "Making a cup of $type.\n"; } echo makecoffee(); echo makecoffee("espresso"); ?> Output: Making a cup of cappuccino. Making a cup of espresso.

16 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP NOTE: The default value must be a constant expression, not (for example) a variable, a class member or a function call. Note that when using default arguments, any defaults should be on the right side of any non- default arguments; otherwise, things will not work as expected. As of PHP 5, default values may be passed by reference.

17 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Returning values Values are returned by using the optional return statement. Any type may be returned, including lists and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. You CANNOT return multiple values from a function, but similar results can be obtained by returning a list.

18 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Returning values Example: Simple elements <?php function square($num) { return $num * $num; } echo square(4); // outputs '16'. ?>

19 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Returning values Example: An array to get multiple values <?php function small_numbers() { return array (0, 1, 2); } list ($zero, $one, $two) = small_numbers(); ?>

20 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Returning values Example: A reference from a function <?php function &returns_reference() { return $someref; } $newref =& returns_reference(); ?>

21 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Functions within function <?php function foo() { function bar() { echo "I don't exist until foo() is called.\n"; } function stroke() { echo “No exist either until foo() is called.\n"; } } /* We can't call bar() NOR stroke() yet since they don't exist. */ foo(); /* Now we can call bar() or stroke() - Accessible through foo()'s processesing. */ bar(); ?>

22 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP PHP does not support function overloading Not possible to undefine or redefine previously- declared functions. Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration. PHP 3 does not support variable numbers of arguments to functions, although default arguments are supported (see Default argument values for more information). Both are supported, as of PHP 4:

23 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Standard Input PHP programming is commonly used for processing files and taking advantage of the interface to system utilities. Possible to write simple interactive programmes although coding is slightly clumsy. Basic approach is to read from a "pre-opened" file using the file handle STDIN. Similar to C's approach but PHP lacks the special functions that work with stdin.

24 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Functions The fscanf() function uses formatting conventions similar to those used in C. Example: A simple PHP programme that prompts for two numbers and prints their sum.

25 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Functions <?php echo "Enter first number "; fscanf ( STDIN,"%d", $x); echo "Enter second number "; fscanf ( STDIN,"%d", $y); echo "Sum is ". ($x+$y); //Note concatenating values ?>

26 COMP6015 - An Introduction to Computer Programming : University of the West Indies Introduction to PHP Exercise: On a website, 30 flavours of tropical wines and the corresponding base prices are advertised. You are required to write a functionally decomposed PHP script which allows a user to select a wine flavour and number of bottles, then displays the total cost of the purchase. Added to the advertised base price of the wine must be a 15% VAT on all wines and 10% luxury tax if the wine exceeds $20 per bottle.


Download ppt "COMP6015 - An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05."

Similar presentations


Ads by Google