Learn PHP Strings

A string can be created within a function you are using or you can store the string within a variable. The advantage of using a variable is that you can easily use the string multiple times within your script without having to recreate it every time.

Single Quotes, Double Quotes, and Heredoc

When creating strings, you have several options. The two most traditionally used methods are single and double quotes. Both choices are completely valid, however, I tend to favor double quotes because they allow for a larger selection of escaped characters to be used within the string. String creation using heredoc is unique to PHP and allows for easy creation of multiline strings. If you are going to be outputting the contents of the heredoc string to a web browser, you will need to include break tags or else the text will output to a single line. When creating a heredoc string, the identifier used after <<< to end the statement should be unique for every heredoc string created in your script.

Examples of Strings Created with Single Quotes, Double Quotes, and Heredoc

//Single Quotes
$single_quote_string='Hello Reader! <br />';

//Double Quotes
$double_quote_string="I hope you find this useful. <br />";

/*Note: Identifier "HERED" used below should be unique 
 *in every heredoc string created in your script. */
I think heredoc is a great way to create strings.<br /> 
Do you agree? <br />

echo $single_quote_string;
echo $double_quote_string;
echo $heredoc_string;



Hello Reader!
I hope you find this useful.
I think heredoc is a great way to create strings.
Do you agree?


Escaping Characters in PHP Strings

Certain characters must be escaped by preceding them with a backslash if you want to use them within your string. Many of these characters are not useful when outputting to the web, but they will be useful when manipulating the content of files.

Example of Strings with Escaped Characters


$my_line="Linefeed: \n";
$my_return="Carriage return: \r";
$my_htab="Horizontal tab: \t";
$my_vtab="Vertical tab: \v";
$my_feed="Form feed: \f";
$my_backslash="Backslash: \\";
$my_quote="Quote: \"";

//Note: Only required when an underscore or letter follow the $ symbol.
$my_dollar="Dollar Sign \$USD";

//Example usage:
echo "<font color=\"red\">Escaping quotes in html properties is a common need.</font>\n<br />\n"; 
//Note: If you view source, you will see <br /> on a line by itself.



Escaping quotes in html properties is a common need.


Concatenation Operator

The only operator in PHP specifically for use with strings is the concatenation operator, which is a period character (.). Its purpose is to combine two strings together. A common way this is used is to combine a chunk of canned text with a variable that will change.

Example of Concatenation Operator Usage

$first_string="I love learning PHP in";
$month=Date("F"); //Note: Assings current month to variable
$third_string="because it is my favorite month of the year.";

echo $first_string . " " . $month . " " . $third_string; 
/* Note: The two " " are used to create spaces between strings.
*        You can avoid this by adding spaces where necessary when
*        creating your strings. */



I love learning PHP in September because it is my favorite month of the year.


String Functions

There are a large number of functions built into PHP for working with strings, so if you need to manipulate a string in anyway, search the string functions manual at may be a simple solution for what you are wanting to do that is already built into the language. As an example, let’s say you have a string containing a lot of text, but you want to truncate it and only display the first 23 characters. You could easily do this using the PHP function substr_replace.

Example Using PHP Substr_Replace to Truncate a String

Hey there, code monkey! 
Isn't PHP scripting fun? 
I can't believe other people do things like 
party, watch TV, and play video games.

//Note: substr_replace (input string, replacement string, start replacing);
echo substr_replace($my_string,"",23); 



Hey there, code monkey!


Speak Your Mind