PHP Functions Ex. #1: Simple Function
Functions in PHP can help automate repetitive tasks and enable you to reuse code with a simple function call. For your first function, we'll keep it really simple.
For this PHP exercise, create a function called "hello" that outputs that phrase we all know and love, "Hello, World!" to the browser. Call the function.
 
Here's the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>Hello Function</title>
</head>
<body>
<h2>Hello Function</h2>
<?php
//Define function.
function hello(){
echo "Hello, World!";
}
//Call function.
hello();
?>
</body>
</html>
See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.