(click anywhere to close)

[PHP] Strings I

category: Website | course: PHP | difficulty:

Now that we’ve discussed what to do with floats and integers, it’s time to move on to the next scalar type: strings. PHP heavily relies on pieces of text being downloaded from servers, or uploaded to them. The famous echo statement can in the end only insert strings into the HTML page. That’s why there’s a lot of ways to alter, modify, and play with strings – and this chapter will tell you all about them.

Variable Expansion

You would expect that if you were to write a variable within a string, it would simply print the variable’s name instead of its value. With the single-quoted and nowdoc syntax that is the case – but, the double-quoted and heredoc syntax can do something special. When you write a variable, it will expand it and display its value instead.

$username = "Panda";

echo 'Last log in: $username at 2PM'; //Prints: Last log in: $username at 2PM
echo "Last log in: $username at 2PM"; //Prints: Last log in: Panda at 2PM

Escape Characters

Both the single-quoted and double-quoted syntax have a problem; what should I do if I want to add an actual quote as a character? If you would just type the quote character, PHP would think the string ended and the results wouldn’t be pretty. We can solve this by escaping the characters – which simply means that we add a backslash ( \ ) in front of it.

Single-quoted strings support only one escape sequence: \' (to use single quotes in your text)

Double-quoted strings support a bunch of escape sequence:




Newline (goes to start of a new line)


Carriage return (goes to end of newline, and moves back until it finds first character stop)


Horizontal tab


Vertical tab




Form feed




Dollar sign


Double quote

//This prints a warning that the variable $password is undefined
echo "Your password has been saved in the $password variable";

//This prints the sentence as you intended it
echo "Your password has been saved in the \$password variable";

//This throws an error
echo 'The meeting is scheduled at 2 o'clock';

//This prints what you want
echo 'The meeting is scheduled at 2 o\'clock';

Most of these only show themselves when you start working with HTML forms and input. Also, because the newline character differs across operating systems, it’s a custom on the internet to use \r\n for newlines. Just to be safe.

String Operators

We’ve seen that PHP converts strings to numbers (in a rather rude way) when we try to add them together using the mathematical operators. So, how should we combine strings? Well, we use another operator for “string addition”, which is called the concatenation operator ( . ).

$combinedstring = $string1 . $string2;
echo "Welcome, " + "my friend!"; //Prints 0, as both strings as interpreted as integers

echo "Welcome, " . "my friend!"; //Prints: Welcome, my friend!

Operator Precedence

Just as with the math operators, the concatenation operator has its own rank of importance. It has the same precedence as the + and operator, which is great most of the time, but can lead to some nasty errors (see the example).

echo "Total Costs: \$" + 50 + " USD"; //Prints: 50
echo "Total Costs: \$" + 50 . " USD"; //Prints: 50 USD
echo "Total Costs: \$" . 50 . " USD"; //Prints: Total Costs: $50 USD

Accessing Characters

Before I bestow upon you a large amount of useful string functions, I have one last special string syntax to explain. If you want to access a single character of string, you can do so with


Here, the index is the position of the character within the string. Computers start counting at 0, so the first character will have index 0, the second character index 1, and so on. (This counting system is important to never ever forget, as it can save you a lot of trouble.)

$tweet = "@pandaqi You really do like pandas, don't you?";

//If the first letter of the tweet is an at-sign
//and the second letter is NOT an empty space...
if($tweet[0] == "@" && $tweet[1] != " ") {
	//Classify the tweet as a response to somebody
	echo "The tweet is a response!";
Do you like my tutorials?
To keep this site running, donate some motivational food!
Chocolate Milk