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 or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my projects

A Sass color keyword system for designers. Replaces CSS defaults with improved hues and more memorable, relevant color names.

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

PHP Concatenation

php / introduction

Estimated reading time: 1 minute

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:

$firstName = "Dudley";
$lastName = "Storey";
$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:

$fullName = $firstName." ".$lastName;

Naturally you can also echo concatenated strings with variables:

echo "Your name is ".$firstName." ".$lastName;

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

$var1 = 23;
$var2 = "skidoo";
$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.

$test = "This is a long ";
$test .= "string of text pieces ";
$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.”

comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.