Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Functions in php

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 86
    Comment on it

    A function is a group of statements that does a special task. the function can be called repeatedly to execute the same statements.

    The syntax of the user defined functions in php is as :

    function functionName() {
        code to be executed;
    } 
    

    Here the function is the keyword for functions and the functionName is the name of the function the name can start with the underscore also.

    The function definition can have the arguments, the above syntax doesn't have. lets see the syntax of the argumented functions,

    function functionName($variable) {
          code to be executed;
    } 
    

    In the above the $variable is the argument of the function whose value is passed at the time of the function call.

    lets see examples of each :

    <?php
              function func() {
                      echo "Hello!";
              } 
    func();
    ?>
    

    In the above we have called the function func() but haven't passed any value.
    Now,

     <?php
                  function func($num1,$num2) {
                       $sum = $num1 + $num2;
                       echo "Sum of the two numbers is : $sum";
                  } 
        func(1,2);
        ?>
    

    in the above we have called the function func() and passed the values (1,2) to the function func for the addition.

    The functions can also return value this can be done with the return statement this as follows :

    <?php
                      function func($num1,$num2) {
                           $sum = $num1 + $num2;
                       return $sum;
                      } 
    
           echo "Sum of the two numbers is : func(1,2)";
      ?>
    

    In the above code the function returns the value of the addition stored in the variable $sum. the function call is made in the echo statement which prints the value of the value returned by the function.

    In this manners the user defined functions can be used in php.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: