4. 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:
5. Double-quote strings
• Unlike single-quote strings, double-quote strings in PHP are capable
of processing special characters.
9. We can perform several functions on string in PHP . The string manipulation functions are.
10. 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.
11. str_word_count() - Count Words in a String
The PHP str_word_count() function counts the number of words in a string.
12. strrev() - Reverse a String
The PHP strrev() function reverses a string.
13. trim() function:
• This function allows us to remove whitespaces or strings from both
sides of a string.
15. str_replace() - Replace Text Within a String
The PHP str_replace() function replaces some characters with some other characters in a string.
21. 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
24. chop() Function
• The chop() function is used to remove whitespace or other predefined
character from the right end of a string.
• Syntax: chop(string,charlist);
• Example
<?php
$str = "Hello World!";
echo "Your string is :".$str."<br>";
echo "By using 'chop()' Functions is your string is: ".chop($str,"World!");
?>
Parameter Description Required/Optional
string the string to be
check
Required
charlist charlist parameter
is empty
Optional
Output:
Your string is :Hello
World!
By using 'chop()'
Functions is your
string is: Hello
25. PHP chunk_split() Function
• PHP chunk_split() function is used to splits a string into smaller parts
or chunks. The function does not alter the original string.
• Syntax: chunk_split(string,length,end)
<?php
$str = "Hello!;
echo "Your string is:".$str;
echo "By using 'chunk_split()' function your string is:".chunk_split($str,1,".
..");
?>
Parameter Description Required/Optional
string String to split Required
Length Specify the length of the
chunks. Default is 76
Optional
end Specify what to place at
the end each chunk.
Optional
Output:
Your string is: Hello
By using 'chunk_split()' function your string
is: H…e...l...l...o...!...
26. PHP type casting
• Type casting allows you to convert a value of one type to another.
To cast a value, you use the following type casting operators:
27. Which of the following functions is used to find
the first occurrence of a substring in a string?
• strlen()
• strpos()
• str_replace()
• str_split()
28. Which of the following functions is used to find
the first occurrence of a substring in a string?
• strlen()
• strpos()
• str_replace()
• str_split()
29. 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()
30. 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()
31. Which of the following functions is used to
count the number of words in a string?
• strlen()
• strpos()
• str_replace()
• str_word_count()
32. Which of the following functions is used to
count the number of words in a string?
• strlen()
• strpos()
• str_replace()
• str_word_count()
33. Which of the following is not a built-in PHP
function for working with strings?
• strlen()
• strpos()
• str_replace()
• str_split()
• strrev()
34. Which of the following is not a built-in PHP
function for working with strings?
• strlen()
• strpos()
• str_replace()
• str_split()
• strrev()
35. What is the following is the output of the following PHP code?
•
• $str = "Hello, world!";
• echo strlen($str);
• 13
36. What is the following is the output of the following PHP code?
• <?php
• $str = "Hello, world!";
• echo strpos($str, "world");
• ?>
• 7
37. What is the following is the output of the following PHP code?
• <?php
• $str = "Hello, world!";
• echo str_replace("world", "universe", $str);
• ?>
• Hello universe
38. What is the following is the output of the following PHP code?
• <?php
• $str = "Hello, world!";
• echo $str[0];
• ?>
• H