Strings
Single vs double quotes
Double-quoted strings interpolate variables. Single-quoted strings are mostly literal.
<?php
$name = "Ada";
echo 'Hello $name\n';
echo "Hello $name\n";
Concatenate
Use the . operator to join strings.
echo "Hi, " . $name . "!\n";
Useful functions
PHP provides many string helpers.
$raw = " php ";
echo trim($raw), "\n";
echo strtoupper("php"), "\n";
echo strlen("php"), "\n";
String habits
- Prefer single quotes when you do not need interpolation
- Be careful with user input and HTML escaping (
htmlspecialchars) - Multibyte text may need
mb_*functions - Heredoc/nowdoc help with multi-line templates
Comments
One comment per signed-in account. Comments are saved with this page’s URL.