PHP Part 3_V2
PHP Part 3_V2
Semester 3
IT 3505
$array_variable[new_index] = new_value;
Syntax :
array_push(array_variable, value1,value2,……)
Example :
$a = array("Nimal","Saman");
array_push($a,"Kamal","Waruna");
IT3505 Web Application Development II
Array of arrays
• Elements of an array can also be arrays.
example :
$a = array(
"males" => array("a" => "Nimal","b" => "Amara","c"
=>"Kamal"),
"females" => array("a" => "Kumari", "b" => "Nirmala", "c" =>
"Kamala"),
"fees" => array (2500,1500,500)
);
Syntax :
foreach (array_expression as $value) statement
Or
foreach (array_expression as $key => $value)
statement
IT3505 Web Application Development II
Looping through array elements -
Example
<?php
$a = array(
1=> "First Item",
"item2" => "Second Item",
5 => "Third item",
"Forth item"
);
<?php
include "example2.php";
echo "Executing the file example1.php\n";
?>
The following script is in a file named example2.php in the
same folder
<?php
echo "Executing the file example2.php\n";
?>
<?php
echo "Executing the file example1.php\n";
require "example2.php";
?>
The following script is in a file named example2.php in the
same folder
<?php
echo "This is the footer to be included in every web page \n";
?>
<html>
<head>
<title>Including PHP in web pages</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<div><?php echo "This is the first script"; ?></div>
<div>
<?php echo "This is the second script<br />";
echo "With many lines<br />";
?>
</div>
</body>
</html>
IT3505 Web Application Development II
How web servers execute PHP
scripts
</body>
</html> <!DOCTYPE html>
<html>
The name attribute specifies <body>
the key value of the $_POST <div> Hello <?php echo $_POST["name"]?></div>
global array element from </body>
which the value of this input </html>
item can be retrieved
</html>
• To delete a
cookie execute
the same
setcookie()
<?php
function with an setcookie("name",“Kamal",tim
expiration date in e()-3600);
the past. ?>
Semantics:
• Reads a line from the file pointed to by the file pointer $handle.
• The command returns
– A line of symbols (including the end of line marker) from the file as a
string when the $length parameter is not specified or
– A string of up to length - 1 bytes from the file when $length
parameter is specified or
– The Boolean value FALSE when there is no more data to read in the
file or
– The Boolean value FALSE if an error occurred while reading the file.