Download presentation
Presentation is loading. Please wait.
Published byAshlee Merritt Modified over 9 years ago
1
ITC 240: Web Application Programming SUBHASH PRAJAPATI 04/09/15
2
Review Client Server Architecture Server Side Language/ Client Side Language PHP Fundamentals/Syntax Variable Function
3
Today More on Variables & Functions Scope Functions: parameters & returns, passing by values Conditionals
4
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
5
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.
6
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); ?>
7
Variable Types Boolean $booleanVar = TRUE; // a boolean String $stringVar = "foo"; // a string $stringVar = 'foo'; // a string Integer $integerVar= 12; // an integer Float $floatVar= 12.5; // a floating point number Others: Array, Objects, Resource, Null (we’ll discuss later)
8
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
9
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- ?> Check PHP date manual for complete list of the possible parametersdate manual
10
Class Exercise 2.1 Write the output of following: <?php echo date('l jS \of F Y h:i:s A'). " "; echo date(‘Y-m-d H:i:s’). " "; ?>
11
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); ?>
12
Class Exercise 2.3 strtotime (string) // pass English phrase <?php $date=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date). " "; $date=strtotime("next Monday"); echo date("Y-m-d h:i:sa", $date). " "; $date=strtotime("+6 Months"); echo date("Y-m-d h:i:sa", $date). " "; ?>
13
Class Exercise 2.3 strtotime (string) // pass English phrase <?php $date=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date). " "; $date=strtotime("next Monday"); echo date("Y-m-d h:i:sa", $date). " "; $date=strtotime("+6 Months"); echo date("Y-m-d h:i:sa", $date). " "; ?>
14
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’; ?>
15
Typecasting OperatorChanges to (int)Integer (bool)Boolean (string)String (array)Array (object)Object (unset)NULL
16
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
17
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
18
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’);
19
Class Exercise 2.4 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
20
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 (); ?>
21
User defined Functions: return Function can also return without the value <?php function returnInMiddle () { // do something return; // code written here will not be executed } ?>
22
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; ?>
23
Variable Scope The variables inside user-defined function have local scope. Check output of following <?php $myName = “Tina”; function printName(){ echo $myName; } printName (); ?>
24
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; ?>
25
Passing by reference We can pass a variable by reference to a function so the function can modify the variable <?php function incrementTheVariable(&$var){ $var++; } $a=5; incrementTheVariable($a); // $a is 6 here ?>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.