Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

The ABC's of PHP Part 5 - Strings & Text
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 . "]";

?>
[ Next Page ]

[Page 1]  [Page 2]  


Comments:

No Messages Found

You can post questions/corrections/feedback here

 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.