Php-Expressions Control Functions
Php-Expressions Control Functions
Expressions
• Using variables within expressions to do
something is what PHP is all about.
<?php
$name = ‘Rob’; Expression
echo $name;
?>
Operator
Some Types of Operator
• Arithmetic • Incrementing
/decrementing
• Assignment
• Logical
• Bitwise
• String
• Comparison
• Ternary
String Operators
• Use a dot to concatenate two strings:
e.g.
$firstname = ‘Rob’;
$surname = ‘Tuley’;
// displays ‘Rob Tuley’
echo $firstname.’ ‘.$surname;
Arithmetic Operators
Example Name Result
$a + $b Addition Sum of $a and $b.
$a - $b Subtraction Difference of $a and $b.
$a * $b Multiplication Product of $a and $b.
$a / $b Division Quotient of $a and $b.
$a % $b Modulus Remainder of $a divided by $b.
Assignment Operators
Example Result
$a = $b Sets $b to the same value as $a.
$a += $b Equivalent to $a = $a + $b.
$a .= $b Equivalent to $a = $a.$b.
Combining Operators
• Note that you can combine operators, for
example use =, + and / in one expression:
$a = 4;
$b = 2;
$c = $a + $b + ($a/$b);
// $c has value 4+2+(4/2) = 8
• Brackets help group operators.
Comparison Operators
Example Name Result
$a == $b Equal TRUE if $a is equal to $b.
$a != $b Not equal TRUE if $a is not equal to $b.
$a <> $b Not equal TRUE if $a is not equal to $b.
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b Gtr than or equal to TRUE if $a is greater than or equal to $b.
Comparisons
• Comparison expressions return a value of
TRUE (or ‘1’) or FALSE (or ‘0’).
e.g.
$a = 10;
$b = 13;
// result is true (‘1’)
echo $a < $b;
Incrementing/Decrementing
echo $letters;
print_r($letters);
So..
We know we can:
1. Store things in named variables.
2. Use expressions to operate on the contents
of these variables.
3. Can compare variables..
while (comparison) {
expressions;
}
Example
• Lets count to 10! Displays 1,2,3,4,5,..,10:
$i = 1;
while ($i <= 10) {
echo $i++;
}
Do .. While
• An alternative...
$i = 1;
do {
echo $i++;
} while ($i <= 10);
For loop
• Sometimes we want to loop around the same bit of
code a number of times.. Use a for loop.
• for (expr1; expr2; expr3) { statements; }
$letters = array(‘a’,’b’,’c’);
foreach ($letters as $key => $value) {
echo “array $key to $value”;
}
Switch statement
• expr is evaluated switch (expr) {
– Case corresponding to case (result1):
result is executed statements;
– Otherwise default case break;
is executed case (result2):
• break statements;
– Ensures next case isn’t break;
executed default:
statements;
}
Switch Example
switch ($name) {
case ‘Rob’:
echo ‘Your name is Rob’;
break;
case ‘Fred’:
echo ‘You are called Fred’;
break;
default:
echo ‘Not sure what your name is’;
}
break, continue, return
• break
– Ends execution of current for, foreach, do … while, while or
switch structure
– Option: Number of nested structures to break out of
• continue
– Skip rest of current loop
– Option: Number of nested loops to skip
• return
– Ends execution of current function/statement/script
Indentation..
• Code readability IS important – notice how all
the code inside a loop/control structure is
indented.
• Once you start writing nested control loops,
indentation is the only way to keep track of
your code!
require, include
• require('filename.ext')
– Includes and evaluates the specified file
– Error is fatal (will halt processing)
• include('filename.ext')
– Includes and evaluates the specified file
– Error is a warning (processing continues)
• require_once / include_once
– If already included won’t be included again
Code Re-use
• Often you will want to write a piece of code
and re-use it several times (maybe within the
same script, or maybe between different
scripts).
• Functions are a very nice way to encapsulate
such pieces of code..
Eh..? What?
• You have already used functions..
echo(‘text to display’);
function myfunction($arg1,$arg2,…,$argN)
{
statements;
return $return_value;
}
Example
• Function to join first and last names together
with a space..
function make_name($first,$last)
{
$fullname = $first.’ ‘.$last;
return $fullname;
}
Calling functions..
• Can be done anywhere..
myfunction($arg1,$arg2,…,$argN)
or
$answer = myfunction($arg1,$arg2,…,$argN)
e.g.
echo make_name(‘Rob’,’Tuley’);
// echoes ‘Rob Tuley’
Functions: Return Values
• Use return()
– Causes execution of function to cease
– Control returns to calling script
• To return multiple values
– Return an array
• If no value returned
– NULL
‘Scope’
• A function executes within its own little
protected bubble, or local scope.
• What does this mean? Its means that the
function can’t ‘see’ any of the variables you
have defined apart from those passed in as
arguments..
• Each new function call starts a clean slate in
terms of internal function variables.
In other words..
• Variables within a function
– Are local to that function
• Disappear when function execution ends
• Variables outside a function
– Are not available within the function
• Unless set as global
• Remembering variables
– Not stored between function calls
• Unless set as static
Global variables..
• To access a variable outside the ‘local’ scope of a
function, declare it as a global:
function add5toa()
{
global $a;
$a = $a + 5;
}
$a = 9;
add5toa();
echo $a; // 14
Static Variables
• Local function variable values are not saved between
function calls unless they are declared as static:
function counter()
{
static $num = 0;
return ++$num;
}
echo counter(); // 1
echo counter(); // 2
echo counter(); // 3
Default Arguments
• Can specify a default value in the function
definition which is used only if no value is
passed to the function when called..
• Defaults must be specified last in the list
function myfunction($arg1,$arg2=‘blah’)…
function myfunction($arg1=‘blah’,$arg2)…
Passing References
• Pass a reference to a variable
– Not the actual variable
• Why?
– Enables a function to modify its arguments
• How?
– Use an ampersand in front of the variable
– &$variable