Published on Jul 24,2017
2.2K Views
Email Post

In the previous PHP tutorial post we had discussed how to declare strings and differentiate PHP code in HTML script. This post will guide you on how to pass parameters from one point to other in the PHP script as well as learn the various built-in functions supported in PHP.

Functions is a set of code which performs a particular task. The main advantage of using this is its “reusability”. They can be called in any part of the program to perform that task.

Syntax:

function functionName()
{
set of code to be executed;
}
functionName();

We can also pass arguments/parameters to the functions. They are of two types:

  • Pass By Value

  • Pass By Reference

Pass by Value:

Parameters are nothing but variables. We can pass any number of parameters to functions.

Syntax:

function functionName(parameter1, parameter2)
{
 set of code to be executed;
 }

Example:

<html>
<head>
<title> PHP Function with three Parameters</title>
</head>
<body>
<?php
function addition($value1, $value2,$value3)
{
 $result = $value1+ $value2+$value3
echo "Sum of the three numbers is : $result ";
}
addition (10, 20,30);
?>
</body>
</html>

Output:

Sum of the three numbers is: 60

Here the function name is “addition” and the parameters are $value1, $value2 and $value3. The output was obtained by adding three numbers. To find sum for other number just pass the numbers as parameter to the function. The function will sum those parameters and returns the result.

Pass by Reference:

An argument is nothing but parameters. We can pass reference as arguments to functions. If we pass value as argument, the value of the argument will only be changed. If we pass argument as reference, the original value will be affected. To pass argument as reference, we need to include an ampersand (&) before the argument.

Example:

<?php
function reference_example(&$string)
{
$string .= 'and the string is modified inside pass by reference function.';
}
$str = 'This is the original value, ';
reference_example($str);
echo $str;     
?>

Output:

This is the original value, and the string is modified inside pass by reference function.

Difference Between Passing Arguments by Value and Arguments by Reference:

The argument outside the function gets affected when we pass by reference.  When we pass by value, the argument outside the function does not get affected. The below example shows the difference, where the variable $str get affected in pass by reference. In the below example, the $str value is ‘This is the original value,’ which we assigned outside the function.

Example:

<?php
function reference_example($string)
{
$string .= 'and the string is modified inside pass by reference function.';
}
$str = 'This is the original value, ';
reference_example($str);
echo $str;     
?>

Output:

This is the original value,

Setting Default Values for Function Parameters:

If a function is defined with parameters, then the function is called by passing parameters. We can also set parameters by default. In this case, if we did not pass parameters while calling functions. The default values will get assigned to the parameters.

Examples:

<?php
function setRadius($radius=10)
{
echo "The Radius of the circle is : $radius <br>";
}
setRadius(20);
setRadius();  // will use the default value of 10
?>

Output:

The Radius of the circle is: 20

The Radius of the circle is: 10

Dynamic Function Calls:

We can call functions dynamically by assigning function names to variables.

Example:

<?php
function setRadius($radius=10)
{
 echo "The Radius of the circle is : $radius <br>";
 }
$function_name = “setRadius”;
 $function_name(20);
?>

Output:

The Radius of the circle is: 20

PHP Function Reference:

The real power of PHP is its functions. There are more than 1000 built-in functions available in PHP. The PHP Array functions are used to manipulate with arrays.

PHP Calendar Functions:

  • Using this function we can get different calendar formats.

  • To work with these functions we have to compile PHP with -enable-calendar in installation.

PHP Class/Object Functions:

  • Class/object function allow us to get  information about classes and instance objects.

  • We can get the class name of the object, as well as its member properties and methods.

PHP Character Functions:

  • In PHP 4.3.0 these functions are enabled by default.

  • For older versions we have to compile with PHP -enable-ctype.

PHP Date and Time Functions:

This function allows us to get date and time from the server.

PHP Directory Functions:

  • This function is used to manipulate directories.

  • While configuring PHP we needs to configure with –enable-chroot-func option to enable chroot() function.

PHP Error Handling Functions:

Error handling functions is used to define our own rules for handling errors and also it facilitates to moodily the functions.

PHP File System Functions:

The file system functions are used to manipulate with files.

PHP MySQL Functions:

PHP has built-in MySQL functions which are used to manipulate with MySQL.

PHP Network Functions:

We have several networking functions available in PHP.

PHP ODBC Functions:

ODBC functions allows us to access several database functions.

PHP String Functions:

Given functions are used to manipulate strings.

PHP SimpleXML Functions:

SimpleXML functions provides a tool set to convert XML to an object.

PHP XML Parser Functions:

XML is a data format used to create document that is interchanged on the web.

PHP provides dynamic content (a website that changes frequently) according to type of the browser you are using, and it randomly generates numbers based on the browser type.

Got a question for us? Please mention them in the comments section and we will get back to you.

Related Posts:

Get Started with PHP & MySQL

Differentiating PHP Code in HTML Script

PHP Tutorial: Data Types and Declaration of Variables & Strings in PHP

Share on

Browse Categories

Comments
0 Comments