Title: Type casting
1Type casting
- In type casting you explicitly change the data
type of a variable from one type to another.
2Example
val Hello Greet (array) val echo
Greet0 //Result Hello
3CONSTANT
4Defining constants
- A constant is a space in the memory where you can
store values that do not change during the
execution of a program. - A constant has the following properties
- It is case sensitive and can be in either
uppercase or lowercase - It follows the same naming convention as a
variable - It can only begin with alphabet or underscore
- By default it has global scope
5Example
- lt?php
-
- define (CONSTANT, This cannot be changed)
- echo CONSTANT
- ?gt
6CONTROL STRUCTURE
7Defining constants
- Control structure are the decision making
mechanisms of any programming language and are
used for evaluating conditions. In any
programming languages, including PHP control
structure are of two types - Conditional statement These statements execute a
code if a condition is either True or False. - Conditional loops These loops run a set of
instructions once or multiple times until a
condition returns True or False. -
8Conditional statement
- The if statement is used to evaluate an
expression and returns a Boolean value. The
Boolean value can be either True or False. If the
condition returns True, the set of statements
following if condition are executed.
Syntax if (expression) statement1 Or if
(expression) statement1
9Conditional statement
- The if statement is used to evaluate an
expression and returns a Boolean value. The
Boolean value can be either True or False. If the
condition returns True, the set of statements
following if condition are executed.
Syntax if (expression) statement1 Or if
(expression) statement1
10Example
lt?php myvalue 5 if (!(myvalue4))
echo "See! I told you 'Myvalue' cannot be
4!"."ltbrgt" if (TRUE) echo "I will
always run."."ltbrgt" ?gt
11The else Statement
- The else statement can only be used along with
the if statement and not seperately. - The code after the else statement will be
executed only when the if statement returns false - You can have only a single else statement and it
should always be the last set of code in the
block
12Example
lt?php age19 if (age lt 18) echo "You
are not an adult since your age is less
than18" //This statement will not be
printed else echo "You are an adult
since your age is more than 18" //This
statement will be printed ?gt
13The else if Statement
- The else if statement is used along with the if
and else statements. You can have multiple else
if statements in a single if construct. - You can use the words else if interchangeably
because in PHP both mean the same thing and
return the same value.
14Example
lt?php num 50 if ((numgt9)
(numlt100)) echo num . " is a 2 digit
number" elseif (num gt100) echo
num . "has 3 digits or more!" else e
cho num . " is a single digit number" ?gt
15The switch Statement
- When a matching value is found, the control
shifts to the specific case statement and all the
statements under it are executed sequentially. - If no matching value is found, then the
statements stored under the default label are
executed until the end of the block is reached or
you come across a break statement.
16The switch Statement
Following is the syntax of the switch
statement switch (variable) case
value1 statement statement case
value2 statement statement ...........
... default statement statement
17Example
lt?php val "first" switch (val) case
"first" echo "You have encountered the first
case.", "\n" break case "second" echo
"You have encountered the second case.",
"\n" break case "third" echo "You have
encountered the third case.", "\n" break ca
se "fourth" echo "You have encountered the
fourth case.", "\n" break ?gt