Strings in PHP
Strings in PHP
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.
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?
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];
?>