Tuesday, April 2, 2019

To understand a line of PHP




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



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


in the code





$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.


Answer



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; */

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...