UNIT-3 - Working With Arrays and Functions
UNIT-3 - Working With Arrays and Functions
SEMESTER : 5
UNIT : 3
Indexed array/
Numeric Array
An array with a numeric key.
Associative array An array where each key has its own specific
descriptive names key .
Multidimensional
array
An array containing one or more arrays within itself.
Indexed Arrays
An indexed or numeric array stores each array element with a numeric index.
Values are stored and accessed in linear fashion.
Array index is represented by number which starts from 0. We can store number,
string and object in the PHP array. All PHP array elements are assigned to an index
number by default.
1st way: $season=array("summer","winter","spring","autumn");
Example :
<?php
$season=array("summer","winter","spring","autumn");
echo "Season are: $season[0], $season[1], $season[2]
and $season[3]";
foreach($season as $a)
{
echo “$a <br>”;
}
Associative Arrays
Associative array will have string as index so that you can establish a strong association
between key and values.
Associative arrays are arrays that use named keys that you assign to them.
Syntax : <?php
$variable_name['key_name'] = value;
$variable_name = array('keyname' => value);
?>
$variable_name is the name of array the variable
['key_name'] is the access index of the element
“value” is the value assigned to the array element.
Associative Arrays
To store the salaries of employees in an array, a numerically indexed array would not be
the best choice. Instead, we could use the employees names as the keys in our associative
array, and the value would be their respective salary.
Example :<?php
$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");
echo "Sonoo salary: ".$salary["Sonoo"]."<br/>";
echo "John salary: ".$salary["John"]."<br/>";
echo "Kartik salary: ".$salary["Kartik"]."<br/>";
foreach($salary as $k=>$v)
{ echo "Key is ".$k." Value :".$v;
echo "<br>";
}
?>
Multidimesional Arrays
<?php
$cars = array ( array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
Multidimesional Arrays in PHP: for
<?php $cars = array ( array("Volvo",22,18), array("BMW",15,13),
array("Saab",5,2), array("Land Rover",17,15)
);
for ($row = 0; $row < 4; $row++)
{ echo "<p><b>Row number $row</b></p>"; echo "<ul>";
for ($col = 0; $col < 3; $col++)
{
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}
Viewing Array Structure and Values
The structure and values of any array can be display using one of two statements —
var_dump() or print_r()
The print_r() function prints the information about a variable in a more human-readable
way.
Syntax : print_r(variable [, return]);
<?php
$a = array("red", "green", "blue");
print_r($a); echo "<br>";
$b = array("Peter"=>"35", “Smith"=>"37", "Joe"=>"43");
print_r($b);
?>
print_r() Function
The structure and values of any array can be display using one of two statements —
var_dump() or print_r()
The print_r() function prints the information about a variable in a more human-readable
way.
Syntax : print_r($variablename);
<?php
$a = array("red", "green", "blue");
print_r($a); echo "<br>";
$b = array("Peter"=>"35", “Smith"=>"37", "Joe"=>"43");
print_r($b);
?>
Adding Elements to arrays
You can add automatically indexed elements usng [ ] operator.
Example:
<?php
$myarray=array();
$myarray [ ] =‘Good Morning’;
$myarray[ ]=‘Have a nice Day’;
$myarray[ ]=‘Today is Tuesday’;
print_r($myarray);
?>
Array Sorting Functions
Syntax :
function functionname()
{
//code to be executed
}
User Defined Functions
Example :
<?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>
User Defined Functions
Example :
<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>
User Defined Functions
Example :
<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>
Parameter Passing Defined Functions
PHP Parameterized functions are the functions with parameters. You
can pass any number of parameters inside a function. These passed
parameters act as variables inside your function.
They are specified inside the parentheses, after the function name.
The output depends upon the dynamic values passed as the parameters
into the function.
Parameter Passing Defined Functions
Example :<?php //Adding two numbers
function add($x, $y)
{ $sum = $x + $y;
echo "Sum of two numbers is = $sum <br><br>";
}
add(467, 943);
//Subtracting two numbers
function sub($x, $y)
{ $diff = $x - $y;
echo "Difference between two numbers is = $diff";
}
sub(943, 467);
?>
Call by Value
PHP allows you to call function by value and reference both. In case of
PHP call by value, actual value is not modified if it is modified inside the
function.
Example :
<?php function adder($str2)
{ $str2 .= 'Call By Value'; }
$str = 'Hello ';
adder($str);
echo $str;
?>
Call by Reference
You can set a parameter to have a default value if the function's caller doesn't
pass it.
Example : <?php
function printMe($param = NULL)
{ print $param; }
printMe("This is test");
printMe();
?>
Dynamic Function Calls
$function_holder = "sayHello";
$function_holder();
?>
Dynamic Function Calls
$function_holder = "sayHello";
$function_holder();
?>
String Function
Ltrim Chr
Rtrim Ord
Trim Chr
Substr Ord
Strcmp Strtolower
strrev Strtoupper
Date Functions
PHP date() function shows day, month and year altogether. Date and time
are stored in computer in UNIX Timestamp format. It calculates time in
number of seconds on GMT (greenwich mean time) i.e started from January
1, 1970, 00:00:00 GMT.
Syntax : string date ( string $format [, int $timestamp = time() ] )
Date function allows you to display date and time in different format and
manipulate it.
Different date functions in PHP are as below:
date
getdate
checkdate
Date Functions
getdate : returns an array with date, time information for an unix timestamp.
Syntax: getdate(timestamp)
Returns an associative array with information related to the timestamp:
[seconds] - seconds
[minutes] - minutes
[hours] - hours
[mday] - day of the month
[wday] - day of the week (0=Sunday, 1=Monday,...)
[mon] - month
[year] - year
[yday] - day of the year
[weekday] - name of the weekday
[month] - name of the month
Date Functions
• Example:
<?php
print_r(getdate());
?>
Date Functions
• checkdate :check the validity of a givendate.
• Syntax: Boolean checkdate ( int month , int day, int year)
• Example:
<?php
echo checkdate(2,28,2016).“<br/>”;
echo checkdate(28,2,2016).“<br/>”;
?>
THANK YOU