Presentation is loading. Please wait.

Presentation is loading. Please wait.

Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions.

Similar presentations


Presentation on theme: "Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions."— Presentation transcript:

1 Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions

2 Open Source Server Side Scripting 2 ECA 236 if statement if ( condition ){ statements; } if ( $dogName == “Halle” ){ echo “You are a Golden Retriever”; }

3 Open Source Server Side Scripting 3 ECA 236 if / else statements if( condition ){ statement1; } else{ statement2; } if( $dogName = = “Halle” ){ echo “You are a Golden Retriever”; } else{ echo “You are not my dog”; }

4 Open Source Server Side Scripting 4 ECA 236 if / elseif / else statements if( condition1 ){ statement1; } elseif ( condition2 ){ statement2; } else{ statement3; } if( $dogName = = “Marley” ){ echo “You are a Border Collie”; } elseif( $dogName = = “Halle” ){ echo “You are a Golden Retriever”; } elseif( $dogName = = “Boo” ){ echo “You are a Monster”; } else{ echo “You are not my dog”; }

5 Open Source Server Side Scripting 5 ECA 236 Nested if statements if( condition1 ){ if( conditionA ){ statementA; } if( conditionB ){ statementB; } } // end outer if else{ statement; }

6 Open Source Server Side Scripting 6 ECA 236 Comparison Operators SymbolMeaningExample = equal to $x = = $y ! = not equal to $x ! = $y < less than $x < $y > greater than $x > $y < = less than or equal to $x < = $y > = greater than or equal to $x > = $y

7 Open Source Server Side Scripting 7 ECA 236 Logical Operators SymbolMeaningExample &&and( $x = = $y ) && ( $z > 13 ) | or( $x > $y ) | | ( $z < = 47 ) !not! ( $x < $y )

8 Open Source Server Side Scripting 8 ECA 236 Operator Precedence OperatorAssociativity ( )N/A [ ]right ! ++ – –right * / %left + –.left < <= N/A == ! =N/A &&left | left ? :left = += – = *= /= %=.=left

9 Open Source Server Side Scripting 9 ECA 236 switch statements switch ( condition) { case “case 1”: statements; break; case “case 2”: statements; break; case “case 3”: statements; break; default: statements; break; } // end switch

10 Open Source Server Side Scripting 10 ECA 236 switch example switch ( $dogName ) { case “Marley”: echo “ You are a Border Collie. ”; break; case “Halle”: echo “ You are a Golden Retriever. ”; break; case “Boo”: echo “ You are a Monster. ”; break; default: echo “ You aren’t one of my dogs. ”; break; } // end switch

11 Open Source Server Side Scripting 11 ECA 236 Embedded PHP <?php if( $dogName == “Marley” ) { ?> You are a Border Collie. <?php } elseif( $dogName == “Halle” ) { ?> You are Golden Retriever. <?php } else { ?> You aren’t one of my dogs. <?php } ?> <?php switch ( $dogName ) { case “Marley”: ?> You are a Border Collie. <?php break; case “Halle”: ?> You are a Golden Retriever. <?php break; default: ?> You aren’t one of my dogs. <?php break; } ?>

12 Open Source Server Side Scripting 12 ECA 236 while loop while( condition ){ statements; } $counter = 1; while( $counter <= 13 ){ echo “$counter ”; $counter ++; } // end while

13 Open Source Server Side Scripting 13 ECA 236 for loop for( expression1; condition; expression2 ){ statements; } for( $counter = 1; $counter <= 21; $counter++ ){ echo $counter. ‘, ‘ ; if( $counter % 5 = = 0 ){ echo ‘ ’; } // end if } // end for

14 Open Source Server Side Scripting 14 ECA 236 do…while loop do statements; while( condition ) $counter = 1; do{ echo “$counter ”; } while( $counter > 13 )

15 Open Source Server Side Scripting 15 ECA 236 Control Structures  Two other loops are each( ) and foreach( ) which are used with arrays  To stop executing a loop use the break; statement  To stop the execution of an entire PHP script, use the exit; statement

16 Open Source Server Side Scripting 16 ECA 236 Programmer-defined Functions  Why create functions  reusable code  easier to test and debug  Functions:  define using function keyword  naming follows same rules as variables  names are case-insensitive  can be made to “reply” to the code which called it by returning a value

17 Open Source Server Side Scripting 17 ECA 236 Programmer-defined Functions cont…  syntax to create a function:  syntax to call a function: function function_name( ) { statements; } function_name( )

18 Open Source Server Side Scripting 18 ECA 236 Programmer-defined Functions cont…  arguments or parameters  functions can be written to take more than one argument  failure to send correct number of arguments results in an error  use return statement to return a value $x = 3; $y = 4; $z = multiplyThem( $x, $y ); function multiplyThem( $n1, $n2 ) { $product = $n1 * $n2; return $product; }

19 Open Source Server Side Scripting 19 ECA 236 Programmer-defined Functions cont…  default arguments  setting a default value for an argument makes it optional  passed values overwrite the default  place default arguments last in function definition $x = 3; $z = multiplyThem( $x ); function multiplyThem( $n1, $n2 = 10 ) { $product = $n1 * $n2; return $product; }

20 Open Source Server Side Scripting 20 ECA 236 Programmer-defined Functions cont…  variable scope  the scope of a variable used in a function is local to the function by default  to create a global variable use global keyword function multiplyThem( $n1, $n2 = 10 ) { global $product; $product = $n1 * $n2; return $product; }


Download ppt "Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions."

Similar presentations


Ads by Google