0

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?

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by diafol
0

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!';

Edited by diafol: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.