Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

PHP String

dynamic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

PHP String

dynamic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PHP String

PHP string is a sequence of characters i.e., used to store and


manipulate text. PHP supports only 256-character set and so that it
does not offer native Unicode support. There are 2 ways to specify a
string literal in PHP.
1. single quoted
2. double quoted

Single Quoted
We can create a string in PHP by enclosing the text in a single-quote.
It is the easiest way to specify string in PHP.
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.
Example
<?php
$str='Hello text within single quote';
echo $str;
?>
Output
Hello text within single quote
We can store multiple line text, special characters, and escape
sequences in a single-quoted PHP string.

Double Quoted
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
<?php
$str="Hello text within double quote."
echo $str;
?>
Output
Hello text within double quote.
Now, you can't use double quote directly inside double quoted
string.

PHP String Function Examples


String functions in PHP are used to manipulate string values.
Function Description Example Output
strtolower echo strtolower( outputs benjamin
Used to convert all ‘Benjamin’);
string characters
to lower case
letters

Function Description Example Output


str_replace Used to locate echo str_replace that laptop is very
and replace (‘the’, ‘that’, expensive
specified string ‘the laptop is
values in a very
given string. expensive’);
The function
accepts three
arguments. The
first argument
is the text to be
replaced, the
second
argument is the
replacement
text and the
third argument
is the text that
is analyzed.

strpos Used to locate echo strpos(‘PHP 4


the and return Programing’,’Pro’);
the position of a
character(s)
within a string.
This function
accepts two
arguments

str_word_count Used to count echo 12


the number of str_word_count
words in a
string. (‘This is a really
long sentence
that I wish to cut
short’);

ucfirst Make the first echo Outputs Respect


character of a ucfirst(‘respect’);
string value
upper case

lcfirst Make the first echo Outputs rESPECT


character of a lcfirst(‘RESPECT’
);
string value lower
case

You might also like