Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITC 240: Web Application Programming SUBHASH PRAJAPATI 04/14/15.

Similar presentations


Presentation on theme: "ITC 240: Web Application Programming SUBHASH PRAJAPATI 04/14/15."— Presentation transcript:

1 ITC 240: Web Application Programming SUBHASH PRAJAPATI 04/14/15

2 Review Variable Types, Typecasting Constant Function Types Variable Scope

3 Today Functions: Passing by value, Passing by reference Conditionals Class Excercises

4 Passing by Value The default parameter will be passing by value. See following example: <?php function incrementTheVariable($var){ $var++; return $var; } $a=5; $b = incrementTheVariable($a); echo $a; echo $b; ?>

5 Passing by reference We can pass a variable by reference to a function so the function can modify the variable (both the variable point to the same content) <?php function incrementTheVariable(&$var){ $var++; } $a=5; incrementTheVariable($a); echo $a; ?>

6 Class Discussion o What are the advantages of using functions? o Should we echo inside a function? Why or why not?

7 Comparison Operations OperatorMeaning $a == $bEqual $a === $bIdentical (both value and type are equal) $a != $bNot Equal $a !== $bType and value are not equal $a < $bLess than $a <= $bLess than or equal to $a > $bGreat than $a >= $bGreater than or equal to

8 Logical Operators OperatorMeaning OR, ||OR AND, &&And XORReturns true if either is true. Returns false if both are true !negation

9 Conditionals Executing different code on given different circumstances Code branching/ Code separation

10 if statement

11 if(TEST CONDITION) { //code that occurs if condition proves true } Eg- $salary = 50000; echo "My salary is $". $salary. " "; if($s > 40000) { echo "I'm Happy!"; } // Now change the value 50000 to 15000 and see what happens

12 If/else statement

13 Eg- $salary = 50000; echo "My salary is $". $salary. " "; if($salary > 40000) { echo "I'm Happy!"; } else { echo “Life is not beautiful”; }

14 Class Exercise 3.1 Write a program to determine the large number in between two numbers. Write a program to determine a number is odd or even.

15 If, elseif statement (chain)

16 elseif statement $salary = 50000; echo "My salary is $". $salary. " "; If ($salary > 40000) { echo "I'm Happy!"; } else if ($salary > 30000) { echo “Life is still ok”; } else { echo “I don’t know what to say”; }

17 Nested statements if ($x < $y) { STATEMENTS_A } else { if ($x > $y) { STATEMENTS_B } else { STATEMENTS_C }

18 Class Exercise 3.2 o Write a program to display the office is open or closed depending on time the user visits the website. Office Hours: Mon-Fri 8:00 AM - 5:00 PM, Sat: 10:00 AM – 2:00 PM Hint: use php date() function to get the current hour/day date (‘G’) gives 24-hour format of an hour without leading zeros. date (‘l’) gives Monday through Saturday

19 Ternary Operator (condition) ? Value_if_true : Value_if_false <?php $opening_time = ( $day == “Sunday”) ? 12 : 9; // above code is equivalent to : if ($day == “Sunday”) $opening_time = 12; else $opening_time = 9; ?>

20 switch select one of many blocks of code to be executed. switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break;... default: code to be executed if n is different from all labels; }

21 Class Exercise 3.3 o Write a program to greet the user with “Good Morning”, “Good Afternoon” or “Good Evening” depending on the time the user visits the website. Write this program utilizing switch statement.

22 Class Discussion o What is different between =, ==, ===?

23 Some PHP String functions o str_word_count ($string) – gives number of words in a string o str_replace ($search $replace, $subject) – replace certain word in a string o strlen ($string) – gives length of the string o strtolower ($string) – converts to lower case o strtoupper ($string) – converts to upper case o trim ($string) – removes white space in both the ends of the string Check PHP Manual for complete list of the functionsPHP Manual

24 Class Exercise 3.4 o Write a program to check whether or not the given word is ‘seattle’ (regardless of the case of the string the user passes and has a space at the end/front). For eg – the function will return true in any of the following ‘seattle’, ‘Seattle’, ‘seattle ’, ‘ sEaTtle ’

25 Code Organization with PHP o include and require include “file_path”; require “file_path”; on failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script include will only produce a warning (E_WARNING) and the script will continue o include_once and require_once similar to include/require with the only difference being that if the code from a file has already been included, it will not be included again.

26 Code Organization with PHP Welcome to my home page! Some text. Some more text.

27 Class Exercise 3.5 Create a webpage template in the format as shown in the picture. Create header, footer and nav templates to include in a webpage.


Download ppt "ITC 240: Web Application Programming SUBHASH PRAJAPATI 04/14/15."

Similar presentations


Ads by Google