demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter.

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

You must be signed up in order to leave comments.

web developer guide

featured comment

by Aisling Brock in New Business Card Design

what i'm reading

A Feast for Crows: A Song of Ice and Fire: Book Four
A Feast for Crows: A Song of Ice and Fire: Book Four

what i'm watching

Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]
Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]

what i'm playing

Borderlands
Borderlands

what i'm hearing

Planets
Planets

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.