2

What does the following line mean, particularly the operator .=?

$query .= "UPDATE authors SET author=LOWER(author) WHERE id=2;";

in the code

<?php

$conn = pg_pconnect("dbname=publisher");

// these statements will be executed as one transaction

$query = "UPDATE authors SET author=UPPER(author) WHERE id=1;";
$query .= "UPDATE authors SET author=LOWER(author) WHERE id=2;";

pg_query($conn, $query);

?>

It seems to make some sort of array such that the last command processes first the first query and then the second.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

6 Answers6

10

This is the concatenate assignment operator. It will concatenate or add to the end of the string. So:

$a = "Hi!";

$a .= " I";
$a .= " love";
$a .= " StackOverflow";
$a .= " a";
$a .= " lot";

echo $a; // echos "Hi! I love StackOverflow a lot"

In your case

$query = "UPDATE authors SET author=UPPER(author) WHERE id=1;";
$query .= "UPDATE authors SET author=LOWER(author) WHERE id=2;";
echo $query; 
/* echos "UPDATE authors SET author=UPPER(author) WHERE id=1; UPDATE authors SET author=LOWER(author) WHERE id=2; */
Andrew G. Johnson
  • 26,603
  • 30
  • 91
  • 135
Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
3

It means $query = $query . "UPDATE authors SET author=LOWER(author) WHERE id=2;";

So it appends the String to the Query Variable.

Henrik P. Hessel
  • 36,243
  • 17
  • 80
  • 100
2

Your question is about the operator .=. It is a shorthand to a string concatenation followed by an assignment.

On assigment by operation operators

There is a family of operators we can call assignment by xyz, where xyz here represents a binary operation on operands of the same type, such as addition, subtraction, concatenation.

So, let's say we have an operator ⊕: int*intint, meaning that it takes a pair of ints and produces another one:

⊕(a, b) = a ⊕ b

Let's say we want to calculate ab and store the results on the variable a. We can do so by:

a = a ⊕ b

But we do this so often when coding that an operator was created to represent the line above. You should take it as a single operation that does both the ⊕ operation and the assignment ( = ) with a single call:

a ⊕= b ⇔ a = a ⊕ b.

Some examples

So, in your case, you have a .= operator. Now that you know about assignment by operation operators, you can guess that:

$query = "Hello, "
$query .= "World!";

is the same as:

$query = "Hello, "
$query = $query . "World!";

See?

Now, another frequent use of this kind operators are the += and -= versions.

However, abuse of this kinds of operators may lead to less readable code (especially when dealing with "low level" operators acting on bits, for example).

Bruno Reis
  • 37,201
  • 11
  • 119
  • 156
1

.= simply means "append". This

$query = "UPDATE authors SET author=UPPER(author) WHERE id=1;";
$query .= "UPDATE authors SET author=LOWER(author) WHERE id=2;";

…results in

$query == "UPDATE authors SET author=UPPER(author) WHERE id=1;UPDATE authors SET author=LOWER(author) WHERE id=2;"
s4y
  • 50,525
  • 12
  • 70
  • 98
0

Concatenates the string... so $query becomes:

"UPDATE authors SET author=UPPER(author) WHERE id=1;UPDATE authors SET author=LOWER(author) WHERE id=2;"
talha2k
  • 24,937
  • 4
  • 62
  • 81
jsight
  • 27,819
  • 25
  • 107
  • 140
0

it separates the updates with ; and executes both of them

Galen
  • 29,976
  • 9
  • 71
  • 89