Learn PHP


Function



A function is a self contained block of code that performs a specific task.


Functions avoid duplicating code. They make it easier to eliminate errors.


Functions can be reused in other scripts. They help you break down a big project.


Parameter


A function often accepts one or more arguments, which are values passed to the function by the code that calls it.


The function can then read and work on those arguments.


Return


A function may optionally return a value that can then be read by the calling code.


In this way, the calling code can communicate with the function.


PHP Function Create


We define functions with the function keyword, followed by the name of the function and two parentheses.


The actual code your function will execute lies between braces.


		  	function myFunc() { 
			   // (do stuff here) 
			}   
		  

Example - define a function


		  	<?php
	         function hello() { 
	            echo "Hello, world!\n"; 
	         } 
	         hello(); 
			?>
		  

PHP Function Return


Function can return one value back from functions by using the return statement.


If you try to assign to a variable the return value of a function that has no return value, your variable will be set to NULL.


Example - return value from a function


		  	<?php
			function foo() { 
			       print "In function"; 
			       return 1; 
			       print "Leaving function..."; 
			} 
			print foo(); 
			?>
		  


PHP Return Reference


As well as passing variables by reference into functions, you can also get functions to return references, rather than values.


Place an ampersand before the function name in your function definition. Then, when you return a variable with the return statement, you pass a reference to that variable back to the calling code, rather than the variable ' s value:


		  	function  &myFunc(){ 
			    // (do stuff) 
			   return $var;  // Returns a reference to $var 
			}   
		  

		  	<?php
			   $myNumber = 5; 

			   function  &getMyNumber() { 
			     global $myNumber; 
			     return $myNumber; 
			   } 

			   $numberRef = &getMyNumber(); 
			   $numberRef++; 
			   echo "\$myNumber = $myNumber\n";   // Displays "6" 
			   echo "\$numberRef = $numberRef\n"; // Displays "6"   
			?>
		  

PHP Recursive Functions


A recursive function is a function which call itself.


Here's a quick overview of how a recursive function operates:


The recursive function is called by the calling code If the base case is met, the function does required calculation, then exits.


Otherwise, the function does required calculation, then calls itself to continue the recursion.


		  	<?php
			function factorial($number) { 
			        if ($number == 0) return 1; 
			        return $number * factorial($number-1); 
			} 

			print factorial(6); 
			?>
		  

Example - A PHP recursive function


		  	<?php
			  function checkInteger($Number)
			  {
			    if($Number > 1)
			    {
			      // integer minus one is still an integer
			      return(checkInteger($Number-1));
			    }
			    elseif($Number < 0)
			    {
			      //numbers are symmetrical, so
			      //check positive version
			      return(checkInteger((-1)*$Number-1));
			    }
			    else
			    {
			      if(($Number > 0) AND ($Number < 1))
			      {
			        return("no");
			      }
			      else
			      {
			        //zero and one are
			        //integers by definition
			        return("yes");
			      }
			    }
			  }

			  print("Is 0 an integer? " .  checkInteger(0) . "<br>\n");
			  print("Is 7 an integer? " . checkInteger(7) . "<br>\n");
			  print("And 3.5? " . checkInteger(3.5) . "<br>\n");
			  print("What about -5? " . checkInteger(-5) . "<br>\n");
			  print("And -9.2? " . checkInteger(-9.2) . "<br>\n");
			?>
		  


PHP Anonymous Function


PHP anonymous functions have no name.


To create functions dynamically


To create short-term, disposable functions


Syntax


To create an anonymous function, you use separated list of parameters if any, and the code for the function body.


$myFunction = create_function( '$param1, $param2', 'function code here;' );


Example for anonymous function


		  	<?php
			$mode = "+"; 
			$processNumbers = create_function( '$a, $b', "return \$a $mode \$b;" ); 
			echo $processNumbers( 2, 3 ); // Displays "5"   
			?>
		  

Example - dynamical function


		  	<?php
			  function write($text)
			  {
			    print($text);
			  }

			  function writeBold($text)
			  {
			    print("<b>$text</b>");
			  }

			  $myFunction = "write";
			  $myFunction("Hello!");
			  print("<br>\n");

			  $myFunction = "writeBold";
			  $myFunction("Goodbye!");
			  print("<br>\n");
			?>
		  



PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o