You are on page 1of 4

PHP Functions a function is a set of statements that performs a particular function andoptionallyreturns a value.

You can pull out a section of code that you have used more than once, place it into a function, and call the function by name when you want the code. Functions have many advantages over contiguous, inline code: Less typing is involved. Functions reduce syntax and other programming errors. They decrease the loading time of program files. They also decrease execution time, because each function is compiled only once, no matter how often you call it. Functions accept arguments and can therefore be used for general as well as specific cases. Defining a Function The general syntax for a function is: function function_name([parameter [, ...]]) { // Statements Return(Variable/Statement); } Ill explain all the square brackets, in case you find them confusing. The first line of the syntax indicates that: A definition starts with the word function. A name follows, which must start with a letter or underscore, followed by any number of letters, numbers, or underscores. The parentheses are required. One or more parameters, separated by commas, are optional. Note : Function names are case-insensitive, so all of the following strings can refer to the print function: PRINT, Print, and PrInT. PHP Functions - Returning Values A function can only return one thing, although that thing can be any integer, float, array, string, etc.

syntax : $myVar = somefunction(); Example : <?php function add($n1, $n2){ return ($n1 + $n2); } echo add(10, 11); ?> Including and Requiring Files The include Statement Using include, you can tell PHP to fetch a particular file and load all its contents. Using include_once Each time you issue the include directive, it includes the requested file again, even if youve already inserted it. Syntax: include 'filename'; Example : Function2.php <?php include "library.php"; echo add(10, 11); ?>

Library.php <?php function add($n1, $n2){ return ($n1 + $n2); } function connect($nametable){ $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($nametable, $con); return($con); } function query($mquery,$no){ $result = mysql_query($mquery); $me = mysql_num_rows($result); while($row = mysql_fetch_array($result)) { if ($no == 1) { echo $row[0] ; echo "<br />"; } elseif ($no == 2) { echo $row[0] . " " . $row[1]; echo "<br />"; } else { echo $row[0] . " " . $row[1]. " " . $row[1]; echo "<br />"; } } } ?> Connecting.php <?php include "library.php"; $tablename = "student"; $con = connect($tablename); query("SELECT * FROM personal",1); mysql_close($con); ?>

Example : Function.php <?php $viewr = 0; include "library.php"; $tablename = "student"; $con = connect($tablename); $query = mysql_query("SELECT * FROM personal"); $result1 = mysql_num_rows($query ); $viewrr = 3; query("SELECT * FROM personal limit $viewr, $viewrr ", 3); mysql_close($con); echo '<form action = connect11.php method = post>'; $y = 0; for ($x = 1; $x < $result1; $x=$x+3){ echo "<input type = submit name = bots value = $y>"; $y = $y + 1; } echo '</form>'; ?> Connect11.php ?php $viewr = $_POST[bots] * 3; include "library.php"; $tablename = "student"; $con = connect($tablename); $query = mysql_query("SELECT * FROM personal"); $result1 = mysql_num_rows($query ); $viewrr = 3; query("SELECT * FROM personal limit $viewr, $viewrr ", 3); mysql_close($con); echo '<form action = connect11.php method = post>'; $y = 0; for ($x = 1; $x < $result1; $x=$x+3){ echo "<input type = submit name = bots value = $y>"; $y = $y + 1; } echo '</form>'; ?>

You might also like