PHP HTML

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Using PHP in 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>

Use of PHP loops in HTML page

We can iterate a HTML code within PHP script.

<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>

Include code from different files

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

Include header.php in date.php using include() func on.

<?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.

Itera ng through associa ve array

foreach($arr as $key => $val)


{
echo "<tr>";
echo "<td>$key</td>";
echo "<td>$val</td>";
echo "</tr>";
}
Lab exercise 2

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

Crea ng a user defined func on in PHP

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

func on func($arg_1, $arg_2, /* ..., */ $arg_n)

echo "Example func on.\n";

return $retval;

?>

The syntax to call a PHP user-defined func on -


$ret=funName($arg1, $arg2, ... $argn);

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

Q- Write a recursive func on in PHP to calculate the factorial of a number.

You might also like