Hi

I understand the basics of concatenate, but then I saw this example and I am now confused!

$Update = "Update people set " .            
            "permission_id = '" . $permission_id . "', " .            
            "username = '" . $username . "', " .
			"forename = '" . $forename . "', " .
			"surname = '" . $surname . "', " .
			"address1 = '" . $address1 . "', " .
	  	    "address2 = '" . $address2 . "', " .
			"town = '" . $town . "', " .
  		    "postcode = '" . $postcode . "' where id = " . $id;

Please can someone help with explaining it?

Recommended Answers

All 2 Replies

Member Avatar for diafol

That's a bad example. No need to concatenate there as simple vars are read inside double quotes.

$Update = 
   "UPDATE 
      `people` 
    SET 
      `permission_id` = '$permission_id',
      `username` = '$username',
      `forename` = '$forename', 
      `surname` = '$surname', 
      `address1` = '$address1', 
      `address2` = '$address2', 
      `town` = '$town',
      `postcode` = '$postcode' 
    WHERE 
      `id` = $id";

Would work too. I'd give the resource where you found that example a wide berth.

Concatenation is quite straightforward, but try to avoid it where possible as it's slow - use double quotes and place var inside.
IN addition, array vars need to be braced {...}:

echo "hello this is {$myarray['name']}";

I see a lot of code where double quoted strings are concatenated when a quote occurs in the string itself. No need, you can just escape the quote:

echo "The guy said, \"Hello, I'm {$myarray['name']}.\" I replied \"I'm $feeling_now.\"";

I find it useful for when I want to place a value from a function into a string:

echo 'Today\'s date is ' . date('d-m-Y') . ' and what a fine day it is!';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.