Strings
String in PHP
• A string is a sequence of letters, numbers, special characters and arithmetic
values or combination of all.
• used to store and manipulate text
• There are Four Way of creating String in PHP
◦ 1: Single quote
◦ 2: Double Quote
◦ 3: Heredoc
◦ 4: Nowdoc
Single Quoted
The simplest way to create a string is to enclose the string literal (i.e. string characters) in single
quotation marks ('), like this:
Single Quoted
This type of string does not process special characters inside quotes.
Double-quote strings
Unlike single-quote strings, double-quote strings in PHP are capable of processing special
characters.
Heredoc:
Heredoc is a special syntax in PHP that is used to define a multiline string. Heredoc strings are
enclosed in a special delimiter, which is a string that starts with <<< and ends with the same
string.
Heredoc syntax is similar to the double-quoted string, without the quotes
Heredoc:
Nowdoc
Nowdoc is similar to the heredoc, but in newdoc parsing is not done
Nowdoc syntax is similar to the single-quoted string
Newdoc
PHP Strings
A string is a sequence of characters, like "Hello world!".
we will look at some commonly used functions to manipulate strings.
strlen() - Return the Length of a String
The PHP strlen() function returns the length of a string.
str_word_count() - Count Words in a String
The PHP str_word_count() function counts the number of words in a string.
strrev() - Reverse a String
The PHP strrev() function reverses a string.
str_replace() - Replace Text Within a String
The PHP str_replace() function replaces some characters with some other characters in a string .
trim() function:
This function allows us to remove whitespaces or strings from both sides of a string.
strtolower() function:
This function converts a string into the lowercase string.
strtoupper() function:
This function converts a string into the uppercase string.
Comparing Strings
PHP strcmp() Function
Comparing Strings
The strcmp() function compares two strings.
The strcmp() function is binary-safe and case-sensitive.
Cleaning Strings
The trim() function removes whitespace and other predefined characters from both sides
of a string.
Related functions:
•ltrim() - Removes whitespace or other predefined characters from the left side of a string
•rtrim() - Removes whitespace or other predefined characters from the right side of a
string
Which of the following functions is used
to find the first occurrence of a
substring in a string?
• strlen()
• strpos()
• str_replace()
• str_split()
Which of the following functions is used
to find the first occurrence of a
substring in a string?
• strlen()
• strpos()
• str_replace()
• str_split()
Which of the following functions is used to
replace all occurrences of a substring in a string
with another substring?
• strlen()
• strpos()
• str_replace()
• str_split()
Which of the following functions is used to
replace all occurrences of a substring in a string
with another substring?
• strlen()
• strpos()
• str_replace()
• str_split()
Which of the following functions is used
to count the number of words in a string?
◦ strlen()
◦ strpos()
◦ str_replace()
◦ str_word_count()
Which of the following functions is used
to count the number of words in a string?
◦ strlen()
◦ strpos()
◦ str_replace()
◦ str_word_count()
Which of the following is not a
built-in PHP function for working
with strings?
◦ strlen()
◦ strpos()
◦ str_replace()
◦ str_split()
◦ strrev()
Which of the following is not a
built-in PHP function for working
with strings?
◦ strlen()
◦ strpos()
◦ str_replace()
◦ str_split()
◦ strrev()
What is the following is the output of the following PHP
code?
$str = "Hello, world!";
echo strlen($str);
13
What is the following is the output of the following PHP
code?
<?php
$str = "Hello, world!";
echo strpos($str, "world");
?>
7
What is the following is the output of the following PHP
code?
<?php
$str = "Hello, world!";
echo str_replace("world", "universe", $str);
?>
Hello universe
What is the following is the output of the following
PHP code?
<?php
$str = "Hello, world!";
echo $str[0];
?>