PHP strings and PHP string functions with example
Strings
A string is a collection of characters, where each character corresponds to a single byte. This indicates that PHP lacks native Unicode support because it only supports a set of 256 characters.
Note that as of PHP 7.0.0, there are no restrictions on string length for 64-bit builds. A string can be as big as 2GB in older versions and 32-bit variants (214783647 bytes maximum )
There are four ways to provide a string literal.
- Single quoted
- double quoted
- heredoc
- nowdoc syntax (since PHP 5.3.0)
Single quoted
A string can be specified most simply by enclosing it in single quotations (the character)
Variables and escape sequences for special characters won't be enlarged when they appear in single-quoted strings, unlike the double-quoted and heredoc syntaxes.
<?php
echo 'Arnold once said :"l\"ll be back ""; //Arnold once said: "I"ll be back "
echo 'you deleted C:*.* ?';// you deleted C:\*.*?
echo "This will not expand :\n a bewline ':// This will not expand :\n a newline "
echo 'variables do not expanded $either ';'// Variables do not $expand $either
?>
Double quoted
If double quotes are used to wrap the string ("). The following escape sequences for special characters will be interpreted by PHP:
Sequences Meaning
\n Linefed (LF or 0*0 (10)in ASCII)
\r Carriage return(CR or 0*0D (13) in ASCII)
\t Horizontal tab (HT or 0*0 (9) in ACII)
\v vertical tab(VT or 0*0B (11) in ASCII)(since PHP5.2.5)
\e ecape (ECS or 0*1B (27) in ASCII)(since PHP 5.4.4)
\f from feed (FF or 0*0C (12) in ASCII)(sinc PHP 5.2.5)
\\ backslash
\$ dollar sign
\" double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation ,which silently overflows to fit in a byte (e.g "\400" === "\000")
The string of characters that matches the regular expression is represented by the notation x[0-9-Fa-f]1,2.
The Unicode codepoint [0-9A-Faf]+, which corresponds to the regular rexpression, will be output to the string as that codepoint's UTF-8 representation (added in PHP 7.0.0)
The fact that variable names will be enlarged is the key characteristic of double-quoted strings.
Herdoc
Heredoc syntax is a third approach to delimit strings. An identifier is used once more to end the quotation after this operator.
<?php
$str =<<< EOD
Example of string
spanning multiple lines
using multiple syntax.
EOD;
echo $str;
?>
Nowdoc
What heredoc are to double-quoted strings, nowdoc are to single-quoted strings. Similar to a heredoc, a nowdoc is supplied, but no parsing is carried out inside of it. A nowdoc is identified with the same <<< sequence used for heredoc, but identifier which followss is enclosed in single quoted , e.g <<< 'EOT'.
Example
<?php
$str=<<< 'EOD'
spanning multiple lines
using nowdoc syntax
EOD;
echo $str;
?>
PHP String Functions
PHP has a number of string functions to access and modify strings.
Strlower() function
Lowercase letters are the result of the strlower() function.
Syntax
string strtolower (string $string)
Example <?php
$str ="My country is India";
$str=strlower($str);
echo $str;
?>
Output
my country is india
strtoupper() function
The strtoupper() function returns string in uppercase letter
syntax
string stroupperca(string $inputstring)
Example
<?php
$str ="My country is XYZ";
$str=strtouper($str);
echo $str;
?>
Output is:
MY COUNTRY IS XYZ
susfirst(() function
The first character of a string is converted to uppercase by the ucfirst() function. The case of the other character is unaffected.
Syntax
string ucfirst(string $str)
Example
</php
$str="my country is Xyz";
$str=ucfirst($str);
echo $str;
?>
Output
My country is Xyz
lcfirst()
The lcfirst() function reruns a string and lowercases the first character. The situation with the other character is unaffected. .
Syntax
string lcfirst(string $str)
Example
<?php
$str="My country is XYZ";
$str=lcfirst($str);
echo $str;
?>
Output
my country is xYZ
ucwords() function
The ucwords() function produces a string after capitalising the first letter of each word.
Syntax
string ucwords ( String $str)
Example
<?php
$tr="My country is xyz";
$str = ucwords($str) ;
echo $str;
?>
output
My Country Is Xyz
Tags:
php