home

Php Functions Tutorial


A Function is a reusable piece of code. You will write it once and you can use it many times. For example, We can make a function to perform addition or subtraction or calculation of VAT, And we can use it as many times as we want. Functions have a body structure{} In PHP the keywords for functions are: function, return

To use a function we have two steps:

To declare a function:

you use the keyword function followed by the name of the function, two parentesis and the block of code in between two curly braces:

function fooName(parameters)
{
    // Function body goes here
    return $returnValue;
}

Functions do not have to return anything, so the return statement can be left out.

Variables used within the function are local to that function and do not effect the global scope.

User defined PHP Functions

zero parameter funtion

Using return keyword is optional, here two samples:

returning a value..

<?php //functions.php  
   //function declaration
   function returnName()
  {
      $returnValue = "<p>my name is Shahid</p>";
return $returnValue;
 } //function call echo returnName(); ?>

without returning a value..

<?php //functions.php  
   //function declaration
   function displayName()
  {
      echo "<p>my name is Shahid</p>";
 } //function call displayName(); ?>

function with parameters

You can parse parameters (also called arguments) to a function to make it work with them.

 

<?php //functions.php  
   //function declaration (the function asks for 2 parameters)
   function displayFirstAndLastName($first, $last)
  {
      echo "<p>my first name is <strong>$first</strong>" .
 " and my last name is <strong>$last</strong></p>";
 } //call the function parsing two strings displayFirstAndLastName("John", "Doe"); //my first name is John and my last name is Doe ?>

 

Here other samples of user defined functions:

<?php //functions.php  
   //function declaration
   function add($a, $b)
   {
//declaring a variable to hold result of the addition
 $result;
//calculating...
 $result = $a + $b;
//return the result
 return $result; } //function call echo "<p>" . add(1, 2) . "</p>"; //3 //another function call echo "<p>" . add(10, 20) . "</p>"; //30 ?>

 

<?php //functions.php  
   //function declaration
   function subtract($a, $b)
   {
//declaring a variable to hold result of the subtraction
 $result;
//calculating...
 $result = $a - $b;
//return the result
 return $result; } //function call echo "<p>" . subtract(4, 2) . "</p>"; //2 //another function call echo "<p>" . subtract(100, 20) . "</p>"; //80 ?>

 

<?php //functions.php  
   //function declaration
   function vat($price)
   {
//declaring a variable to hold result of the addition
 $result;
//calculating...
 $result = ($price * 20) / 100;
//echoing the result
 echo "The VAT of $price is: $result"; } //function call echo "<p>" . vat(100) . "</p>"; ?>

 

PHP Built-in Functions

PHP comes with many built-in (or internal) functions. We don't have to define those functions. We just need to use them, by calling their names and passing them values. A list of all PHP builtin functions is given at php manual funtion reference

PHP has nearly 3,000 built in functions covering pretty much anything you could want to do with a web application. Luckily, it also has a fantastic manual. The manual not only includes full documentation and example code for almost every function but also provides a user comments feature which is full of valuable tips. If you are having a problem with a PHP function check the manual page - the chances are someone else will have had the same problem and documented a solution in the comments section.

A useful shortcut for looking up the manual page for a function is to type www.php.net/name-of-the-function straight in to your browqser - the PHP site will redirect you to the manual page for that function. For example, www.php.net/date takes you straight to the manual page for the date() function.

Working with date() function:

<?php //functions.php  
/*
  Date() is a built-in php function for showing datas.
  we can pass it many different values/arguments.
  Here some examples:
*/

   echo "<p>". date("D") ."</p>";         //Day name in three letters   => Tue 
   echo "<p>". date("M") ."</p>";         //Month name in three letters => Dec
   echo "<p>". date('l, jS F Y')."</p>"; //Day, date Month Year => Monday, 23 March 1920
?>

Functions to check a variable:

<?php //functions.php  
/*
  Here some examples of functions to check a variable datatype:
*/
   is_int($variable)       //check if is INTEGER, returns a boolean (true/false)
   is_float($variable)    //check if is FLOAT, returns a boolean (true/false)
   is_string($variable)  //check if is STRING, returns a boolean (true/false)

/* isset() allows you to check if a variable has been previously set: */ isset($variable) // returns a boolean (true/false) ?>

 

Using form values:

<?php //functions.php  
/*
  Here some examples of functions to use to handle form data:
*/

   strval(intval($variable))  //STRVAL gets the string value of a variable
   strval(floatval($variable))  //INTVAL and FLOATVAL get the integer/float value of a var
   addslashes($variable)       // Escape single quote with a back slash
   stripslashes($variable)     // Escape back slash from string
?>

 

Using print_r():

<?php //functions.php  
/*
  print_r() displays all the values of an array
*/
   //declaring a multidimensional associative array
   $arr = array("a"=>"dog",
"b"=>"cat",
"c"=>"horse",
"reptiles"=> array("snakes", "lizards", "turtles")
 );

//using <pre> and print_r() to display the content of the array in a well formatted way
echo "<pre>";
print_r($arr);
echo "</pre>";

/*
Array ( [a] => dog [b] => cat [c] => horse [reptiles] => Array ( [0] => snakes [1] => lizards [2] => turtles ) )
*/

?>

 

Using mail() to send an email:

<?php //functions.php  
   //to send an email
mail($to, $subject, $message, $from);
echo "Thank you for sending email"; ?>

All built-in PHP functions can be found at:  http://www.php.net/quickref.php

Click here to read about how to send an email.


Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Copyright © 2014 by Cramerz.com.