Skip to main content
  1. Tech
  2. Gadgets & Tech
  3. Tech Gear

Using functions in a PHP program (part 2)

See also

As we continue with the series of Using functions in a PHP program, the next program will demonstrate the function block. As shown in the Using functions in a PHP program (part 1) article, in order for us to display a company logo throughout a document, three lines of code are required, these lines of code are typed each time the company logo is shown. To remedy the repetition of typing the three lines of code, the function block is used. The format is shown below:

function functionname()

{

block of statements;

return;

}

The PHP programming code listed below demonstrates displaying a company logo two times in the same script.

NOTE: The jpeg images are stored in the "C:\WAMPSERVER\www\images" folder. As demonstrated in the previous programs, we used notepad a plain text editor. The PHP programs are stored on our web server, "C:\Wampserver\www." Please note the stored web server location.

Now let us create the PHP program, please follow the steps outlined below:

  1. Select and click "Start."
  2. Select and click "All Programs."
  3. Select and click "Accessories."
  4. Select and click "Notepad."
  5. Type the PHP code exactly as shown below.
  6. Once you have type the entire program, Select "File," "Save As," and then go to the webroot of your server. For example, C:\Wampserver\www. Type Filename, "logotwice.php," and then Select "Save."

<html>

<head>

<title>LOGO</title>

</head>

<body>

<h1>LOGO</h1>

<h3>Demonstrates LOGO<h3>

<?php

function logo()

{

print "<p><img src = 'images/JONESLOGO.jpg' width='700' height='125'/><p>";

print "<p style='font-size: large'>The Finest Florist </p>";

print "<p style='font-style: italic'>beautiful creative designs</p>";

return;

}

print "<p><h1>Here is the first Jones Florist Logo</h1><p>";

logo();

print("<hr>");

print("<hr>");

print "<p><h1>Here is the second Jones Florist Logo</h1><p>";

logo();

?>

<br>

</body>

</html>

In the logotwice.php program, the function block is identified as "function logo()", each time you want the company code to be displayed all you need to do is call the function by using "logo()."

Once displayed on the internet, the function block eliminates the need to type the three lines of code each time the company logo is needed to be displayed. This is definitely a more sufficient method to use.

Use your browser to view the logotwice.php program. The steps are outlined below:

  1. Select and click "Start. "
  2. Select and click "All Programs."
  3. Select and click "Internet Explorer."
  4. Locate the Web Address Field, near the top-left of the Internet Explorer window and type in the Web address of the logotwice.php program, "http://localhost/logotwice.php," then enter.

In our next article, Using functions in a PHP program (part 3), we will demonstrate how to use variables in functions.

Advertisement