Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. PHPBeginner - What are Strings?

What are Strings? - Images: Japanese

Hey there, everyone! Let's keep things moving — this time we're diving into strings in PHP.

In programming, a sequence of characters is called a 'string'. In English, it's literally called a 'String'.

In PHP, you create a string by wrapping characters in either ''' (single quotes) or '"' (double quotes), like this:

<?php
echo 'Hello world';
echo "Hello world";

One thing to note: you can't mix them. Opening with one and closing with the other won't work — you need to use the same quote type on both ends.

<?php
echo 'Hello world"; // This won't work.

"So what does it mean to make something a string?" Good question. Let's say you have the number '1'. For a human reading it, whether it's a number or a character doesn't really matter — but in the programming world, that distinction is critical. Is '1' a numeric value, a string, or something else entirely? Being able to define that clearly makes your programs much more flexible and avoids all kinds of headaches.

Here's an example from JavaScript (not PHP, but it illustrates the point nicely). In JavaScript, you can use '+' for addition — but you can also use '+' to concatenate strings.

In JavaScript, wrapping something in ' or " makes it a string. By being clear about whether something is a string or a number, you get to control whether 1 + 2 produces the number 3 or the string 12 — exactly as intended.

console.log(1 + 2); // Outputs the number 3.
console.log("1" + "2"); // Outputs the string '12'.

If there were no way to define something as a string, '1 + 2' would always give you the number '3', which would severely limit what your programs can do. That's why almost every programming language has the concept of a 'string' — and being explicit about types also makes it easier to build the internals of a language itself.

This is just a small glimpse, but you can see why being able to define types is so useful. By the way, the category that a piece of data falls into is called its 'data type'. Data types include not just 'string' but also 'number', 'boolean', and more. We'll cover those in later articles, so for now let's get a solid grip on what 'strings' are all about.

With that in mind, let's take a closer look at how ''' and '"' work in PHP. The behavior varies between languages, and in PHP, there's actually a difference between the two.

First up: '''. With single quotes, whatever you write inside gets output exactly as-is. Any characters you type come out as a string, exactly as written. What you write is what you get — yes, it bears repeating.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
<?php
	echo 'Hello there';
?>
</body>
</html>

After PHP processes this, the text file that comes back looks like this:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
Hello there</body>
</html>

Sure enough, the text is output exactly as written.

Now let's look at ". For basic usage, it works the same way:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
<?php
	echo "Hello there";
?>
</body>
</html>

Running this gives you:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
Hello there</body>
</html>

Yep — same result as with single quotes. When it comes to outputting plain characters, ''' and '"' behave identically.

However, '"' has a special trick: it recognizes 'escape sequences' and processes them. Take a look at the source code below. '\n' is an escape sequence that represents a newline.
Note: Escape sequences are explained in the next article.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
<?php
	echo "Hel\nlo there";
?>
</body>
</html>

After PHP processes this, here's the text file that comes back:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
Hel
lo there</body>
</html>

Notice the line break right in the middle of the text. Now let's try the same thing using ''' instead of '"':

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
<?php
	echo 'Hel\nlo there';
?>
</body>
</html>

Here's what comes back this time:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
Hel\nlo there</body>
</html>

With ''', everything is output literally — '\n' is treated as the two characters backslash and 'n', not a newline. That's the key difference between ''' and '"'.

'"' also has another handy feature: it expands 'variables' inline when outputting. Check out the source code below.
Note: Variables are covered in a later article.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
<?php
	$txt = "Hatsune Miku";
	echo "I really like $txt .";
?>
</body>
</html>

Running this gives you:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
I really like Hatsune Miku .</body>
</html>

As you can see, '"' automatically expands variables and includes their values in the output.

One thing to watch out for when using variable expansion: if there's no space around the variable, PHP can't tell where the variable name ends, and the expansion won't work correctly. Here's the same example without the surrounding spaces:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
<?php
	$txt = "Hatsune Miku";
	echo "I really like$txttoday.";
?>
</body>
</html>

Here's what the text file looks like when this runs:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
I really like</body>
</html>

Something's off, right? Because there are no spaces, PHP can't identify 'txt' as the variable name on its own. It's essentially treating the variable as '$txttoday' instead of '$txt'.

Adding spaces would fix the problem, but then those spaces show up in the output too, which means unwanted whitespace around your variable value. Not ideal.

The solution is to wrap the variable in {} (curly braces). This tells PHP exactly where the variable name starts and ends.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
<?php
	$txt = "Hatsune Miku";
	echo "I really like{$txt}today.";
?>
</body>
</html>

The output for the above is:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Test</title>
</head>
<body>
I really likeHatsune Mikutoday.</body>
</html>

This way, the variable expands perfectly without needing any extra spaces. It's a commonly used technique, so it's worth committing to memory.

So that's the difference between ''' and '"' in a nutshell.

Since '"' has those extra features, it does a bit more work under the hood — which means ''' is technically faster for outputting plain strings. So when you're just outputting static text, ''' is the slightly better choice.

That said, the performance difference is tiny, so honestly either one works fine in practice.

Alright, let's wrap this one up. You may have noticed the term 'escape sequence' popping up a few times — that's exactly what we'll be covering in the next article. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .