PHP Functions

PHP Functions tutorial explain you How to write Functions in PHP? with simple examples.

PHP Function is a special block of code that we can write and will be executed whenever we need it. Using PHP functions we can write a code which is more readable.

There are two PHP function types: User Defined and Built in. PHP has more than 700 built-in functions in PHP that can be used to write PHP script.

PHP Built-In Functions

Like other programming language, PHP offers some built-in functions for day-to-day use in our coding. Most of these functions are very helpful in achieving programming goals and are well documented.

Create Function in PHP

At the time you are creating a PHP function, follow these simple guidelines:

  • Always begin your function with the keyword function.
  • Remember that your function's code must be between the "{" and the "}".
  • At the time you are utilizing your function, make sure you spell the function name correctly.

When we create a function, we first need to give it a name, like my_function.

Example on create function in PHP:

<?php 
// This function will not execute until call it 
function my_function () 
{      
 echo "Hello World!";      
} 
// Make the function run here by calling it 
my_function (); 
?>

PHP Functions Arguments

We pass arguments to PHP functions in order to process variable data and display, store, or return results.

Example on Passing one argument to function:

<?php 
// This function will not execute until call it 
function my_function ($var1) 
{      
 echo "Hello World!" . $var1; 
}	 
// Make the function run here by calling it now and adding an argument value
my_function (" This is a user defined function with one argument."); 
?>

The Output will be:


Hello World! This is a user defined function with one argument.

Example on Passing more than one argument to PHP functions:

<?php 
// This function will not execute until call it 
function my_function ($var1, $var2) 
{ 
 echo  $var1 . $var2; 
} 
// Send the function 2 arguments and fire the function off 
my_function ("PHP ", "is a scripting language."); 
?>

The Output will be:


PHP is a scripting language.

Returning Values from PHP functions

Many times we will want our custom PHP function to return a value to your script rather than displaying the data when the function runs.

Using the PHP return statement we can accomplish this easily. The return statement will stop the function from running any further, and send a return value back to the code that called the function to run.

However, PHP function can only return one value, although that value can be any integer, float, array, string, etc. that we choose!
To capture this value we can set a variable equal to the PHP function.

Example on Returning value from function in PHP:

<?php 
function mySum($numX, $numY)
{ 
 $total = $numX + $numY; 
 return $total; 
} 
$myNumber = 0; 
echo "Before the function, myNumber = ". $myNumber ."<br />"; 
$myNumber = mySum(3, 4);  // Store the result of mySum in $myNumber 
echo "After the function, myNumber = " . $myNumber ."<br />"; 
?>

Output will be:


Before the function, myNumber = 0
After the function, myNumber = 7
   

Online PHP Guide Quiz

You can Check your knowledge by playing the Online PHP Quiz into Various category.

Online PHP Quiz useful to the participants of competitive examinations and quiz programs.

Would you like to test your PHP skills?

   

Search

   

Online PHP Guide Quiz

Would you like to test your PHP skills?

   

Facebook Like Button

   

Follow Us On Google+

 

 

   
Copyright 2012@OnlinePHPGuide.com