PHP String Notes
PHP String Notes
PHP string is a sequence of characters i.e., used to store and manipulate text.
1. single quoted
2. double quoted
3. heredoc syntax
4. newdoc syntax (since PHP 5.3)
Single Quoted
For specifying a literal single quote, escape it with a backslash (\) and to specify
a literal backslash (\) use double backslash (\\). All the other instances with
backslash such as \r or \n, will be output same as they specified instead of
having any special meaning.
For Example
Example 1
<?php
$str1='Hello text
multiple line
text within single quoted string';
$str2=’$str1 morning’
echo $str2
?>
Output:
In PHP, we can specify string through enclosing text within double quote also.
But escape sequences and variables will be interpreted using double quote
PHP strings.
Example 1
<?php
$str1=”Hello text
multiple line
text within single quoted string”;
$str2=”$str1,. morning”
echo $str2
?>
Output:
Example 2
<?php
$num1=10;
echo "Number is: $num1";
?>
Output:
Number is: 10
Heredoc
Heredoc syntax (<<<) is the third way to delimit strings. In Heredoc syntax, an
identifier is provided after this heredoc <<< operator, and immediately a new
line is started to write any text. To close the quotation, the string follows itself
and then again that same identifier is provided. That closing identifier must
begin from the new line without any whitespace or tab.
Naming Rules
The identifier should follow the naming rule that it must contain only
alphanumeric characters and underscores, and must start with an underscore
or a non-digit character.
For Example
Valid Example
<?php
$str = <<<Demo
It is a valid example
Demo; //Valid code as whitespace or tab is not valid before closing identifi
er
echo $str;
?>
Output:
It is a valid example
ADVERTISEMENT
Example
<?php
$city = 'Delhi';
$str = <<<DEMO
Hello! My name is Misthi, and I live in $city.
DEMO;
echo $str;
?>
Output:
ADVERTISEMENT
Hello! My name is Misthi, and I live in Delhi.
Newdoc
Newdoc is similar to the heredoc, but in newdoc parsing is not done. It is also
identified with three less than symbols <<< followed by an identifier. But here
identifier is enclosed in single-quote, e.g. <<<'EXP'. Newdoc follows the same
rule as heredocs.
Example-1:
<?php
$str = <<<'DEMO'
Welcome to php
Learn with newdoc example.
DEMO;
echo $str;
echo '</br>';
echo <<< 'Demo' // Here we are not storing string content in variable st
r.
Welcome to php.
Learn with newdoc example.
Demo;
?>
Output:
strlen($str)
This function returns the length of the string or the number of characters in
the string including whitespaces.
<?php
?>
Copy
str_word_count($str)
This function returns the number of words in the string. This function comes in
handly in form field validation for some simple validations.
<?php
?>
?>
Copy
strpos($str, $text)
This function is used to find the position of any text/word in a given string. Just
like an array, string also assign index value to the characters stored in it,
starting from zero.
<?php
?>
Copy
This function is used to replace a part of the string with some text. While using
this function, the first argument is the part of string that you want to replace,
second argument is the new text you want to include, and the last argument is
the string variable itself.
Let's take an example,
<?php
echo $str;
?>
Copy
Welcome to python
ucwords($str)
This function is used for formatting the string. This function converts first
letter/character of every word in the string to uppercase.
Let's take an example and see,
<?php
echo ucwords($str);
?>
Welcome To Php
strtoupper($str)
To convert every letter/character of every word of the string to uppercase, one
can use strtoupper() method.
<?php
echo strtoupper($str);
?>
Copy
WELCOME TO PHP
strtolower($str)
This function is used to convert every letter/character of a string to lowercase.
<?php
echo strtolower($str);
?>
welcome to php
?>
php
trim($str, charlist)
This function is used to remove extra whitespaces from beginning and the end
of a string. The second argument charlist is optional. We can provide a list of
character, just like a string, as the second argument, to trim/remove those
characters from the main string.
<?php
echo trim($str2,"Heo");
?>
Hello World
llo Hell