Friday, 6 November 2015

PHP 5 Functions

02:05

PHP  BUILT IN  Functions example basename(path,suffix) :

The basename() function returns the filename from a path.

Syntax

basename(path,suffix)

ParameterDescription
pathRequired. Specifies the path to check
suffixOptional. Specifies a file extension. If the filename has this file extension, the file extension will not show

Example

<?php
$path = "/testweb/home.php";

//Show filename with file extension
echo basename($path) ."<br/>";

//Show filename without file extension
echo basename($path,".php");
?>
The output of the code above will be:
home.php
home
The real power of PHP comes from its functions; it has more than 1000 built-in functions.

PHP User Defined Functions

Besides the built-in PHP functions, we can create our own functions.
A function is a block of statements that can be used repeatedly in a program.
A function will not execute immediately when a page loads.
A function will be executed by a call to the function.

Create a User Defined Function in PHP

A user defined function declaration starts with the word "function":

Syntax

function functionName() {
    code to be executed;
}

In the example below, we create a function named "writeMsg()". The opening curly brace ( { ) indicates the beginning of the function code and the closing curly brace ( } ) indicates the end of the function. The function outputs "Hello world!". To call the function, just write its name:

Example


<?php

function writeMsg()
 {
    echo "Hello
 world!";
}

writeMsg(); // call the function

?>

Output:
Hello world!

PHP Function Arguments:

Information can be passed to functions through arguments. An argument is just like a variable.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just seperate them with a comma.
The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name:

Example


<?php
function familyName($fname) {
    echo "$fname Refsnes.<br>";
}

familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
Output:
Jani Refsnes.
Hege Refsnes.
Stale Refsnes.
Kai Jim Refsnes.
Borge Refsnes.

The following example has a function with two arguments ($fname and $year):

Example


<?php
function familyName($fname, $year) {
    echo "$fname Refsnes. Born in $year <br>";
}

familyName("Hege", "1975");

familyName("Stale", "1978");

familyName("Kai Jim", "1983");

?>
Output:
Hege Refsnes. Born in 1975 
Stale Refsnes. Born in 1978 
Kai Jim Refsnes. Born in 1983 

PHP Functions - Returning values

To let a function return a value, use the return statement: 

Example

<?php
function sum($x, $y) {
    $z = $x + $y;
    return $z;
}

echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>

Output:
5 + 10 = 15
7 + 13 = 20
2 + 4 = 6






PASS  BY  VALUE  &  PASS BY REFERENCE :


By definition, pass by value  means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only "using" the parameter for some computation, not changing it for the client program.

In pass by reference (also called pass by address), a copy of the address of the actual parameter is stored. Use pass by reference when you are changing the parameter passed in by the client program.

suppose you wanted a function to square the value in $value . you might try that by passing $value to a function  named say, squarer,which squares the number passed to it:

function  squarer($number)
{

$number *=$number

}

however, that 's  not going to work,because the arguments passed to the squarer function are passed by value.

you can change that by prefacing the argument you want  passed by reference with an ampersand (&)-which will make PHP pass that argument by reference..when you pass an argument by reference ,that gives the code in the function direct access to that argument  back in the calling code ,so to square the value in $value,all you have to do is to preface the argument with & in the argument list like this in following examples:

Pass by reference :

--------------------------------------------------------------------------------------------------------------------------

<?php
$value=4;
echo "Before the call.\$value holds $value <br>";
squarer($value);
squarer($value);




echo "After the call, \$value holds $value <br>";
function  squarer(&$number)
{

$number *=$number;  //$number=$number*$number

}
?>

 output:

Before the call.$value holds 4
After the call, $value holds 256


Pass by value:
--------------------------------------------------------------------------------------------------------------------------

<?php
$value=4;
echo "Before the call.\$value holds $value <br>";
squarer($value);
squarer($value);




echo "After the call, \$value holds $value <br>";
function  squarer($number)
{

$number *=$number;  //$number=$number*$number

}
?>
output:

Before the call.$value holds 4
After the call, $value holds 4 

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

1 comments:

  1. Nice blog post. Very useful for learners. PHP functions is very important for learning PHP. PHP Training Bangalore

    ReplyDelete

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top