๐Ÿ“ Web Development

PHP Strings: Quotes, Functions and Formatting

Aug 22, 20265 min read3 viewsBy Flow

PHP strings are text โ€” names, emails, page titles, anything a person reads. Most of what a website outputs is a string, so the handful of functions below come up in nearly every script you write.

Single quotes and double quotes are not the same

This is the difference that matters most, and it catches people for weeks.

<?php
$name = "Ayesha";

echo "Hello $name<br>";
echo 'Hello $name';
?>

Output:

PHP strings output: double quotes print Hello Ayesha while single quotes print Hello $name

Double quotes look inside the string and swap variables for their values. Single quotes do not โ€” they print exactly what you typed, dollar sign and all.

Neither is better. Use double quotes when you want a variable in the text, single when you do not. If a string has no variables in it, single quotes say so at a glance.

Joining text

The dot from the operators lesson is still how you glue strings together.

<?php
$first = "Ada";
$last  = "Lovelace";

$full = $first . " " . $last;

echo $full . "<br>";

$full .= " โ€” programmer";
echo $full;
?>

Output:

Joining PHP strings with the dot operator to print Ada Lovelace, then appending with .=

.= adds to the end of what is already there. You will see it constantly in loops that build up a block of HTML one line at a time.

Length and case

<?php
$title = "learn php strings";

echo strlen($title);
echo "<br>";
echo strtoupper($title);
echo "<br>";
echo ucfirst($title);
echo "<br>";
echo ucwords($title);
?>

Output:

PHP strings length and case output: strlen 17, then strtoupper, ucfirst and ucwords results

strlen() counts characters, spaces included. ucfirst() lifts only the first letter; ucwords() lifts the first letter of every word.

These are the functions behind tidy headings and normalised names โ€” useful the moment you start accepting text from a form.

Searching and replacing

<?php
$text = "PHP is a server side language";

var_dump(str_contains($text, "server"));
echo "<br>";
echo str_replace("server side", "backend", $text);
?>

Output:

PHP strings search and replace: str_contains returns true and str_replace swaps server side for backend

str_contains() answers yes or no. str_replace() hands back a new string โ€” it does not change the original. Store the result or it is gone.

Trimming and cutting

Text from a form usually arrives with stray spaces on the ends. trim() removes them.

<?php
$input = "   ayesha@example.com   ";

echo "[" . $input . "]<br>";
echo "[" . trim($input) . "]<br>";
echo substr("Hello world", 0, 5);
?>

Output:

PHP strings trim output showing the email with and without surrounding spaces, and substr printing Hello

The square brackets are only there so you can see the spaces. substr() cuts a piece out: start at position 0, take 5 characters.

Counting starts at zero here, exactly like PHP arrays.

Strings and arrays together

Two functions move between the two, and you will reach for them often.

<?php
$csv = "php,mysql,html";

$parts = explode(",", $csv);
print_r($parts);

echo "<br>";
echo implode(" | ", $parts);
?>

Output:

PHP strings with explode and implode: the text split into an array, then joined back with pipes

explode() cuts a string into an array. implode() joins an array back into a string. Same pair from the arrays lesson, seen from the string side.

Three mistakes with PHP strings

Expecting single quotes to read variables. 'Hello $name' prints the dollar sign. Use double quotes when you want the value.

Throwing away the result. str_replace(), trim() and strtoupper() all return a new string and leave the original alone. trim($input); on its own does nothing at all.

Using + to join text. PHP uses the dot. Plus is only for numbers โ€” the same rule from PHP operators.

Common questions about PHP strings

The string questions that come up most often in real code.

What is the difference between single and double quotes in PHP?

Double quotes look inside the string and swap variables for their values. Single quotes print exactly what you typed, dollar sign and all. Use double quotes when you want a variable in the text, single when you do not.

How do I join two strings in PHP?

Use the dot: $full = $first . " " . $last;. Use .= to add to the end of a string you already have. The plus sign does not join text in PHP โ€” it is only for numbers.

Why does trim() not remove the spaces from my variable?

Because it returns a cleaned copy and leaves the original untouched. trim($input); on its own does nothing. Write $input = trim($input);, or use the returned value directly.

How do I count the characters in a PHP string?

strlen() returns the number of characters, spaces included. For text with accents or non-English characters use mb_strlen() instead โ€” strlen() counts bytes, so it over-counts anything outside plain ASCII.

What is next

PHP strings are what your visitor actually sees, and almost every one of them starts life as something typed into a form. Reading that input safely is the next step.

Need to go back? PHP arrays pairs closely with this lesson.

Comments

Loading comments...

Link copied to clipboard