May 6th 2016
A string in programming represents a series of characters typed in from the keyboard. They are usually enclosed in quotes, for example: 'Hello World!'.
PHP offers many built-in functions that can be used to manipulate strings. You can use these functions to retrieve the length of strings, convert strings to lowercase or to uppercase, there are even functions that allows you to replace characters from inside the strings.
A string variable declaration can be done with single quotes or double quotes notation. On top of that, you can also define them using the Heredoc and Nowdoc syntaxes.
Here is a list of string declarations in PHP:
Single quotes notation:
<?php $string = 'Hello World!'; ?>
Double quotes notation:
<?php $string = "Hello World!"; ?>
<?php $string = <<<LINE Hello World! LINE; ?>
<?php $string = <<<'STRING' Hello World! STRING; ?>
The difference between single and double quoted strings
There is a difference between strings that are defined with single quotes and strings that are defined with double quotes.
The difference resides in the fact that the double quoted strings are parsed and the single quoted strings aren't.
This means, that any variable found in a double quoted string will be evaluated and the value of the variable will take the place of the variable name.
Let's take the following code for example:
<?php $name = "John Doe"; $first_string = "My name is $name."; $second_string = 'My name is $name.'; echo $first_string; //Outputs: My name is John Doe. echo $second_string; //Outputs: My name is $name. ?>
The difference between Heredoc and Nowdoc
There is also a difference between these two syntaxes.
It's the same difference that appears between the single quoted strings and the double quoted ones.
Heredoc is parsed and Nowdoc isn't.
<?php $name = "John Doe"; $first_string = <<<STRING My name is $name. STRING; $second_string = <<<'STR' My name is $name. STR; echo $first_string; //Outputs: My name is John Doe. echo $second_string; //Outputs: My name is $name. ?>
Heredoc and Nowdoc act like the twin brothers of the quotes.
As you can see, Heredoc works like the double quoted strings and Nowdoc works like the single quoted ones.
These ways of defining strings are cleaner to work with because special characters need not be escaped.
A couple of good uses for Heredoc is to create SQL query variables and to create chunks of HTML code.
Heredoc and Nowdoc have a similar syntax, they both start with
<<< followed by an identifier (usually uppercased).
The identifier is set by the programmer that writes the code (it's not predefined), in the case of Nowdoc, it's wrapped in single quotes. You need to use the identifier to mark the end of the string definition .
Unlike some programming languages, PHP lacks a concatenation function, it uses the dot operator
<?php $first_string = 'Is that a bird?'; $second_string = ' Is that a plane?'; $third_string = $first_string . $second_string; echo $third_string; //Outputs: Is that a bird? Is that a plane? ?>
You can use the short notation to append content to an existing variable:
Turn a string into an array
PHP has a built-in function that can convert a string into an array.
You need to use the
explode function to achieve this.
explode function requires two arguments, the first one is the delimiter (the character that is used to distinguish between the array elements), and the second one represent the string that needs to be converted.
In the example, I used the space delimiter (" ").
<?php $string = 'The force is strong with this one.'; $array = explode(' ', $string); print_r($array); //Outputs: array('The', 'force', 'is', 'strong', 'with', 'this', 'one.') ?>
String handling functions:
Find out the length of a string with the
<?php echo strlen('John Doe'); //Outputs: 8 ?>
Remove whitespace from the start of the string and from the end of it with
<?php echo trim(' Let the trimming begin!'); //Outputs: Let the trimming begin! echo trim('The trimming has begun! '); //Outputs: The trimming has begun! echo trim(' The trimming works! '); //Outputs: The trimming works! ?>
str_replace to replace a word or multiple occurrences of a word from a string:
<?php echo str_replace('name', 'One Punch Man', 'I am name!'); //Outputs: I am One Punch Man! echo str_replace('work', 'fun', 'Web development means work, work and work!'); //Outputs: Web development means fun, fun and fun! ?>
Turn a string to uppercase letters using
<?php echo strtoupper('I have the power! He-man!'); //Outputs: I HAVE THE POWER! HE-MAN! ?>
Convert a string to lowercase letters with the
<?php echo strtolower('Justin Bieber'); //Outputs: justin bieber ?>
Cut a string down to a specific number of characters using the
<?php $string = 'Valar Morghulis'; echo substr($string, 0, 5); //Outputs: Valar ?>
ucwords to uppercase the first letter of every word within a string:
<?php echo ucwords('We will rock you!'); //Outputs: We Will Rock You! ?>
Uppercase the first word and lowercase the rest with the
<?php echo ucfirst('Winter Is Coming'); //Outputs: Winter is coming ?>
Get the number of words from within a string with the
<?php echo str_word_count('What is dead may never die'); //Outputs: 6 ?>
strrev to reverse a string:
<?php echo strrev('alucard'); //Outputs: dracula echo strrev('racecar'); //Outputs: racecar ?>
This article represents my take on the string variable type in PHP, I wrote about how they are declared, I pointed out some differences between them and highlighted the commonly used string functions. There are still more functions available in PHP that deal with strings. If you are eager to learn more about PHP strings, you can check out the links listed below or you can take a look inside the PHP Manual.
Let me know if you have any questions or if you found something that you consider inaccurate. Thank you for taking your time to read this article!