0% found this document useful (0 votes)
13 views

PHP2.ppt

The document provides an overview of PHP constants, conditional statements, loops, functions, and arrays. It explains how to create constants, use conditional statements like if, switch, and various loop types such as while, for, and foreach. Additionally, it covers user-defined functions, function arguments, and different types of arrays including indexed, associative, and multidimensional arrays.

Uploaded by

veena v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

PHP2.ppt

The document provides an overview of PHP constants, conditional statements, loops, functions, and arrays. It explains how to create constants, use conditional statements like if, switch, and various loop types such as while, for, and foreach. Additionally, it covers user-defined functions, function arguments, and different types of arrays including indexed, associative, and multidimensional arrays.

Uploaded by

veena v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

PHP Constants

Constants are like variables except that once they


are defined they cannot be changed or
undefined.
• A constant is an identifier (name) for a simple
value. The value cannot be changed during the
script.
• A valid constant name starts with a letter or
underscore (no $ sign before the constant name).
• Note: Unlike variables, constants are
automatically global across the entire script.
Create a PHP Constant
• To create a constant, use the define() function.
• Syntax
• define(name, value, case-insensitive)
• Parameters:
• name: Specifies the name of the constant
• value: Specifies the value of the constant
• case-insensitive: Specifies whether the
constant name should be case-insensitive.
Default is false
Constants are Global
• Constants are automatically global and can be used across the
entire script
PHP Conditional Statements
Conditional statements are used to perform different actions
based on different conditions.
In PHP we have the following conditional statements:
 if statement - executes some code if one condition is
true
 if...else statement - executes some code if a condition
is true and another code if that condition is false
 if...elseif...else statement - executes different codes for
more than two conditions
 switch statement - selects one of many blocks of code
to be executed
PHP - The if Statement
• The if statement executes some code if one
condition is true.
• Syntax
• if (condition)
{
code to be executed if condition is true;
}
• PHP - The if...else Statement
• The if...else statement executes some code if a
condition is true and another code if that
condition is false.
• Syntax
• if (condition)
{
code to be executed if condition is true;
} else
{
code to be executed if condition is false;
}
• PHP - The if...elseif...else Statement
• The if...elseif...else statement executes different codes for more
than two conditions.
• Syntax
if (condition)
{
code to be executed if this condition is true;
}
elseif (condition)
{
code to be executed if first condition is false and this condition is
true;
}
else
{
code to be executed if all conditions are false;
}
• The PHP switch Statement
• Use the switch statement to select one of many blocks of code to
be executed.
• Syntax
• switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
PHP Loops
• Often when you write code, you want the same block of
code to run over and over again a certain number of times.
So, instead of adding several almost equal code-lines in a
script, we can use loops.
• Loops are used to execute the same block of code again
and again, as long as a certain condition is true.
• In PHP, we have the following loop types:
• while - loops through a block of code as long as the
specified condition is true
• do...while - loops through a block of code once, and then
repeats the loop as long as the specified condition is true
• for - loops through a block of code a specified number of
times
• foreach - loops through a block of code for each element in
an array
• The PHP while Loop
• The while loop executes a block of code as
long as the specified condition is true.
• Syntax
• while (condition is true)
{
code to be executed;
}
• Example Explained
• $x = 1; - Initialize the loop counter ($x), and
set the start value to 1
• $x <= 5 - Continue the loop as long as $x is less
than or equal to 5
• $x++; - Increase the loop counter value by 1
for each iteration
• Example Explained
• $x = 0; - Initialize the loop counter ($x), and
set the start value to 0
• $x <= 100 - Continue the loop as long as $x is
less than or equal to 100
• $x+=10; - Increase the loop counter value by
10 for each iteration
• The PHP do...while Loop
• The do...while loop will always execute the
block of code once, it will then check the
condition, and repeat the loop while the
specified condition is true.
• Syntax
• Do
{
code to be executed;
}
while (condition is true);
• Note: In a do...while loop the condition is
tested AFTER executing the statements within
the loop. This means that the do...while loop
will execute its statements at least once, even
if the condition is false. See example below.
• This example sets the $x variable to 6, then it
runs the loop, and then the condition is
checked:
The PHP for Loop
• The for loop is used when you know in advance how
many times the script should run.
• Syntax
• for (init counter; test counter; increment counter)
{
code to be executed for each iteration;
}
• Parameters:
• init counter: Initialize the loop counter value
• test counter: Evaluated for each loop iteration. If it
evaluates to TRUE, the loop continues. If it evaluates to
FALSE, the loop ends.
• increment counter: Increases the loop counter value
• Example Explained
• $x = 0; - Initialize the loop counter ($x), and
set the start value to 0
• $x <= 10; - Continue the loop as long as $x is
less than or equal to 10
• $x++ - Increase the loop counter value by 1 for
each iteration
• Example Explained
• $x = 0; - Initialize the loop counter ($x), and
set the start value to 0
• $x <= 100; - Continue the loop as long as $x is
less than or equal to 100
• $x+=10 - Increase the loop counter value by
10 for each iteration
The PHP foreach Loop
• The foreach loop works only on arrays, and is
used to loop through each key/value pair in an
array.
• Syntax
• foreach ($array as $value)
{
code to be executed;
}
• For every loop iteration, the value of the current
array element is assigned to $value and the array
pointer is moved by one, until it reaches the last
array element.
PHP Break and Continue
• PHP Break
• The break statement can also be used to jump
out of a loop.
• This example jumps out of the loop when x is
equal to 4:
• PHP Continue
• The continue statement breaks one iteration
(in the loop), if a specified condition occurs,
and continues with the next iteration in the
loop.
• This example skips the value of 4:
PHP Functions
PHP Built-in Functions
• PHP has over 1000 built-in functions that can be called
directly, from within a script, to perform a specific task.
PHP User Defined Functions
• Besides the built-in PHP functions, it is possible to
create your own functions.
• A function is a block of statements that can be used
repeatedly in a program.
• A function will not execute automatically when a page
loads.
• A function will be executed by a call to the function.

