PHP Strings in Brief: Quotes, Heredoc and String Functions

Quick Summary
- Single quotes and nowdoc print variables exactly as written, while double quotes and heredoc replace them with their values.
- Short examples cover substr, strrev, explode, str_split, strtok, str_replace, trim, wordwrap, nl2br, sscanf, and ord with chr.
- Check strpos and stripos results with === false, because a match at the very start returns 0; only strpos is case-sensitive.
A quick reference for strings in PHP: how single and double quotes differ, heredoc and nowdoc syntax for longer text, and the built-in functions that cut, reverse, split, search, replace, trim and format strings. Each section has a short code example.
Single quotes vs double quotes
Inside single quotes, PHP doesn’t replace variables or escape sequences such as \n, so $name and \n are printed exactly as written. Double quotes replace $name with its value and \n with a line break.
$name = "Nahid";
$string01 = 'my name is $name \n';
echo $string01;
echo "\n";
$string02 = "my name is $name \nHello !";
echo $string02;Heredoc
Heredoc syntax (<<<EOD) is a way to write a large block of text inside PHP. Like a double-quoted string, it replaces variables with their values.
/**
* Heredoc PHP syntax is a way to write large bloc of text inside PHP
*/
$aboutMe = <<<EOD
I am Nahid Mahamud
I am a full-stack Web Developer
I live in Dhaka, Bangladesh
EOD;
echo $aboutMe;
//Hredoc also parse variables
$myName = "Nahid Mahamud";
$myJob = "full-stack Web Developer";
$myPlace = "Dhaka, Bangladesh";
$aboutMe = <<<EOD
I am {$myName}
I am a {$myJob}
I live in {$myPlace}
EOD;
echo $aboutMe;Nowdoc
Nowdoc puts the identifier in single quotes (<<<'EOD') and works like a single-quoted string: variables are not replaced.
/**
* NOW DOC -> WORKS LIKE SINGLE QUOTE STRING
* WILL NOT PARSE VARIABLES AND STRING LITERAL
*/
$aboutMe = <<<'EOD'
I am Nahid
I am a UX/UI designer
I live in Dhaka
EOD;
echo $aboutMe;ASCII values with ord() and chr()
ord() returns the ASCII value of a character, and chr() returns the character for a value.
/**
* will print ASCI value from A - Z
*/
$A = ord('A');
$Z = ord('Z');
for($i=$A; $i<=$Z; $i++){
$letter = chr($i);
$asciVal = ord($letter);
echo $letter ." -> ". $asciVal;
echo PHP_EOL;
}Get part of a string with substr()
/**
* SUBSTR in String
*/
$greetings = "Hello World";
//will print -> Hello
echo substr($greetings,0,5);
//will print -> World
echo "\n";
$length = strlen($greetings);
echo substr($greetings,($length-5),$length);Reverse a string
/**
* REVERSE STRING
*/
$greetings = "Hello World";
$length = strlen($greetings)-1;
for( $i = $length; $i >= 0; $i-- ){
echo $greetings[$i];
}
///simple way
$greetings = "Hello World";
echo strrev($greetings);Split a string: explode(), str_split() and strtok()
/**
* STRING TOKENIZATION
* JOIN & IMPLODES -> DO THE SAME THING
*/
$greetings = "Hello World! How are you?";
$parts = explode("!",$greetings);
print_r ($parts);
$parts = explode(" ",$greetings);
print_r ($parts);
$original = join(" ", $parts);
echo $original;
//split by character
$parts2 = str_split($greetings);
print_r($parts2);
//iterator
$greetings = "Hello Dhaka, How are you?";
$parts3 = strtok($greetings, ", ");
while( $parts3 !== false ){
echo $parts3."\n";
$parts3 = strtok(", ");
}Find text in a string: strpos() and stripos()
strpos() is case-sensitive and stripos() is not. Both return the position of the first match, or false if there is none. A match at the very start returns 0, so check the result with === false.
/**
* SEARCH SOMETHING INSIDE A STRING
* strpos is case sensetive
* stripos is -> case insensetive
* Strripos will do reverse search
*/
$quote = "Jack of all trades, master of none";
$quote2 = "Jack of all Trades, master of none";
echo strpos($quote, "trades");
echo stripos($quote2, "trades");Search and replace with str_replace()
/**
* SEARCH INSIDE STRING & REPLACE
*/
$quote = "Jack of all trades, master of none";
$replaceString = str_replace('trades','tasks',$quote);
$replaceString2 = str_replace(array('trades','master'),array('tasks','expert'),$quote);
echo $replaceString;
echo PHP_EOL;
echo $replaceString2;Trim whitespace: trim(), ltrim() and rtrim()
/**
* STRING TRIMMING
* trim, ltrim, rtrim
* will remove nonredable charchaters
* ltrim -> will trim left sided characters
* rtrim -> will trim right sided characters
*/
$string = " hello \n";
//this will only trim spaces
$string2 = trim($string,' ');
//this will trim whitespaces + \n \t..
$string = trim($string);
echo $string2;
echo $string;Wrap long lines with wordwrap()
/**
* WORDWRAP IN STRING
*/
$string = "Life is like a hurricane here in Duckburg. Race cars, lasers, aeroplanes - it's a duck blur.";
echo $string;
echo PHP_EOL;
echo PHP_EOL;
echo wordwrap($string, 34);
echo PHP_EOL;
echo PHP_EOL;
echo wordwrap($string, 14, "\n", true);Turn line breaks into HTML breaks with nl2br()
/**
* NL-2-BR
* convert \n into a newline/break for HTML pages
*/
$string = "Life is like a hurricane \nhere in Duckburg. \nRace cars, lasers, aeroplanes - it's a duck blur.";
echo nl2br($string);Parse a formatted string with sscanf()
/**
* sscanf
*/
$name = "Nahid Mahamud 0212190022";
$parts = sscanf($name, "%s %s %10s");
print_r($parts);
$partsWithVar = sscanf($name, "%s %s %10s", $fname, $lname, $phone);
echo $fname;
echo PHP_EOL;
echo $lname;
echo PHP_EOL;
echo $phone;Related articles
- joining strings with . and .=: the concatenation operators
- formatting strings with printf and sprintf: placeholders instead of concatenation
- the PHP array cheat sheet: explode() and join() from the array side

