itc 240: web application programming subhash prajapati 04/14/15
TRANSCRIPT
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 OperationsOperator Meaning
$a == $b Equal
$a === $b Identical (both value and type are equal)
$a != $b Not Equal
$a !== $b Type and value are not equal
$a < $b Less than
$a <= $b Less than or equal to
$a > $b Great than
$a >= $b Greater than or equal to
Logical OperatorsOperator Meaning
OR, || OR
AND, && And
XOR Returns 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 . "<br />";
if($s > 40000) {
echo "I'm Happy!";
}
// Now change the value 50000 to 15000 and see what happens
If/else statement
Eg-
$salary = 50000;
echo "My salary is $" . $salary . "<br />";
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.
elseif statement $salary = 50000;
echo "My salary is $" . $salary . "<br />";
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;
?>
switchselect 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.
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 functions
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<html>
<body>
<div class="menu">
<?php include 'menu.php';?>
</div>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
</body>
</html>