Monday 5 August 2019

PHP Functions with practical examples

PHP has the function capability as available with other programming languages

What is a function?

A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value.Suppose you have to call a piece of code several times in the process,then function will be lot of help to u as you can just create a function of that code and call that function as many times as you wish in the code
PHP itself has lot of built in function in his library which we are already using in the our code

How to define the function in Php?

Function are defined using the word function in php
We can give any name to the function but it should not start with number
All the PHP code for the function should be put inside { and }
General syntax for the function

<?php
function function_name(param1, … , paramN)
{
statement_1;
statement_2;

….;return return_value;
}
?>
<?php
function function_name()
{
execute code;
}
?>
  •  You may or may not provide the parameter to the function.
  • return from function is optional. If you dont want any return value that is ok
Lets us the different example of PHP Functions

PHP Functions with No parameter


Here you can just process any statement in it   and execute it
<?php
function Printsitename()
{echo “My site name is techgoeasy.com”;
}Printsitename() ;
?>
The output will be
My site name is techgoeasy.com

PHP Functions with parameter but no return value

In this case, we can pass the parameter in the function and process the parameter to deliver the required result
<?php
function MutiplyNumbers($n1,$n2)
{
$mul = $n1 * $n2;
echo ‘The multiplication of the number is $mul’;
}MutiplyNumbers(2,3)
?>
The output will be
6

PHP Functions with parameter and return value


In this case , we can pass the parameter and do validate and get the result back in any variable
<?php
function valid_name($name)
{$name = trim($name);if (empty($name))
{
return false; // it was empty
}
$result = ereg(“^[A-Za-z0-9_\-]+$”, $name); //only A-Z, a-z and 0-9 are allowed
if ($result)
{
return true; // ok no invalid chars
} else
{
return false; //invalid chars found
}
return false;
}
if (valid_name(‘TEC$H’))
{
echo ‘Name is good’;
}
else
{
echo ‘Name is bad’;
}
?>
Output will be
Name is bad

PHP Functions with default parameter value

<?php
function Yourweight($weight=50)
{echo ‘Your weight is $weight’;
}Yourweight(60);
Yourweight();
?>
The output will be
60
50


Variable scope with Php Function

The scope of the variable could local,global and static
Local variable are variable defined in function and there scope is limited to the function. They cannot be used outside the function
Two function can use same name of the variable as there scope will be limited to function only
<?php
function test()
{
$x=2;
echo “$x”;
}
test();
//the below statement would generate error as variable x is local to function
echo “$x”;
?>


Global variable are variable which are defined outside function. They can be used only inside the function using global keyword
<?php
$y=2;
function test()
{
$x=2;
// this will generate error global variable is not available to function
echo “$x to $y”;
}test();?>
<?php
$y=2;
function test()
{
$x=2;
global $y
// this will not generate error as global keyword is being used now
echo “$x to $y”;
}
test();
?>

0 comments:

Post a Comment