1. Computing & Technology

Discuss in my forum

Learn PHP - All About PHP Functions

By , About.com Guide

5 of 5

More Function Writing
Functions can contain any of the information we have learned so far. Let's combine functions with some of the material we learned earlier to create a multiplication table:

 <?php 
 function mul() 
 { 
 global $start; 
 print "<tr>"; 
 for ($num=1; $num <= 10; $num++ ) 
 	{ 
 	$cell = $num * $start; 
 	print "<td> " . $cell . " </td>"; 
 	} 
 print "</tr>"; 
 } 
 $start = 0; 
 print "<table border=1 cellpadding=3>"; 
 while ( $start <=10 ) 
 	{ 
 	mul(); 
 	$start++; 
 	} 
 print "</table>"; 
 ?> 

One new thing you may have noticed is "GLOBAL". Since the variable $start is not defined within the function, we use the tag "GLOBAL" to let it know that it needs to use the $start variable that we have defined outside of the function.

©2012 About.com. All rights reserved.

A part of The New York Times Company.