demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

PHP Concatenation

In PHP it is common to join variables to text and/or HTML tags to produce a complete element: this is referred to as concatenation. The concatenation operator is the period (.) character.

You can concatenate the value of two variables to create a value for a third:

  1. $firstName = “Dudley”;
  2. $lastName = “Storey”;
  3. $fullName = $firstName.$lastName;

In the example above, $fullName contains the value “DudleyStorey”. If you want a space in the value of $fullName, you can concatenate the variables together with a physical space:

  1. $fullName = $firstName.” “.$lastName;

Naturally you can also echo concatenated strings with variables:

  1. echo “Your name is “.$firstName.” “.$lastName;

Note that joining a number with a string creates a string:

  1. $var1 = 23;
  2. $var2 = “skidoo”;
  3. $var3 = $var1.$var2;

The value of $var3 is “23skidoo”.

It is common to “build up” the value of a single variable through concatenation, especially when the individual pieces are fairly long; for example, when composing an email to be sent via PHP.

  1. $test = “This is a long “;
  2. $test .= “string of text pieces ”;
  3. $test .= “all joined together.”;

(Note the use of spaces).

At the end of this process the value of $test will be “This is a long string of text pieces all joined together.”

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.