In the last part of this series we looked at what a variable
was in general. Today we'll be covering strings and text.
We'll look at the contents those variables would typically
hold.
What is a string?
Put simply a string is any sequence of alpha/numeric or
punctuation characters enclosed by either a set of ""
parentheses or a set of '' parentheses.
Strings are generally used to hold data that we as humans
may write down in a sentence or read in a document such as
this one, and generally we would not normally perform
mathematical calculations on a string. This is not to say
however that a string cannot hold a number value, it can.
Under normal circumstances however, you would not unless
that number was purely for display.
You'll notice also that there are 2 sets of parentheses used
to enclose strings mentioned above, each set operates
differently to the other.
Using '' parentheses
When you enclose a PHP string using '' (Otherwise know as
single quotation marks) exactly what you put in the string
is displayed. This means that no embedded variables are
translated and no special sequences such as carriage returns
are obeyed.
Let me show you an example:
<?php
$variable = 'Hello world';
Print 'Here is some text [$variable]';
?>
When this is run from the command line the following will be output:
Here is some text [$variable]
Given what we've discussed so far, you might think that this
is to be expected, and in a way you would be right, however
things are rather different when using the alternate
double quote parentheses.
Using "" parentheses
So what's so different about using "" (Otherwise known as double quotation marks), well when we use "" PHP actually interprets the contents of the string. In the case of embedded variable names, these are substituted for the actual contents of the variable, in the case of special escape sequences these are interpreted and replaced with different values as required (More on those in just a moment).
Using the same example as before, let's rewrite it with double quotes instead of single quotes.
<?php
$variable = "Hello world";
print "Here is some text [$variable]";
?>
This will now output:
Here is some text [Hello world]
PHP's ability to substitute variables directly in a sctring
like that is one of it's strongest features, and sometimes a
source of frustration to the beginner.
Many PHP beginners assume that because they can insert
variables using double quotes, they can also do the same
using PHP's built in functions, unfortunately this will not
work as expected. EG:
A user trying to use:
<?php
Print "Todays date is date('r')";
?>
May rightfully assume that the result would be:
Todays date is Wed, 25 Mar 2009 13:32:21 +0000
When in actual fact the result will be:
Todays date is date('r')
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 . "]";
?>
Some of the purists out there will also have you believe
that using "" is significantly slower than using '' and yes
in a way, there are grounds for this argument because of the
extra work that has to be done for strings using "", in
reality however I've tested this theory a number of times,
and found that it's dependent on the speed of the server
and/or PC your running on, and in most cases the difference
is so small that it's not really worth the argument. At the
end of the day however, again it is a personal decision as
to what method you use.
So what about these special escape sequences you mentioned?
Escape sequences are one or more characters in sequence
preceded by a \ symbol, usually there is only one character
following the \ the most common of which are as follows:
- \n Linefeed
- \r Carriage return
- \t Tab
- \\ An actual \ symbol
- \" Double quotes
- \' Single quotes
In most cases when using a line feed, you can just use \n
under Windows or Linux/Unix, however if you are writing
files on the 2 platforms you may need to watch the
differences.
Under windows, a normal end of line is actually \r\n, where
as under linux it just a \n
What problem does this cause? Well if your creating a text file, then using just a
\n under Windows will mean your file will end up like this:
Under Linux/Unix your text file will end up like this
Line1
Line2
If however you use \r\n, under Windows your file will look like this:
Line1
Line2
Under Linux/Unix however, the output is undefined. This
means that sometimes it may look right, other times it may
not, but there is no "same way" every time.
\t is usually used to line up columns of text at the
console, and just like using the tab key in a text editor
such as notepad, will attempt to line up the columns as long
as the text fits within it's width. EG:
<?php
Print "Hello\tworld\n";
Print "Hello\tworld\n";
?>
Will produce the following output:
Hello World
Hello World
However, if the text in the first column is longer
<?php
Print "Hello Hello Hello\tworld\n";
Print "Hello\tworld\n";
?>
The following will be seen:
Hello Hello Hello World
Hello World
The remaining sequences \\ \$ \" & \' are all to
allow those characters to be printed in your string. This is
important, because as we have seen already if we use those
characters they have special meaning. Also please note that
a ' can be used in a string if the string is enclosed
by " and vice versa.
This can be very important especially when you are
outputting HTML to a web page, as web page tags need
enclosing parentheses around any attributes specified in the
tags, EG:
<?php
Print "<img src='mypic.jpg' alt='My Picture'/>";
?>
To output an image tag. You can also do this the other way:
<?php
Print '<img src="mypic.jpg" alt="My Picture"/>';
?>
The first way seems to be the most used, I'm guessing because of the variable substitution ability, EG:
<?php
$mypic = 'images/mypic.jpg';
Print "<img src='$mypic' alt='My Picture'/>";
?>
Don't be fooled by the $mypic being surrounded by '', if you look at the whole line it's the "" that matter, the inner '' will be ignored.
What next?
Well now you understand what strings are and how you can use
them, it's time to start reading the PHP manual page on
string functions, this can be found at
http://www.phpbuilder.com/manual/language
.types.string.php.
PHP has a huge amount of functions built into it for
handling strings, unfortunately I don't have the space here
to go over them all. The PHP manual link above has excellent
descriptions of what each of the functions do, as always I
encourage you to explore them and see what they do.
Don't worry if you use a function and it doesn't work as
expected, play with the code until it does. It's all about
playing with the code and understanding what does what.
Until next time
Don't tangle your strings. Check back next week when we'll
cover numbers & maths.
Shawty