0

I am working on server side processing datatables. I have this example that I don't understand.

what is this part even doing, why are some of these being called before the actual variable ?

$params = $columns = $totalRecords = $data = array(); 

or this

$where_condition = $sqlTot = $sqlRec = "";

I searched for the answer, but I'll I find is variable variables like $$a

<?php

    require_once("../connections/mysqli_connect.php");

    $params = $columns = $totalRecords = $data = array();

    $params = $_REQUEST;

    $columns = array(
        0 => 'post_id',
        1 => 'post_title', 
        2 => 'post_desc'
    );

    $where_condition = $sqlTot = $sqlRec = "";

    if( !empty($params['search']['value']) ) {
        $where_condition .= " WHERE ";
        $where_condition .= " ( post_title LIKE '%".$params['search']['value']."%' ";    
        $where_condition .= " OR post_desc LIKE '%".$params['search']['value']."%' )";
    }

    $sql_query = " SELECT * FROM li_ajax_post_load ";
    $sqlTot .= $sql_query;
    $sqlRec .= $sql_query;

    if(isset($where_condition) && $where_condition != '') {

        $sqlTot .= $where_condition;
        $sqlRec .= $where_condition;
    }

    $sqlRec .=  " ORDER BY ". $columns[$params['order'][0]['column']]."   ".$params['order'][0]['dir']."  LIMIT ".$params['start']." ,".$params['length']." ";

    $queryTot = mysqli_query($con, $sqlTot) or die("Database Error:". mysqli_error($con));

    $totalRecords = mysqli_num_rows($queryTot);

    $queryRecords = mysqli_query($con, $sqlRec) or die("Error to Get the Post details.");

    while( $row = mysqli_fetch_row($queryRecords) ) { 
        $data[] = $row;
    }   

    $json_data = array(
        "draw"            => intval( $params['draw'] ),   
        "recordsTotal"    => intval( $totalRecords ),  
        "recordsFiltered" => intval($totalRecords),
        "data"            => $data
    );

    echo json_encode($json_data);
5
  • Are you asking specifically about .= or [...] = $something = $something_else? Commented Aug 3, 2018 at 20:55
  • 4
    The variables are just being initialized. It's short hand. In the $params line they are all being set to a empty array. In the $where_condition line they are all being set to noting. Commented Aug 3, 2018 at 20:57
  • Code review is a better place for this question... oh wait, it already exists: codereview.stackexchange.com/questions/94299/… Commented Aug 3, 2018 at 21:05
  • PS: Nothing wrong with doing this, heck I even do it alot when initializing multiple debugging variables to empty. Better to slap em all in one line of code, than a block of ten or so lol :) So YMMV. Commented Aug 3, 2018 at 21:08
  • Thank you all for your help and codereview link. Commented Aug 4, 2018 at 3:06

3 Answers 3

1

This is referred to as multiple assignment or chained assignment.

You can do this in PHP because of two things:

  1. PHP assigns by value
  2. An assignment is an expression that has a value (the assigned value)

So for the expression $params = $columns = $totalRecords = $data = array(); in your example:

  • $data = array() assigns a literal value (an empty array) to $data, but it is also an expression that evaluates to an empty array.

  • $totalRecords = $data = array() assigns the value of the expression $data = array() to $totalRecords, but it is also an expression that evaluates to an empty array.

And so on.

It's important to note that, because all the assignments are done by value, each of the assigned variables have their own value, and none of them are inherently associated each other after the assignment, e.g. appending a value to one of the variables like $data[] = 'something'; does nothing to $totalRecords or any of the other variables that were assigned together.

For future reference, it works this way for scalar types and arrays, but if the assigned value is an object, the effect is different. Each of the variables holds a copy of the identifier of the same object, so all of them do refer to the same object, for example:

$one = $two = $three = new StdClass;
$three->newProperty = 'value';
echo $one->newProperty;  // echoes value
Sign up to request clarification or add additional context in comments.

Comments

0

This means that all variables are being assigned to the same value on the right. It's the same as:

$data = array();
$totalRecords = $data; // which is an empty array (array())
$columns = $totalRecords; // which is an empty array (array())
$params = $columns; // which is an empty array (array())

and

$sqlRec = "";
$sqlTot = $sqlRec; // which is ""
$where_condition = $sqlTot; // which is ""

2 Comments

You made it clear as day for me. Thanks. I can't up vote though so I hope others do.
... but you should already be able to qualify an answer as "accepted answer".
0

The docs says:

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to").

http://php.net/manual/en/language.operators.assignment.php

And

The following table lists the operators in order of precedence, with the highest-precedence ones at the top. Operators on the same line have equal precedence, in which case associativity decides grouping.

right = += -= *= **= /= .= %= &= |= ^= <<= >>= assignment

http://php.net/manual/en/language.operators.precedence.php

Which means that, when using =, the expression is evaluated from the right to the left. So.. this:

$params = $columns = $totalRecords = $data = array(); 

Is the same as

$data = array();
$totalRecords = $data;
$columns = $totalRecords;
$params = $columns;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.