PHP HTML
PHP HTML
Place the PHP tags in any tag of the HTML body and write PHP script inside the PHP tags. Forma ng
tags may be used to format the text.
<!DOCTYPE html>
<html>
<body>
<?php
echo "<b>"."Hello "."</b>"."PHP";
?>
</body>
</html>
<html>
<head>
< tle>Subject list in Sem 5 DCE</ tle>
</head>
// Create a PHP array
<?php
$subjects = array('WD','Python','CHCM','STE');
?>
<h1>List of subjects</h1>
<ul>
// Iterate through PHP array
<?php foreach($subjects as $subject)
{ ?>
// HTML tags are not enclosed in PHP tags
<li><?php echo $subject;?></li>
<?php } ?>
</ul>
</body>
</html>
There are four func ons available in PHP to help include other files within a PHP file. These are
1. include()
2. include_once()
3. require()
4. require_once().
The include() func on will include and evaluate the specified file and give us a warning if it cannot find
the file. The require() func on does the same thing, but it gives us an error instead of a warning if the
file cannot be found.
We might uninten onally include the same file mul ple mes when working on big projects.
include_once() and require_once() func ons in PHP could be used to avoid such issues.
header. php
<html>
<head>
<title>PHP HTML</title>
</head>
<body>
<div>We will display today date</div>
date.php
<?php
include('header.php');
?>
<div>
<?php
echo "Today date is <b>".date("Y/M/d")."</b>"."<br>";
echo "The time is " . date();
?>
</div>
</body>
</html>
Lab exercise 1
Q - Write a PHP script in HTML to create an array of all the programming languages learnt, iterate
through it and display each of them.
Q - Write the PHP script to display the following table where the country and corresponding capital
are stored in PHP associa ve array.
Country Capital
India New Delhi
USA Washington, D.C
Ireland Dublin
A PHP user-defined func on declara on starts with the keyword func on.
Func on names follow the same rules as other labels in PHP. A valid func on name starts with a le er
or underscore, followed by any number of le ers, numbers, or underscores. In PHP, variable names
are case-sensi ve but func on names are not case sensi ve.
<?php
return $retval;
?>
Where arg1, arg2,…. argn are func on arguments and retval is the return value.
A er calling a func on the return value of the func on stores into the $ret variable.
Lab exercise 3