A string is series of characters. For example ‘C’ is a character
and ‘Cyber Teak’ is a string. Everything inside quotes, single (‘ ’) and double
(“ ”) in PHP is treated as a string.
A string literal can be specified in two different ways:
1. Single-quote strings:
Single quite type of string does not processes special characters inside quotes.
Example:
<?php
//Single-quote
string
$var = ‘Welcome
in Cyber Teak’;
echo $var;
?>
2.
Double-quote strings: If the string is enclosed in double-quotes ("),
PHP will process the following escape sequences for special characters like “\n”,
“\t”, etc.
Example:
<?php
//
double-quote strings
echo “Welcome
in \n”;
$var = “cyberteak.com”;
echo $var;
echo “Welcome
in $var”;
?>
Some frequently used special characters that are used with
double-quote strings.
- “\n” is replaced by a new line
- “\t” is replaced by a tab space
- “\$” is replaced by a dollar sign
- “\r” is replaced by a carriage return
- “\\” is replaced by a backslash
- “\”” is replaced by a double quote
- “\’” is replaced by a single quote
String Functions
Built in functions
in PHP are some existing library functions which can used directly in our
programs making an appropriate call to them.
1. strlen() function: This
function is used to find the length of a string. This function is take the string
as argument and return the length. Mean return number of characters in the
string.
Example:
<?php
echo strlen(“Hello Cyber Teak”); //
output 16
?>
2. strrev() function: This
function is used to reverse a string. This function accept a string as argument
and returns its reversed string.
Example:
<?php
echo strrev(“Hello CyberTeak!”); // output !kaeT rebyC olleH
?>
3. str_word_count() function:The PHP
str_word_count() function counts the number of words in a string.
Example:
<?php
echo str_word_count("Cyber Teak!"); //
outputs 2
?>
4. strpos() function:The PHP
strpos() function searches for a specific text within a string.
Example:
<?php
echo strpos("Cyber Teak!", "Teak");
// outputs 6
?>
5. str_replace() function: The
str_replace() function replaces some characters with some other characters in a
string.
Example:
<?php
echo str_replace("Teak", "Veer",
"Cyber Teak!"); // outputs Cyber Veer!
?>
Reference
0 Comments