Jump to content
Larry Ullman's Book Forums

Ch 12 Understanding 'List()'


Recommended Posts

list ($check, $data) = check_login ($dbc, $_POST['email'], $_POST['pass']);
 
Regarding the above.
 
would you be able to go into a bit more detail. Another thing ...
 
PHP.net states  that it is not really a function  :blink:

 

"Like array(), this is not really a function, but a language construct. list() is used to assign a list of variables in one operation." - php.net

Link to comment
Share on other sites

list() is one of those nice functions like array_map() which does a lot of work for you in one step.

 

In the above example, the check_login() function is called and returns an array. list() then assigns the array elements to the 2 variables which list() provides as arguments. Because functions can only return 1 variable (which of course can be an array), this technique is a neat way to call your function and assign a bunch of variables in one step. The longhand version would be

$login_data = check_login ($dbc, $_POST['email'], $_POST['pass']);
$check = $login_data[0];
$data = $login_data[1];
Link to comment
Share on other sites

You need to pass the Database connection resource ($dbc) along for the function to be able to use it. The theory behind this is called scope, and determines where variables are available. The $dbc variable won't exists inside the function check_login unless:

 

1. You pass it as an argument, as done above

2. Declare it global inside the function

 

Passing it as an argument is considered the best practice of the two, as global variables are generally bad. That's another discussion though. Here's how you do that:

function check_login($username, $password)
{
   global $dbc; // Declare variable global. You can use it below now

   // Other stuff here
}
  • Upvote 2
Link to comment
Share on other sites

 Share

×
×
  • Create New...