ITC 240: Web Application Programming SUBHASH PRAJAPATI 04/14/15
Review Variable Types, Typecasting Constant Function Types Variable Scope
Today Functions: Passing by value, Passing by reference Conditionals Class Excercises
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; ?>
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; ?>
Class Discussion o What are the advantages of using functions? o Should we echo inside a function? Why or why not?
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
Logical Operators OperatorMeaning OR, ||OR AND, &&And XORReturns true if either is true. Returns false if both are true !negation
Conditionals Executing different code on given different circumstances Code branching/ Code separation
if statement
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 to and see what happens
If/else statement
Eg- $salary = 50000; echo "My salary is $". $salary. " "; if($salary > 40000) { echo "I'm Happy!"; } else { echo “Life is not beautiful”; }
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.
If, elseif statement (chain)
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”; }
Nested statements if ($x < $y) { STATEMENTS_A } else { if ($x > $y) { STATEMENTS_B } else { STATEMENTS_C }
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
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; ?>
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; }
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.
Class Discussion o What is different between =, ==, ===?
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
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 ’
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.
Code Organization with PHP Welcome to my home page! Some text. Some more text.
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.