Google News
logo
PHP Strings
A string is sequence(series) of characters. where every character is the same as a byte. i.e. used to store and manipulate text.

A string literal can be specified in three different ways.

single quoted : 'Hello world!'
double quoted : "Hello world!"
heredoc syntax : strings is by using heredoc syntax ("<<<").
PHP String - Single quoted

We can create a string in PHP by enclosing text in a single quote.

<!DOCTYPE html>
<html>
<head>
	<title>PHP String - Single quoted</title>
</head>

<body>

<?php
	$str='Welcome to Free Time Learning(www.freetimelearning.com).';  
	echo $str;  ;
?>


</body>
</html>
Output :
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.

<!DOCTYPE html>
<html>
<head>
	<title>PHP String - Double quoted</title>
</head>

<body>

<?php
	$str="Welcome to Free Time Learning(www.freetimelearning.com).";  
	echo $str;  ;
?>

</body>
</html>
Output :
PHP String Functions

PHP provides various string functions to access and manipulate strings. A list of important PHP string functions are given below.

Function Description
addcslashes() Returns a string with backslashes before the specified characters
addslashes() Returns a string with backslashes in front of predefined characters
bin2hex() Convert binary data into hexadecimal values
chop() Removes whitespace or other characters from the right end of a string
chr() Returns a one-character string containing the character specified by ASCII
chunk_split() Split a string into smaller chunks
convert_cyr_string() Converts a string from one Cyrillic character set to another
convert_uudecode() Decodes a uuencoded string
convert_uuencode() Encodes a string using the uuencode algorithm
count_chars() Return information about characters used in a string
crc32() Calculates the crc32 polynomial of a string
crypt() One-way string encryption (or hashing)
echo() Output one or more strings
explode() Split a string into an array by a specified string
fprintf() Write a formatted string to a specified output stream
get_html_translation_table() Returns the translation table used by htmlspecialchars() and htmlentities()
hebrev() Convert logical Hebrew text to visual text
hebrevc() Convert logical Hebrew text to visual text with newline conversion
hex2bin() Decodes a hexadecimally encoded binary string
html_entity_decode() Convert all HTML entities to their applicable characters
htmlentities() Convert all applicable characters to HTML entities
htmlspecialchars_decode() Convert special HTML entities back to characters
htmlspecialchars() Convert special characters to HTML entities
implode() Return a string by joining the elements of an array with a specified string
join() Alias of implode()
lcfirst() Converts the first character of a string to lowercase
levenshtein() Calculate Levenshtein distance between two strings
localeconv() Returns localized numeric and monetary formatting information
ltrim() Removes whitespace (or other characters) from the beginning of a string
md5() Calculate the md5 hash of a string
md5_file() Calculates the md5 hash of a file
metaphone() Calculate the metaphone key of a string
money_format() Formats a number as a currency string
nl_langinfo() Returns specific local information
nl2br() Inserts HTML line breaks before all newlines in a string
number_format() Format a number with grouped thousands
ord() Returns the ASCII value of the first character of a string
parse_str() Parses the string into variables
print() Output a string
printf() Output a formatted string
quoted_printable_decode() Convert a quoted-printable string to an 8 bit string
quoted_printable_encode() Convert a 8 bit string to a quoted-printable string
quotemeta() Quotes meta characters
rtrim() Removes whitespace (or other characters) from the end of a string
setlocale() Set locale information
sha1() Calculate the sha1 hash of a string
sha1_file() Calculate the sha1 hash of a file
similar_text() Calculate the similarity between two strings
soundex() Calculate the soundex key of a string
sprintf() Return a formatted string
sscanf() Parses input from a string according to a format
str_getcsv() Parse a CSV string into an array
str_ireplace() Replace all occurrences of the search string with the replacement string. Case-insensitive version of str_replace().
str_pad() Pad a string to a certain length with another string
str_repeat() Repeats a string a specified number of times
str_replace() Replace all occurrences of the search string with the replacement string (case-sensitive)
str_rot13() Performs the ROT13 encoding on a string
str_shuffle() Randomly shuffles all characters in a string
str_split() Splits a string into an array
str_word_count() Counts the number of words in a string
strcasecmp() Binary safe comparison of two string (case-insensitive)
strchr() Finds the first occurrence of a string inside another string. Alias of strstr()
strcmp() Binary safe comparison of two string (case sensitive)
strcoll() Locale based comparison of two string (case sensitive)
strcspn() Returns the number of characters found in a string before any part of some specified characters are found
strip_tags() Strip HTML and PHP tags from a string
stripcslashes() Un-quotes a string quoted with addcslashes()
stripos() Find the position of the first occurrence of a case-insensitive substring in a string
stripslashes() Un-quotes a quoted string
stristr() Finds the first occurrence of a string inside another string (case-insensitive). Case-insensitive version of strstr()
strlen() Returns the length of a string
strnatcasecmp() Compares two strings using a "natural order" algorithm (case-insensitive)
strnatcmp() Compares two strings using a "natural order" algorithm (case-sensitive)
strncasecmp() Binary safe string comparison of the first n characters (case-insensitive)
strncmp() Binary safe string comparison of the first n characters (case-sensitive)
strpbrk() Searches a string for any of a set of characters
strpos() Find the position of the first occurrence of a substring in a string
strrchr() Find the last occurrence of a character in a string
strrev() Reverses a string
strripos() Find the position of the last occurrence of a string inside another string (case-insensitive)
strrpos() Find the position of the last occurrence of a string inside another string (case-sensitive)
strspn() Returns the number of characters found in a string that contains only characters from a specified charlist
strstr() Find the first occurrence of a string inside another string (case-sensitive)
strtok() Splits a string into smaller strings
strtolower() Converts a string to lowercase
strtoupper() Converts a string to uppercase
strtr() Translate characters or replace substrings
substr() Return a part of a string
substr_compare() Compares of two strings from a specified start position up to the length of the comparison. The comparison is binary safe and optionally case-sensitive
substr_count() Counts the number of times a substring occurs in a string
substr_replace() Replaces a part of a string with another string
trim() Removes whitespace (or other characters) from the beginning and end of a string
ucfirst() Converts the first character of a string to uppercase
ucwords() Converts the first character of each word in a string to uppercase
vfprintf() Write a formatted string to a specified output stream
vprintf() Output a formatted string
vsprintf() Return a formatted string
wordwrap() Wraps a string to a given number of characters
PHP strlen() function