Create a User Defined Function in PHP
• A user-defined function declaration starts with
the word function:
Syntax
• function functionName()
{
code to be executed;
}
• Note: A function name must start with a letter
or an underscore. Function names are NOT
case-sensitive.
PHP Function Arguments
• Information can be passed to functions through
arguments. An argument is just like a variable.
• Arguments are specified after the function name,
inside the parentheses. You can add as many
arguments as you want, just separate them with
a comma.
• The following example has a function with one
argument ($fname). When the familyName()
function is called, we also pass along a name (e.g.
Ajith), and the name is used inside the function,
which outputs several different first names, but
an equal last name:
The following example has a function with two arguments
($fname and $year):
PHP Default Argument Value
• The following example shows how to use a
default parameter. If we call the function
setHeight() without arguments it takes the
default value as argument:
• PHP Functions - Returning values
• To let a function return a value, use
the return statement:
PHP Return Type Declarations
• To declare a type for the function return, add
a colon ( : ) and the type right before the
opening curly ( { )bracket when declaring the
function.
• In the following example we specify the return
type for the function:
You can specify a different return type, than the argument
types, but make sure the return is the correct type:
PHP Arrays
An array stores multiple values in one single variable:
• What is an Array?
• An array is a special variable, which can hold more than one
value at a time.
• If you have a list of items (a list of car names, for example),
storing the cars in single variables could look like this:
• $cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
However, what if you want to loop through the cars and find
a specific one? And what if you had not 3 cars, but 300?
The solution is to create an array!
• An array can hold many values under a single name, and
you can access the values by referring to an index number.
Create an Array in PHP
• In PHP, the array() function is used to create
an array:
array();
In PHP, there are three types of arrays:
1. Indexed arrays - Arrays with a numeric index
2. Associative arrays - Arrays with named keys
3. Multidimensional arrays - Arrays containing
one or more arrays
• Get The Length of an Array - The count() Function
• The count() function is used to return the length (the
number of elements) of an array:
1.PHP Indexed Arrays
• There are two ways to create indexed arrays:
a.The index can be assigned automatically (index
always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
b.or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
The following example creates an indexed array named $cars,
assigns three elements to it, and then prints a text containing the
array values:
Loop Through an Indexed Array
• To loop through and print all the values of an
indexed array, you could use a for loop, like
this:
• PHP Associative Arrays
• Associative arrays are arrays that use named keys
that you assign to them.
• There are two ways to create an associative
array:
• $age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
or:
• $age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
• The named keys can then be used in a script:
Loop Through an Associative Array To loop through and print all the values of
an associative array, you could use a foreach loop, like this:
• PHP - Multidimensional Arrays
• A multidimensional array is an array containing
one or more arrays.
• PHP supports multidimensional arrays that are
two, three, four, five, or more levels deep.
However, arrays more than three levels deep are
hard to manage for most people.
• The dimension of an array indicates the number
of indices you need to select an element.
• For a two-dimensional array you need two indices
to select an element
• For a three-dimensional array you need three
indices to select an element
• PHP - Two-dimensional Arrays
• A two-dimensional array is an array of arrays (a three-dimensional array is
an array of arrays of arrays).
• First, take a look at the following table:
• We can store the data from the table above in
a two-dimensional array, like this:

Now the two-dimensional $cars array contains four arrays, and it has two
indices: row and column.
To get access to the elements of the $cars array we must point to the two
indices (row and column):
PHP - Sort Functions For Arrays
• sort() - sort arrays in ascending order
• rsort() - sort arrays in descending order
• asort() - sort associative arrays in ascending
order, according to the value
• ksort() - sort associative arrays in ascending
order, according to the key
• arsort() - sort associative arrays in descending
order, according to the value
• krsort() - sort associative arrays in descending
order, according to the key

You might also like