String manipulation in PHP

(Last Updated On: December 8, 2018)

1. Introduction

In the last article in this series, we learned about arrays. In this article, we will learn about strings in PHP. String is a collection or sequence of characters. “Bootsity” is an example of string. Now, let’s learn more about strings.

2. Defining Strings

In PHP, we can define string in multiple ways. Lets discuss them one by one:

2.1 Single Quote Strings

We can declare strings using single quotes. When we use single quotes to define a string and print them, PHP interpreter does not parse the string and print it as it is. Consider:

$many = 9;
$sentence = 'Our solar system contains $many planets';
print $sentence;

Output:

Our solar system contains $many planets

2.2 Double Quote Strings

PHP also allows declaring strings in double quotes. At the time of printing of a double quote string, it is parsed and if there are any variables, they are replaced. Consider:

$many = 9;
$sentence = "Our solar system contains $many planets";
print $sentence;

Output:

Our solar system contains 9 planets

2.3 heredoc

Heredoc method of defining string in PHP is used when we have very complex strings.

The heredoc supports all the features of double quotes. It also allows strings to be defined in more than one line without string concatenation.

We use heredoc syntax when there is a complex string spread over multiple lines. In this syntax, we may use double quotes inside the string sequence without escaping them.

Example:

$name = "Bootsity";

$here_doc = <<<EOT
This is $name website
for PHP, Laravel and Angular Tutorials

EOT;

echo $here_doc;

Output:

This is Bootsity website
for PHP, Laravel and Angular Tutorials

2.4 nowdoc

The Nowdoc string definition method in PHP is like the heredoc method but it works like the way single quotes work.

For nowdoc, no parsing takes place at time of printing inside the Nowdoc blocks.

Nowdoc string definition comes handy when we are working with raw data that do not need to be parsed.

Consider:

$name = "Bootsity";

$here_doc = <<<'EOT'
This is $name website
for PHP, Laravel and Angular Tutorials

EOT;

echo $here_doc;

Output:

This is $name website
for PHP, Laravel and Angular Tutorials

 

3. Concatenating strings

In PHP, string concatenation is done using concatenation operator, which is denoted by a dot (.)

Lets see how we can use it practically:

$str = "Bootsity"." Tutorials"
echo $str; // will print - Bootsity Tutorials

4. Some important String functions

In this section, we are including most frequently used in-built PHP functions related to strings. If you want to see the complete list of string functions, you can see it here – http://php.net/manual/en/ref.strings.php

4.1 strtolower

strtolower makes a string lowercase

print strtolower("PHP and Laravel Tutorials"); // php and laravel tutorials

Similarly, to make a string uppercase, we can use strtoupper

4.2 strlen

strlen computes length of string

print strlen("abcd"); // 4

4.3 explode

explode returns array after separating strings delimited by given string

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

4.4 substr

substr returns part of the string

$rest = substr("abcdef", 0, -1);  // returns "abcde"
$rest = substr("abcdef", 2, -1);  // returns "cde"
$rest = substr("abcdef", 4, -4);  // returns false

4.5 ucfirst

ucfirst turns a string’s first character uppercase

$foo = 'hello world!';
$foo = ucfirst($foo);             // Hello world!

5. Comments

Comments are strings that we write in our programs to give information about the current code. Comments are meta-data about code and are not executed at run-time. PHP supports two type of comments: 1. Single line comments and 2. Multi line comments. Let’s see them:

<?php

/* Multiline comment
   it spreads across multiple lines
*/

// Single line comment

# another syntax for single line comment
?>

6. Conclusion

In this article we learned about what is string, the four ways to define a string – single quote, double quote, heredoc and nowdoc. We also learned to concatenate strings using dot concatenation operator. We also saw single line and multiple line comments in PHP. In the next article in this series, we will learn about different operators in PHP.

LEAVE A REPLY

Please enter your comment!
Please enter your name here