Upload
ashlee-merritt
View
221
Download
1
Tags:
Embed Size (px)
Citation preview
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.
ConstantsAn 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
• Float
$floatVar= 12.5; // a floating point number
• 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 () functionA 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.1Write 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.2mktime (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);
?>
Class Exercise 2.3strtotime (string) // pass English phrase
<?php
$date=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $date) . "<br>";
$date=strtotime("next Monday");
echo date("Y-m-d h:i:sa", $date) . "<br>";
$date=strtotime("+6 Months");
echo date("Y-m-d h:i:sa", $date) . "<br>";
?>
Class Exercise 2.3strtotime (string) // pass English phrase
<?php
$date=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $date) . "<br>";
$date=strtotime("next Monday");
echo date("Y-m-d h:i:sa", $date) . "<br>";
$date=strtotime("+6 Months");
echo date("Y-m-d h:i:sa", $date) . "<br>";
?>
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.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
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
<?phpfunction 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;
?>