Transcript Document
ITC 240: Web Application
Programming
S UBHA S H P R A JA PATI
0 7 / 01/15
Review
• Client Server Architecture
• Server Side Language/ Client Side Language
• PHP Fundamentals/Syntax
• Variable
• Function
Today
• More on Variables & Functions
• Scope
• Functions: parameters & returns, passing by values
• Conditionals
Variables
• Container that can hold certain value (storing data into memory location)
• Starts with $ sign
• A noun
• Starts with underscore or letter
• Can be named with a-z, A-Z, _, 0-9 characters
• cAsE sEnSiTivE - $myVar and $Myvar are different
Functions
• A block of code to perform a task
• Starts with “function” keyword
• A verb
• Can be named with a-z, A-Z, _, 0-9 characters
• NOT case sensitive – myfunction() & myFunction() are same, but good practice is just use one.
Constants
An identifier with an associated value which cannot be altered by the program during normal
execution. Written in all UPPER_CASE
<?php
define ("SALES_TAX", .096);
$itemPrice = 50;
echo $totalPrice = $itemPrice + ($itemPrice * SALES_TAX);
?>
Variable Types
• Boolean
$booleanVar = TRUE; // a boolean
• String
$stringVar = "foo"; // a string
$stringVar = 'foo'; // a string
• Integer
$integerVar= 12;
// an integer
$floatVar= 12.5;
// a floating point number
• Float
• Others: Array, Objects, Resource, Null (we’ll discuss later)
Function Types
• User-defined Functions
The functions we define
• Build in Functions
The functions that come with PHP
for eg: date();
date ( string $format [, int $timestamp = time() ] )
// returns string
PHP date () function
A program to display today’s date
<?php
echo date (‘Y’); // displays the current year
echo date (‘m’); // displays the current month
echo date (‘d’); // displays today’s date
echo (‘H:i:s’); // Hour, minute, seconds
// automatic copyright
© 2013-<?php echo date("Y")?>
?>
Check PHP date manual for complete list of the possible parameters
Class Exercise 2.1
Write the output of following:
<?php
echo date('l jS \of F Y h:i:s A'). "<br>";
echo date(‘Y-m-d H:i:s’). "<br>";
?>
Class Exercise 2.2
mktime (hour,minute,second,month,day,year) // returns unix timestamp
<?php
$date =mktime(10, 21, 45, 4, 11, 2012);
echo "Created date is " . date("Y-m-d h:i:sa", $date);
?>
Typecasting
gettype ()/ settype() – built in functions
<?php
$foo = '1';
echo gettype($foo); // outputs 'string'
settype($foo, 'integer');
echo gettype($foo); // outputs 'integer‘
// another way
$a = (int) ‘55’;
?>
Typecasting
Operator
Changes to
(int)
Integer
(bool)
Boolean
(string)
String
(array)
Array
(object)
Object
(unset)
NULL
User defined functions: Parameters
Parameters: arguments passed to a function
<?php
function calculateSomething ($firstNumber, $secondNumber){
// do some calculations here
}
?>
In above example, $firstNumber and $secondNumber are two parameters
User defined functions: Return Value
Return Value: the value returned by the function
<?php
function addNumber ($firstNumber, $secondNumber){
$sum = $firstNumber + $secondNumber;
return $sum;
}
?>
In above example $sum is the return value
User defined functions: Function Call
Function Call: Just defining the function does NOT do anything, until you call it.
function doSomeMagic ($magicName) {
// magic code here
}
// function call
doSomeMagic (‘awesome magic’);
Class Exercise 2.3
Write a program (a function) to converts Fahrenheit to Celsius temperature.
(for example 45.5 F = 7.5 C)
Formula:
C = ((F-32) * (5/9))
Celsius Scale: 0-100
Fahrenheit Scale: 32-212
Default parameter value
<?php
function orderDrink ($drink = “coffee") { // coffee is the default value
return "Making a cup of $drink.\n";
}
echo orderDrink (“Ice Tea");
echo orderDrink ();
?>
User defined Functions: return
Function can also return without the value
<?php
function returnInMiddle () {
// do something
return;
// code written here will not be executed
}
?>
Variable Scope
The variables inside user-defined function have local scope.
Check output of following:
<?php
function addNumbers($a, $b){
$sum = $a + $b;
return $sum;
}
echo $sum;
$sum = addNumbers (5,6);
echo $sum;
?>
Variable Scope
The variables inside user-defined function have local scope.
Check output of following
<?php
$myName = “Tina”;
function printName(){
echo $myName;
}
printName ();
?>
Global Variable
The variables inside user-defined function have local scope.
Check output of following
<?php
$a = 1;
$b = 2;
function addNumbers () {
global $a, $b;
$sum = $a+$b;
return $sum;
}
$sum = addNumbers ();
echo $sum;
?>
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
Operator
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 Operators
Operator
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 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
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 2.4
• Write a program to determine the large number in between two numbers.
• Write a program to determine a number is odd or even.