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;
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;
Our solar system contains 9 planets
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.
$name = "Bootsity"; $here_doc = <<<EOT This is $name website for PHP, Laravel and Angular Tutorials EOT; echo $here_doc;
This is Bootsity website for PHP, Laravel and Angular Tutorials
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.
$name = "Bootsity"; $here_doc = <<<'EOT' This is $name website for PHP, Laravel and Angular Tutorials EOT; echo $here_doc;
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
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
strlen computes length of string
print strlen("abcd"); // 4
explode returns array after separating strings delimited by given string
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces; // piece1 echo $pieces; // piece2
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
ucfirst turns a string’s first character uppercase
$foo = 'hello world!'; $foo = ucfirst($foo); // Hello world!
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 ?>
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.