PHP: ".=" Operator meaning?

Associate
Joined
17 Jan 2005
Posts
632
Location
London, UK
Hey guys,

Just a quick question, I was wondering if someone could tell me that the ".=" operator does?

I guess it results in $a = $a . $b

But what does . mean? :p

Thanks,
King :)
 
cf. +=, -=, /=, *=, %=

Every operator can be put before an assignment operator in this way.

For example,

Code:
$foo += 5;

is the same as:

Code:
$foo = $foo + 5;

and

Code:
$foo /= 5;

is the same as:

Code:
$foo = $foo / 5;
 
I use it to break up a long query so I can see where I've made a mess more clearly...

Code:
$query = "SELECT column_1, column_2 ";
$query .= "FROM table_1 ";
$query .= "WHERE column_id='$string' ";
$query .= "ORDER BY column_1 DESC ";
$query .= "LIMIT 1";
 
If you're going to use concatenation then it's better do do:

Code:
$query =  "SELECT FOO "
             ."FROM bar";

But it's better still to not use concatenation at all:

Code:
$query = "
SELECT foo
FROM bar
";
 
paulsheff said:
I use it to break up a long query so I can see where I've made a mess more clearly...

Code:
$query = "SELECT column_1, column_2 ";
$query .= "FROM table_1 ";
$query .= "WHERE column_id='$string' ";
$query .= "ORDER BY column_1 DESC ";
$query .= "LIMIT 1";
Code:
$query = sprintf("
    SELECT column_1, column_2
    FROM table_1
    WHERE column_id = '%s' AND column_somethingelse = '%s'
    ORDER BY column_1 DESC
    LIMIT 1",
    $string,
    $somethingelse
);

Is how I tend to do complex queries, although I'll often keep the SQL string on a single line, as per:
Code:
$query = sprintf("SELECT column_1, column_2 FROM table_1 WHERE column_id = '%s' AND column_se = '%s' ORDER BY column_1 DESC LIMIT 1",
    $string,
    $se
);

Simply because I find it easier to read an SQL statement on a single line rather than broken up because of the way I visualise the query in my mind.
 
I tend to use a DB class and have:
Code:
$db->parseSelectQuery($fieldsToSelect, $from, $criteria);
but meh :p

but for extended queries I just one line it and for joins/where/and/or start a new line..

Code:
$query = "SELECT `table1`.`column` FROM `table1`
INNER JOIN `table2` ON `table1`.`column` = `table2`.`column` 
WHERE `column` = 'value'";
 
Last edited:
Back
Top Bottom