<< PHP Output | PHP Tutorials | PHP Operators >>

PHP Strings

Creating strings is simple. They can either be created in variables, or just as regular output (as shown via last section 'PHP Output'). Here is an example of a string stored inside a variable:

<?php
$string = 'Hello there.';
?>

That outputs nothing, but Hello there. will be stored inside the $string variable. You can output it like so:

<?php
$string = 'Hello there.';
echo $string;
?>

What the end user sees:

Hello there.

Notice you don't have to surround the $string variable with quotes when you are outputting it. This is because the PHP engine knows it is a string, therefore the quotes are not needed.

Double Quotes vs. Single Quotes

Yes, one is better than the other - it's true. The winner is of course single quotes. Using single quotes is easier, plus it's faster ( la test). Both will work, and the choice is up to you, but I suggest single quotes.

Connecting Strings (Concatenation)

Yeah that is a big word, don't try to remember it. I didn't know what it was until a few minutes ago actually :P Strings can be connected! You can either connect them via variables, or directly inside an output statement. They are connected like so:

<?php
$string_one = 'Hello';
$string_two = ', how are you?';
$string = $string_one . $string_two;
echo $string . '<br />';
echo $string_one . $string_two;
?>

What the end user sees:

Hello, how are you?
Hello, how are you?

Pretty spiffeh huh? Anyway the . (period) will act as a connector between strings stored as variables or just directly in quotes.

Variables in Strings

Yup, that's possible too. Say you had a number and you wanted it in a string, but you don't want to use concatenation. Well you can just use the variable inside the string to accomplish this:

<?php
$number = 1;
$string = "I am number $number!";
echo $string;
?>

What the end users will see:

I am number 1!

As you can see above, the variable was put directly into the string. Using this method is useful but can get out of hand. If there are characters after the variable you are trying to output, it won't output because the PHP parser thinks it is looking for that variable. So I suggest you use { and } to wrap the variables:

<?php
$number = 1;
$string = "I am number {$number}!";
?>

What the end users will see:

I am number 1!

See? The same thing except the variable is now wrapped. You may have also noticed that I'm using double quotes to output variables instead of single quotes. Single quotes tell the PHP engine that there are no variables to be parsed in the string, so they will be ignored. Double quotes on the other hand do tell the PHP engine that there are variables and they should be parsed. If you wanted to accomplish the same thing as above with single quotes, you'd have to do it like this:

<?php
$number = 1;
$string = 'I am number ' . $number . '!';
?>

What the end users will see:

I am number 1!

See? Will single quotes you'll have no choice but to use that big word that starts with c :)

PHP Heredoc Syntax

Heredoc. Nice word eh? This is a great invention by the PHP peeps. It is basically like the 'hole' in the PHP code I mentioned before, except it is actually inside the PHP code. The syntax is as follows:

<?php
$number = 1;
$string = <<<MY_STRING
I am number {$number}!
MY_STRING;
?>

What the end users will see:

I am number 1!

Yeah, I skipped ahead a bit with the variable in there, but it's useful to know. Heredoc is like double quotes too, as variables can go inside them. But you don't need to use the backslash to escape anything. There are rules to be followed here! Because if the following rules aren't followed, an error will be output.

The first rule is the <<< that you see there. This tells the PHP engine that MY_STRING (which follows the naming conventions used on a variable. Anyway the three <s must be present. Just think of it as sending the data into that variable (which it is doing :P). Also, the declaration of the end of the heredoc must be in the first column of code and that line of code must look like this:

A_STRING;

It cannot contain anything but the orignal name plus the semicolon. You can also directly output the heredoc string:

<?php
echo <<<A_STRING
Some text <b>and bold.</b>
A_STRING;
?>

What the end users will see:

Some text and bold.

As you can see above you do not need the = when using echo to output a heredoc string. This may be too complicated for you right now, so if you don't feel comfortable with it just use normal single and double quotes.

Special Characters

There are some special characters you should note down that involve strings. These are Escaped Characters that will show up when inside a double quoted string or heredoc string:

  • \n - linefeed - creates a new line (not equal to <br />)
  • \r - carriage return - creates a new line (not equal to <br />)
  • \t - a horizontal tab space
  • \\ - backslash
  • \$ - dollar sign (if not followed by a character not allowed in variables)
  • \" - double quote

<< PHP Output | PHP Tutorials | PHP Operators >>