函数数组定义?

问题描述:

I have a slight gap in my understanding of how functions and arrays work together

I have this function which gets the login info from a form named submittedlogin then performs a simple query and fetches the array if the, but only if the values arent empty and returns errors if there are (truncated that part)

function check_login($dbc, $email = '', $pass = '') {

$q = "SELECT user_id, user_type, first_name, time_zone FROM users WHERE email='$e' AND pass=SHA1('$p')";

Then on the actual login page I call the function with the posted values

if (isset($_POST['submittedlogin'])) {

    require_once ('includes/login_functions.inc.php');
    require_once ('../mysqli_connect.php');
    list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']);

    if ($check) { // OK!

        // Set the session data:.
        session_start();
        $_SESSION['user_id'] = $data['user_id'];
        $_SESSION['first_name'] = $data['first_name'];
        // Get priviledges
        $_SESSION['user_type'] = $data['user_type'];
        // Store the HTTP_USER_AGENT:
        $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
        //

Note that the $_SESSION['user_type'] = $data['user_type']; does return the correct value.

My question would be how is how does the syntax work here? :

list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']);

Better phrasing would probably be, how are the $check and $data variables defined?? Does:

$check = check_login($dbc) and $data = $q??

Clarification would be much appreciated.

我对函数和数组如何协同工作的理解略有差距 p> 我有这个函数从一个名为submittedlogin的表单中获取登录信息,然后执行一个简单的查询并获取数组,如果是,但只有当值不为空时才返回错误,如果有(截断那部分)则返回错误 p>

  function check_login($ dbc,$ email ='',$ pass =''){
 
 $ q =“SELECT user_id,user_type,first_name,time_zone FROM users WHERE email =  '$ e'AND传递= SHA1('$ p')“; 
  code>  pre> 
 
 

然后在实际的登录页面上,我调用带有发布值的函数 p >

  if(isset($ _ POST ['submittedlogin'])){
 
 require_once('includes / login_functions.inc.php'); 
 require_once('../  mysqli_connect.php'); 
 list($ check,$ data)= check_login($ dbc,$ _POST ['email'],$ _POST ['pass']); 
 
 if($ check){/  / OK!
 
 //设置会话数据:。
 session_start(); 
 $ _SESSION ['user_  id'] = $ data ['user_id']; 
 $ _SESSION ['first_name'] = $ data ['first_name']; 
 //获取权限
 $ _SESSION ['user_type'] = $ data ['  user_type']; 
 //存储HTTP_USER_AGENT:
 $ _SESSION ['agent'] = md5($ _ SERVER ['HTTP_USER_AGENT']); 
 // 
  code>  pre> 
 \  n 

请注意, $ _ SESSION ['user_type'] = $ data ['user_type']; code>会返回正确的值。 p>

我的问题是 请问这里的语法是如何工作的? : p>

list($ check,$ data)= check_login($ dbc,$ _POST ['email'],$ _POST ['pass']); code> p>

更好的措辞可能是,如何定义$ check和$ data变量? 是: p>

$ check = check_login($ dbc)和 $ data = $ q ?? p>

非常感谢澄清。 p> div>

list($val1, $val2) = array('val1', 'val2');

now $val1 is 'val1'; and $val2 is 'val2'

list($val1, $val2) = array('val1', 'val2', 'val3', 'val4');

we've got 2 first values from array (nothing changed to $val1 and $val2).

So, if you do something like

$sql = 'SELECT if(md5(\''.$Password.'\') = password,1,0), username, email FROM users WHERE username = \''.$username.'\'';
$res = mysql_query($sql, $connection);
list($isAuthorized, $username, $email) = mysql_fetch_array($res);

You'd get array of 3 element returned by mysql_fetch_array and pass them into $isAuthorized, $username and $email variables.

Im not sure you can use the list trick with the return from a function but i could be mistaken... Regardless in order to do that you would ned to return an array from function. the vars you give to list are the array elements on the right side of the equation so :

list($check, $data) = array('one', 'two');
echo $check; // outputs one
echo $data; // outputs two

the function check_login returns an array like this:

array('user', 'type', 'first', 'time');

and by

list ($check, $data) =  array('user', 'type', 'first', 'time');

$check and $data gets equal to the first two values from the array

it means the function check_login() return an array having two values and each will be assigned to the variable in the list()

let u get return value from check_login() is

array('yes','hello');

and using list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']);

means

$check = 'yes';
$data =''hello';

Reference