PHP-Lab Programs
PHP-Lab Programs
Example 1:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
$items = Array("Orange","Apple") ;
foreach($items as $value)
print $value . "<br>" ;
?>
</body>
</html>
Output
Orange
Apple
Example 2:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
$items = Array("Orange","Apple") ;
print_r($items);
?>
</body>
</html>
2
Output
Example:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
print_r($items);
?>
</body>
</html>
Output
Array ( [item0] => Array ( [0] => Orange [1] => Apple ) [item1] => Array ( [0] => Cat
[1] => Dog ) )
Associative Array
Example:
html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
3
$employeeAges["Dipu"] = "27" ;
$employeeAges["Ramu"] = "26" ;
$employeeAges["Manu"] = "25" ;
?>
</body>
</html>
Output
Example 2:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
print_r($employeeAges);
?>
</body>
</html>
Output
Example 1:
<html>
<head>
<title>Using print_r </title>
</head>
<body>
<?php
$animals = Array("Cat","Dog");
print_r($animals);
?>
</body>
</html>
Output
Example 2:
<html>
<head>
<title>Using print_r </title>
</head>
<body>
<?php
$animals = Array("a"=>"Cat","b"=>"Dog");
print_r($animals);
?>
</body>
</html>
Output:
Example:
<html>
<head>
<title>My sample script</title>
</head>
<body>
<?php
?>
</body>
</html>
Output
Count is 3
Example 1:
<html>
<head>
<title>Insert into an array</title>
</head>
<body>
<?php
$animals = Array("a"=>"Cat","b"=>"Dog");
array_unshift($animals, "Horse", "Cow");
print_r($animals);
?>
6
</body>
</html>
Output:
Array ( [0] => Horse [1] => Cow [a] => Cat [b] => Dog )
Example 1:
<html>
<head>
<title>Append elements to the array</title>
</head>
<body>
<?php
$numbers = Array(10,20,30);
print "Before appending..<br>" ;
print_r($numbers);
array_push($numbers,40,50);
print "<br>After appending.. <br>" ;
print_r($numbers) ;
?>
</body>
</html>
Output
Before appending..
Array ( [0] => 10 [1] => 20 [2] => 30 )
After appending..
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 )
Example:
<html>
<head>
7
<?php
$numbers = Array(10,20,30,40);
print "Before poping..<br>" ;
print_r($numbers);
$element = array_pop($numbers);
print "<br>Element popped : " . $element ;
print "<br>After poping..<br>" ;
print_r($numbers) ;
?>
</body>
</html>
Output:
Before poping..
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 )
Element popped : 40
After poping..
Array ( [0] => 10 [1] => 20 [2] => 30 )
Example 1:
<html>
<head>
<title>Sort ordinary array</title>
</head>
<body>
<?php
$numbers = Array(30,10,20,40);
print "Before sorting..<br>" ;
print_r($numbers);
print "<br>After Sorting..<br>" ;
sort($numbers);
print_r($numbers) ;
8
?>
</body>
</html>
Output:
Before sorting..
Array ( [0] => 30 [1] => 10 [2] => 20 [3] => 40 )
After Sorting..
Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 )
Example:
<html>
<head>
<title>Sort ordinary array</title>
</head>
<body>
<?php
$numbers = Array(30,10,20,40);
print "Before sorting..<br>" ;
print_r($numbers);
print "<br>After Sorting..<br>" ;
rsort($numbers);
print_r($numbers) ;
?>
</body>
</html>
Output:
Before sorting..
Array ( [0] => 30 [1] => 10 [2] => 20 [3] => 40 )
After Sorting..
Array ( [0] => 40 [1] => 30 [2] => 20 [3] => 10 )
9
Example:
<html>
<head>
<title>Sort associative array</title>
</head>
<body>
<?php
$fruits = Array("o"=>Orange,"a"=>"Apple","g"=>"Grapes");
print "Before sorting..<br>" ;
print_r($fruits);
print "<br>After Sorting..<br>" ;
asort($fruits);
print_r($fruits) ;
?>
</body>
</html>
Output:
Before sorting..
Array ( [o] => Orange [a] => Apple [g] => Grapes )
After Sorting..
Array ( [a] => Apple [g] => Grapes [o] => Orange )
Example:
<html>
<head>
<title>Sort associative array</title>
</head>
<body>
<?php
$fruits = Array("o"=>Orange,"a"=>"Apple","g"=>"Grapes");
print "Before sorting..<br>" ;
print_r($fruits);
10
?>
</body>
</html>
Output:
Before sorting..
Array ( [o] => Orange [a] => Apple [g] => Grapes )
After Sorting..
Array ( [o] => Orange [g] => Grapes [a] => Apple )