Quantcast
Channel: WP Woodshed
Viewing all articles
Browse latest Browse all 16

Assigning Multiple Values to a Variable with Concatenation

$
0
0

While working on an email contact form in Treehouse’s PHP development series, I came across a tricky bit of code that really stumped me. Specifically, how to assign multiple values to a single variable  – $email_body – for output in the web browser.

Here’s the code:

$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Message: " . $message;
echo $email_body;

At first, I was confused on how this would work, specifically assigning a value to the same variable in successive instances. I thought, “wouldn’t assigning a new value to the same $email_body variable erase the content assigned in the previous step?”

After looking at it for a few minutes, something clicked. By including the $email_body variable itself in each new assignment, it wasn’t being erased or overwritten at all. It was being added to.

Concatenation works by linking two separate bits of code into one longer chain. This is accomplished by using the concatenation operator (.), also known as a period.

I quickly took a snapshot of the screen with my phone, and ran to the bedroom to try and explain it to my wife (who generally hates this kind of stuff). I figured if I could explain it to her in terms she could understand, then it would help me learn it all the better.

Here’s how I did it, using the pajama pants she was wearing at the time as an example:

Step 1 – $pajama_pants is “” (null)

$pajama_pants = "";

At this point, $pajama_pants is empty (null), i.e. no value has been assigned to it.

Step 2 – $pajama_pants becomes “Blue”

$pajama_pants = ""; 
$pajama_pants = $pajama_pants . "Blue";

$pajama_pants is now assigned the value of itself, ” (which is empty, i.e. no value), plus “Blue” (the color of her pants)

$pajama_pants = "Blue"

Step 3 – $pajama_pants becomes “Blue” plus “Polka Dots”

$pajama_pants = ""; 
$pajama_pants = $pajama_pants . "Blue";
$pajama_pants = $pajama_pants . "Polka Dots";

$pajama_pants is assigned the value of itself, which is now “Blue,” plus the value of “Polka Dots.”

$pajama_pants = "Blue Polka Dots"

Step 4 – $pajama_pants becomes “Blue Polka Dots” plus “Cotton”

$pajama_pants = ""; 
$pajama_pants = $pajama_pants . "Blue";
$pajama_pants = $pajama_pants . "Polka Dots";
$pajama_pants = $pajama_pants . "Cotton";

$pajama_pants is assigned the value of itself, which is now “Blue Polka Dots” plus the value of “Cotton.”

$pajama_pants = "Blue Polka Dots Cotton"

Voila!

Putting it together

The takeaway from this example is that when a variable is assigned the value of itself, e.g. $pajama_pants = $pajama_pants, it’s not really erased, but simply overwritten by including its previous value PLUS whatever else is assigned to it. Like so: $email_body = $email_body . $name

Think of it like hooking the individual links of a chain together to make a longer chain.

Let’s look back at the original example with $email_body

$email_body = ""; // At this point, $email_body is empty, i.e. no value has been assigned to it
$email_body = $email_body . "Name: " . $name . "\n"; // 
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Message: " . $message;
echo $email_body;

With each step, the chain of $email_body grows longer, adding on each additional value and then outputting them with the echo command.


Viewing all articles
Browse latest Browse all 16

Trending Articles