Home » Web development » PHP » PHP Functions with practical examples

PHP Functions with practical examples

PHP has lot of in-built Functions for use. PHP also provides opportunity to create use defined function also as available with other programming languages. We will discussing here the various type of User defined PHP Functions

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 User Defined 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
See also  What is HTML5

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
The multiplication of the number is 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

In this case if we don’t pass the parameter default value is picked up

<?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 be 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.Lets see few example to explain this

<?php
$y=2;
function test()
{
$x=2;
// this will generate error global variable is not available to function
echo “$x to $y”;
}
test();
?>

The above function will generate error global variable are not available to function like this

<?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();
?>

The above will not generate any error as we are using the global variable in the function by first defining the global variable correctly in function

See also  PHP echo construct

Hope you like post on PHP Functions

Also Reads
How to run php file in windows : This post talks about how we can php file on the the window and do the testing before moving to the web server
uses of PHP : This has detailed description why PHP is so much used through out the world.
first php program : This is good starting point for anybody starting with PHP and talk in good detail about how to write your PHP program and execute it
Define constant in PHP
PHP String Functions
PHP Array functions
https://en.wikipedia.org/wiki/PHP

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top