The strlen() function is used to calculate the number of characters inside a string. It also includes the blank spaces inside the string.

<!DOCTYPE html>
<html>
<head>
	<title>PHP strlen() function</title>
</head>

<body>


<?php
	echo strlen("Free Time Learning");
?>

</body>
</html>
Output :
PHP str_word_count() function

The PHP str_word_count() function counts the number of words in a string:

<!DOCTYPE html>
<html>
<head>
	<title>PHP str_word_count() function</title>
</head>

<body>


<?php
	echo str_word_count("Free Time Learning");
?>

</body>
</html>
Output :
PHP str_replace() function

The str_replace() replaces all occurrences of the search text within the target string.

<!DOCTYPE html>
<html>
<head>
	<title>PHP str_replace() function</title>
</head>

<body>


<?php
	echo str_replace("Time", "Learning", "Free Time");
?>

</body>
</html>
Output :
PHP strrev() function

The strrev() function reverses a string.

<!DOCTYPE html>
<html>
<head>
	<title>PHP strrev() function</title>
</head>

<body>


<?php
$ftl = 'Free Time Learning';
 
// Display reversed string
	echo strrev($ftl);
?>

</body>
</html>
Output :
PHP strtoupper() function

The strtoupper() function returns string in uppercase letter.

<!DOCTYPE html>
<html>
<head>
	<title>PHP strtoupper() function</title>
</head>

<body>


<?php
$ftl = 'Free Time Learning';
 
   // Upper case Letters
	echo strtoupper($ftl);
?>

</body>
</html>
Output :
PHP strtolower() function

The strtolower() function returns string in lowercase letter.

<!DOCTYPE html>
<html>
<head>
	<title>PHP strtolower() function</title>
</head>

<body>


<?php
$ftl = 'Free Time Learning';
 
   // Upper case Letters
	echo strtolower($ftl);
?>

</body>
</html>
Output :