Skip to content
Home » Blog » How to pass values in a function

How to pass values in a function

You can pass values in the functions you can pass single value and you can pass an array. In this post I will explain how values are passed in function.

class Base
{

    function CheckUsers($userid) //passing the values
    {
        if($userid==2)
        {
          // code...
        }
        else {
          // code...
        }
        

    }
}

On the other file you need to call this function like this:

include "class/class_lib.php";
$ba= new Base();
$ba->CheckUsers(2);

How to pass array in the function:

This is how you need to pass the array in the function. For this first we will see the file coding which calls the function.

include "class/class_lib.php";
$ba= new Base();
//Initializing the array
$user=[]; 
$user['Name']='John';
$user['UserID']='2';
$user['Email']='john@doe.com';
//Passing the array in the function
$ba->CheckUsers($user); 

Here is how you can use the array data in the function

class Base
{
    function CheckUsers($data)
    {
      echo $data['Name']; //Calling the array values 
      echo $data['UserID'];
      echo $data['Email'];
    }
}

See you in the more advanced topics…

Leave a Reply

Your email address will not be published. Required fields are marked *