#native_company# #native_desc#
#native_cta#

Strings & Text in PHP – The ABCs of PHP Part 5 Page 2

By PHP Builder Staff
on April 15, 2009

Variable substitution in a string only works with variables.
To use a function you need to use the string concatenation
operator, which in PHP is a full stop ‘.’
This is different to a lot of languages where a + symbol is
used. PHP still uses a + symbol, but this is only used for
mathematical additions (as we’ll see in the next episode).
In order to produce the expected output above, we would then
use the following:
<?php

  Print "Todays date is " . date('r');

?>
So why use variable substitution at all?
If you remember from our introduction, PHP was originally
designed as a hypertext pre-processor. Back in the early
days, the first idea was to just embed tags into a text
string, and the pre-processor would then replace those tags
with just the information required. So for example you may
have had:
<?php

  Print "Todays date is $date";

?>
And that would have output something similar to:
Todays date is 25/3/09
Today however there are many different ways of achieving
this type of use, and using variable substitution is just a
matter of personal preference.
I often tend to use concatenation, purely because it makes
my code more readable. Bear in mind also that using the
concatenation method also works with single quotes, so:
<?php

  $var = "Hello world";

  Print 'Some text [' . $var . ']';

?>
Works exactly the same as
<?php

  $var = "Hello world";

  Print "Some text [" . $var . "]";

?>