Pass data to a function

  • Function Arguments

    Let us define some variable and a function for finding student's grade

    
                      // define the variables
                      $name="manoj";
                      $mark=50;
                     
                      //define a function
                      function findGrade(){
                            if($mark>40){
                                  echo "Passed";
                            }
                            else{
                                  echo "Failed";
    
                            }
    
                      }
    
                     // call the function
                     findGrade();
    
                      
    When executing the above code, we will get error 'undefined variable $mark'. It means that the variable defined outside of a function is not avaialble in a function SOLUTION:
    We can pass the variables defined outside of a function into a function as its arguments

    data can be passed to functions through arguments. An argument is just like a variable.


    Syntax
    
                        <?php 
                        function function_name($first_parameter, $second_parameter) {
                              executable code;
                        }
                        ?>
    
                        
    $first_parameter & $second_parameter are function parameters
    Example:
    
                        <?php 
                        // function along with three parameters
                        function findTotal($num1, $num2, $num3)
                        {
                              $product = $num1 * $num2 * $num3;
                              echo "The product is $product";
                        }
    
    
    
                        // Calling the function
                        // Passing three arguments
                        findTotal(2, 3, 5);
                        ?>
    
                        
  • Problem1 : Write a program to check student grade based on marks

    Let us find the grade of 3 students

    Conditions:
    If marks are 60% or more, grade will be First Division.
    If marks between 45% to 59%, grade will be Second Division.
    If marks between 33% to 44%, grade will be Third Division.
    If marks are less than 33%, student will be Fail.

    1. Variables

    
                  $student1 =["name" => "manoj", "mark" => 90];  
                  $student2 =["name" => "anu",  "mark" => 60];   
                  $student3 =["name" => "hari", "mark" => 80];  
                  

    2. find the grade of student1

    
                      // find grade of student1
    
                      $marks=$student1['mark'];
                      if ($marks>=60)
                      {
                            $grade = "First Division";
                      }
                      else if($marks>=45)
                      {
                            $grade = "Second Division";
                      }
                      else if($marks>=33)
                      {
                            $grade = "Third Division";
                      }
                      else
                      {
                            $grade = "Fail";
                      }
    
                      echo "$student1['name'] grade: $grade";
    
    
                
                  

    3. find the grade of student2

    let us copy and use the above code

    
                      // find grade of student2
    
                      $marks=$student2['mark'];
                      if ($marks>=60)
                      {
                            $grade = "First Division";
                      }
                      else if($marks>=45)
                      {
                            $grade = "Second Division";
                      }
                      else if($marks>=33)
                      {
                            $grade = "Third Division";
                      }
                      else
                      {
                            $grade = "Fail";
                      }
    
                      echo "$student2['name'] grade: $grade";
    
    
                
                  

    4. find the grade of student3

    let us copy and use the above code

    
                      // find grade of student3
    
                      $marks=$student3['mark'];
                      if ($marks>=60)
                      {
                            $grade = "First Division";
                      }
                      else if($marks>=45)
                      {
                            $grade = "Second Division";
                      }
                      else if($marks>=33)
                      {
                            $grade = "Third Division";
                      }
                      else
                      {
                            $grade = "Fail";
                      }
    
                      echo "$student3['name'] grade: $grade";
    
    
                
                  

    5. Show the grade user friendly

    
                      <table class="table table-bordered">
                                  <tr>
                                         <td>
                                         <?php 
                                                    echo $student1['name'];
                                              ?>                                          
                                         </td>
                                         <td>
                                         <?php 
                                                     $marks=$student1['mark'];
                                                     if ($marks>=60)
                                                     {
                                                           $grade = "First Division";
                                                     }
                                                     else if($marks>=45)
                                                     {
                                                           $grade = "Second Division";
                                                     }
                                                     else if($marks>=33)
                                                     {
                                                           $grade = "Third Division";
                                                     }
                                                     else
                                                     {
                                                           $grade = "Fail";
                                                     }
                                   
                                                     echo $grade;
                                              ?>                                          
                                         </td>
                                   </tr>
    
                                   <tr>
                                         <td>
                                         <?php                                                echo $student2['name'];
                                              ?>                                          
                                         </td>
                                         <td>
                                         <?php                                                 $marks=$student2['mark'];
                                                     if ($marks>=60)
                                                     {
                                                           $grade = "First Division";
                                                     }
                                                     else if($marks>=45)
                                                     {
                                                           $grade = "Second Division";
                                                     }
                                                     else if($marks>=33)
                                                     {
                                                           $grade = "Third Division";
                                                     }
                                                     else
                                                     {
                                                           $grade = "Fail";
                                                     }
                                   
                                                     echo $grade;
                                              ?>                                          
                                         </td>
                                   </tr>
    
    
                                   <tr>
                                         <td>
                                         <?php                                                echo $student3['name'];
                                              ?>                                          
                                         </td>
                                         <td>
                                         <?php 
                                                     $marks=$student3['mark'];
                                                     if ($marks>=60)
                                                     {
                                                           $grade = "First Division";
                                                     }
                                                     else if($marks>=45)
                                                     {
                                                           $grade = "Second Division";
                                                     }
                                                     else if($marks>=33)
                                                     {
                                                           $grade = "Third Division";
                                                     }
                                                     else
                                                     {
                                                           $grade = "Fail";
                                                     }
                                   
                                                     echo $grade;
                                              ?>                                          
                                         </td>
                                   </tr>
    
                       </table>
    
                

    1. The above logic is not good. The same code is duplicated or copied for each students. Here, code debugging or maintains is not easy when there will be a bug fixing or code changing. We must change one by one in all places whereever we used the same logic

    2. don't use php logical code inside or between html tags

  • Better Solution : Implement the above code using function for avoiding duplicated code

    Define a function

    . Let us define a function for finding grade and pass the mark to the function as parameter

    
                           function findGrade($marks){
    
                                  if ($marks>=60)
                                  {
                                        $grade = "First Division";
                                  }
                                  else if($marks>=45)
                                  {
                                        $grade = "Second Division";
                                  }
                                  else if($marks>=33)
                                  {
                                        $grade = "Third Division";
                                  }
                                  else
                                  {
                                        $grade = "Fail";
                                  }
    
                                  return $grade;
                                  
                           } 
                  

    Function call and data passing

    
                      <table class="table table-bordered">
                                  <tr>
                                         <td>
                                         <?php 
                                                    echo $student1['name'];
                                              ?>                                          
                                         </td>
                                         <td>
                                         <?php                                                
                                                     findGrade($student1['mark']);
                                              ?>                                          
                                         </td>
                                   </tr>
    
                                   <tr>
                                         <td>
                                         <?php                                                echo $student2['name'];
                                              ?>                                          
                                         </td>
                                         <td>
                                         <?php                                                 findGrade($student2['mark']);
                                              ?>                                          
                                         </td>
                                   </tr>
    
    
                                   <tr>
                                         <td>
                                         <?php                                                echo $student3['name'];
                                              ?>                                          
                                         </td>
                                         <td>
                                         <?php                                                 findGrade($student3['mark']);
                                              ?>                                          
                                         </td>
                                   </tr>
    
                       </table>
    
                
    Code debugging and maintanins are easy. If anything or any line of code needs to be changed, we can easily change it inside the function and the change will be reflected everywhere, where the function is called