Using Strings
Maxim Maletsky on 2001 February 21
Maxim Maletsky on 2001 February 21
Strings are the core functionality in web programming. Usually all PHP programmer does is generating text under certain conditions and by retrieving the data from a database to be outputted later on as HTML, email, queries, system commands or in any other form. And all the texts for PHP are just a bunch of strings. That's why, knowing how to handle strings is very important. It not only makes your scripts run faster but also makes your coding style more flexible and clean
.So, what are the strings?
A string is anything surrounded by two matching quotes:
<?php
"I am a string inside double quotes"
'I am a string inside single quotes'
?>
What I mean here by "matching quotes" is that a string must start and end with the same type of quotes. For example, a string starting with a single quotes may have double quotes inside but must still end with a single quote:
<?php
'I can have here as many "Double Quotes" as I want'
but I cannot end it with an "
?>
Once a string is started it is looking for an ending quote and will not end unless it finds it.
<?php
'This shouldn't work at all
?>
Here PHP understands it as:
'This shouldn' - a totally legal single quoted string,
t work at all'- some strange, not understood bunch of characters which, for sure, would produce you a parse error.
Escaping characters in string
If you need to have the same quote inside as the string uses itself you must escape it with a backslash ().
<?php
'This string won't give you any problems'
?>
The parser would comment that like:
"starting single quoted string ' ending single quoted string' STOP! - a preceding backslash found!' remove backslash leaving the quote untouched' continue' searching for the next single quote' ending single quoted string' no backslash found - DONE!"
Note that the backslash itself is something special in all kind of strings and has to always be escaped with another backslash.
Strings and Variables
The variable values can also be used in strings. To do so you have to join a string and a variable by using concatenate operator (.), Yes, a dot.
<?php
$name = 'Maxim';
$surname = 'Maletsky';
echo = 'Full Name: ' . $name . ' ' . $surname; // prints as "Full Name: Maxim Maletsky"
?>
A dot must be placed before or after a string connecting it to a variable.
Whenever two variables are next to each other you can just use concatenate operator to merge them
<?php
$max = 'Max';
$im = 'im';
echo $max . $im; // Will print "Maxim"
?>
Concatenate (.) and the assignment (=) when joined create a sequence of a variable
<?php
$name = 'Maxim ';
$name .= 'Maletsky';
echo $name; // Will produce "Maxim Maletsky"
?>
This is very useful when building an SQL query. For example:
<?php
$SQL = "SELECT";
if($everything == 'Yes')
$SQL .= " * ";
else
$SQL .= " this, and, that ";
$SQL .= "FROM table";
$result = mysql_query($SQL); // here $SQL is: SELECT ' FROM table
?>
The difference between single quotes and double quotes
This is something many people ask: "is there any difference between single and double quotes?" The difference in fact is: Double quoted strings are interpreted while single quoted strings are treated exactly as written.
When PHP reads a double quoted string it searches inside for variables and other values such as n t r.
<?php
$name = 'Maxim';
$surname = 'Maletsky';
echo "The user is $name $surname"; // The user is Maxim Maletsky
echo 'The user is $name $surname'; // The user is $name, $surname
echo "The user is $name $surname"; // The user is Maxim Maletsky (and new line)
echo 'The user is $name $surnamen'; // The user is $name, $surnamen
?>
It is always better to exit the string with concatenate (.) when using something more complex then a simple variable.
<?php
echo "The user is $user['surname'], $user['name']"; // will result you a parse error
// You should use concatenate (.)
echo "The user is " . $user['surname'] . ", " . $user['name'];
?>
Here is what works in strings and what doesn't:
<?php
echo "Name = $user[$name]"; // this will work
echo "Name = $user[$name][$surname]"; // this will not work as expected,
echo "Name = GetName()";
// use concatenate(.) instead:
echo 'Name = ' . $user[$name][$surname];
echo 'Name = ' . GetName();
// You can also put braces around the complex variables:
echo "Name = {$user[$name][$surname]}"
?>
How to choose the right quotes type for my string?
It is totally programmer's choice what quotes to use for strings. It is all about personal coding style. The performance doesn't change dramatically.
However I would recommend using single quotes as often as you can and avoid the useless use of double quotes. Because double quoted strings are interpreted they are also a little slower then single quoted strings. And, unless you have only simple variables to add to the string, using single quote is smarter. Here are some examples of my personal own style:
<?php
// this is the best for a string with no variables inside
echo '<IMG SRC="/images/submit.gif">';
// this is very useful for strings which have some variables in it.
$image = '/images/submit.gif';
echo "<IMG SRC="$image">";
?>
If you are too lazy escaping double quotes in double quoted strings there are two work-arounds for this:
<?php
// 1 // Use single quotes for html and double quotes for strings
echo "<IMG SRC='$image'>";
// 2 // Have a variable with double quotes in it already:
$image = '"/images/submit.gif"';
echo "<IMG SRC=$image>";
?>
Single quotes are very useful to use with HTML.
An example of combination of single and double quoted strings:
<?php
$html = '<table border="0" width="200" cellspacing="0" cellpadding="0">';
$html .= '
<tr align="center" valign="top">
<td width="100">Number</td>
<td>Square</td>
</tr>
';
for ( $i=0 ; $i<10 ; $i++) {
$square = $i * $i;
$html .= "t<tr>ntt<td>$i</td>ntt<td>$square</td>nt</tr>n";
}
$html = "$htmln</table>";
?>
Well, I guess this is enough of my own programming style, time to create one for yourself!
Good Luck,
Maxim Maletsky
