Functions in php

In a program we have very long code. This code is distributed in number of files, classes and functions. Functions are the set of statements which perform similar task. For example in authentication system, login, register and logout are the functions.

Simple functions

The syntax of simple function is:

  1. <?php
  2. function function_name()
  3. {
  4. //code
  5. }
  6. ?>

Just like variable name, you can give any name to function (in above we have given name as function_name). The code is written inside the curly brackets. This code executes when we call this function (just like built-in functions). We call function by its name followed by brackets.

  1. <?php
  2. // you can call function also here before defining
  3. // defining function with name say_hello
  4. function say_hello()
  5. {
  6. // this fuction is printing hello
  7. echo "hello";
  8. }
  9. // calling the function
  10. say_hello();
  11. ?>

Function with parameters

We can send extra information (like username and password) to the functions using the arguments. For this, we have to define parameters.

NOTE: Arguments are the data which we are passing to function when we call it and parameters are the variables which we define when we write the code for function. We can assume parameters as holes and arguments as data which we are putting in those holes.

  1. <?php
  2. // login function with two parameters
  3. function login($user, $pass)
  4. {
  5. // this fuction is printing username and password
  6. echo "hello, username is $user and password is $pass </br>";
  7. }
  8. // calling the function
  9. login("harish","mypassword");
  10. // calling the function
  11. login("username","yourpassword");
  12. ?>

NOTE:

Parameters are local variables of the function and can only be accessible within the function.

Function with default value

We can give default values to the parameters and such parameters can be escape when we call the function.

  1. <?php
  2. // login function with two parameters
  3. function login($user, $pass, $active = "not active")
  4. {
  5. // this fuction is printing username and password
  6. echo "hello, username is $user and password is $pass and user is $active </br>";
  7. }
  8. // calling the function
  9. login("harish","mypassword");
  10. // calling the function
  11. login("username","yourpassword","active");
  12. ?>

NOTE: If you want to skip a argument in function call then corresponding parameter must be on the last position just like in the above example. Every parameter has null default value (if you have not defined your own), so you can skip last variables any time.

Function with variable parameters

Functions can have variable parameters. In fact we can pass any number of arguments to the function. To access these arguments we have to use following built-in functions:

  1. func_get_args( ): It returns array of all arguments
  2. func_num_args( ): It returns total number of arguments
  3. func_get_arg(argument_number): It returns argument at position argument_number

In this example, we are returning sum of the numbers which are passing to the function as arguments and also printing those arguments.

  1. <?php
  2. function sum()
  3. {
  4. // using func_get_args() to get all argumetns in array
  5. $ary = func_get_args();
  6. print_r($ary);
  7. echo "
  8. ";
  9. $sum = 0;
  10. // using func_num_args to get total number of arguments
  11. for($i = 0; $i < func_num_args(); $i++)
  12. {
  13. // using func_get_arg(index) to get value of argument
  14. echo func_get_arg($i).'
  15. ';
  16. $sum+= func_get_arg($i);
  17. }
  18. echo "sum is $sum
  19. ";
  20. }
  21. sum(1,2,3,4);
  22. sum(23,2343,54,2,1,6);
  23. ?>

OUTPUT:

  1. rray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
  2. 1
  3. 2
  4. 3
  5. 4
  6. sum is 10
  7. Array ( [0] => 23 [1] => 2343 [2] => 54 [3] => 2 [4] => 1 [5] => 6 )
  8. 23
  9. 2343
  10. 54
  11. 2
  12. 1
  13. 6
  14. sum is 2429

Anonymous functions:

These functions are created by using create_function(args_string, code_string);

  1. <?php
  2. // creating anonymous function
  3. $anonymous = create_function('$a,$b', "return \$a + \$b;");
  4. echo $anonymous(2,3);
  5. ?>

It will give output 5.



ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Help Dad unlock new skills this summer in just $10 on Udemy

Development Category (English)300x250

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Best Video Courses