Skip to content
Strings
step 1/5

Reading — step 1 of 5

Learn

~1 min readStrings

PHP has two main string-quote styles, and the difference matters:

$name = "Alice";
echo "Hello, $name\n";        // double quotes — interpolates $vars and \n
echo 'Hello, $name\n';        // single quotes — literal $name and literal \n

String functions:

$s = "hello";
echo strlen($s);              // 5
echo strtoupper($s);          // HELLO
echo str_replace("l", "L", $s); // heLLo
echo substr($s, 1, 3);        // ell
echo strpos($s, "ll");         // 2 (index)

Concatenate with .: "a" . "b". For complex interpolation use {$expr} inside double-quotes:

echo "Length: {$arr['key']}";

sprintf for printf-style formatting. heredoc and nowdoc (<<<TAG ... TAG) for multi-line strings.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…