PHP Functions

PHP function is a piece of code that can be reused many times. It can take input as argument list and return value. There are thousands of built-in functions in PHP.

In PHP, we can define Conditional functionFunction within Function and Recursive function also.


Advantage of PHP Functions

Code Reusability: PHP functions are defined only once and can be invoked many times, like in other programming languages.

Less Code: It saves a lot of code because you don’t need to write the logic many times. By the use of function, you can write the logic only once and reuse it.

Easy to understand: PHP functions separate the programming logic. So it is easier to understand the flow of the application because every logic is divided in the form of functions.


PHP User-defined Functions

We can declare and call user-defined functions easily. Let’s see the syntax to declare user-defined functions.

Syntax

  

function functionname(){  
//code to be executed  
}

Note: Function name must be start with letter and underscore only like other labels in PHP. It can’t be start with numbers or special symbols.

PHP Functions Example

<?php  
function sayHello(){  
echo "Hello PHP Function";  
}  
sayHello();//calling function  
?>  

Output:Hello PHP Function

PHP Function Arguments

We can pass the information in PHP function through arguments which is separated by comma.

PHP supports Call by Value (default), Call by ReferenceDefault argument values and Variable-length argument list.

  

<?php  
function sayHello($name){  
echo "Hello $name<br/>";  
}  
sayHello("Sonoo");  
sayHello("Vimal");  
sayHello("John");  
?>

Output:

Hello Sonoo
Hello Vimal
Hello John
<?php  
function sayHello($name,$age){  
echo "Hello $name, you are $age years old<br/>";  
}  
sayHello("Sonoo",27);  
sayHello("Vimal",29);  
sayHello("John",23);  
?>  

Output:

Hello Sonoo, you are 27 years old
Hello Vimal, you are 29 years old
Hello John, you are 23 years old

PHP Call By Reference

Value passed to the function doesn’t modify the actual value by default (call by value). But we can do so by passing value as a reference.

By default, value passed to the function is call by value. To pass value as a reference, you need to use ampersand (&) symbol before the argument name.

<?php  
function adder(&$str2)  
{  
    $str2 .= 'Call By Reference';  
}  
$str = 'Hello ';  
adder($str);  
echo $str;  
?>  

Output:

Hello Call By Reference

PHP Function: Default Argument Value

We can specify a default argument value in function. While calling PHP function if you don’t specify any argument, it will take the default argument. Let’s see a simple example of using default argument value in PHP function.File: functiondefaultarg.php

<?php  
function sayHello($name="Sonoo"){  
echo "Hello $name<br/>";  
}  
sayHello("Rajesh");  
sayHello();//passing no value  
sayHello("John");  
?>  

Output:

Hello Rajesh
Hello Sonoo
Hello John

PHP Function: Returning Value

Let’s see an example of PHP function that returns value.File: functiondefaultarg.php

<?php  
function cube($n){  
return $n*$n*$n;  
}  
echo "Cube of 3 is: ".cube(3);  
?>  

Output:

Cube of 3 is: 27

Install PHP

To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software stack. It is available for all operating systems. There are many AMP options available in the market that are given below:

  • WAMP for Windows
  • LAMP for Linux
  • MAMP for Mac
  • SAMP for Solaris
  • FAMP for FreeBSD
  • XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some other components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.

If you are on Windows and don’t want Perl and other features of XAMPP, you should go for WAMP. In a similar way, you may use LAMP for Linux and MAMP for Macintosh.

Download and Install WAMP Server

Click me to download WAMP server

Download and Install LAMP Server

Click me to download LAMP server

Download and Install MAMP Server

Click me to download MAMP server

Download and Install XAMPP Server

Click me to download XAMPP server

How to install XAMPP server on windows

We will learn how to install the XAMPP server on windows platform step by step. Follow the below steps and install the XAMPP server on your system.

Step 1: Click on the above link provided to download the XAMPP server according to your window requirement.

Install PHP

Step 2: After downloading XAMPP, double click on the downloaded file and allow XAMPP to make changes in your system. A window will pop-up, where you have to click on the Next button.

Install PHP

Step 3: Here, select the components, which you want to install and click Next.

Install PHP

Step 4: Choose a folder where you want to install the XAMPP in your system and click Next.

Install PHP

Step 5: Click Next and move ahead.

Install PHP

Step 6: XAMPP is ready to install, so click on the Next button and install the XAMPP.

Install PHP

Step 7: A finish window will display after successful installation. Click on the Finish button.

Install PHP

Step 8: Choose your preferred language.

Install PHP

Step 9: XAMPP is ready to use. Start the Apache server and MySQL and run the php program on the localhost.

How to run PHP programs on XAMPP, see in the next tutorial.

Install PHP

Step 10: If no error is shown, then XAMPP is running successfully.

Install PHP