PHP String

PHP String

ᴰᵒᵖᵖᵉˡᵍᵃⁿᵍᵉʳ 多佩尔甘格尔

A PHP string is a sequence of characters i.e. used to store and manipulate text. There are 4 ways to specify string in PHP.

  • single quoted
  • double quoted
  • heredoc syntax
  • newdoc syntax (since PHP 5.3)

 

Single Quoted PHP String

We can create a string in PHP by enclosing text in a single quote. It is the easiest way to specify string in PHP.

<?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.

<?php  
$str1='Hello text   
multiple line  
text within single quoted string';  
$str2='Using double "quote" directly inside single quoted string';  
$str3='Using escape sequences \n in single quoted string';  
echo "$str1 <br/> $str2 <br/> $str3";  
?>

 

Output:

Hello text multiple line text within single quoted string

Using double "quote" directly inside single quoted string

Using escape sequences \n in single quoted string

 

Note: In single quoted PHP strings, most escape sequences and variables will not be interpreted. But, we can use single quote through \' and backslash through \\ inside single quoted PHP strings.

1.    

<?php  
$num1=10;   
$str1='trying variable $num1';  
$str2='trying backslash n and backslash t inside single quoted string \n \t';  
$str3='Using single quote \'my quote\' and \\backslash';  
echo "$str1 <br/> $str2 <br/> $str3";  
?>  

 

Output:

trying variable $num1

trying backslash n and backslash t inside single quoted string \n \t

Using single quote 'my quote' and \backslash

 

Double Quoted PHP String

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.

<?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  
$str1="Using double "quote" directly inside double quoted string";  
echo $str1;  
?>  

 

Output:

Parse error: syntax error, unexpected 'quote' (T_STRING) in C:\wamp\www\string1.php on line 2

 

We can store multiple line text, special characters and escape sequences in a double quoted PHP string.

<?php  
$str1="Hello text   
multiple line  
text within double quoted string";  
$str2="Using double \"quote\" with backslash inside double quoted string";  
$str3="Using escape sequences \n in double quoted string";  
echo "$str1 <br/> $str2 <br/> $str3";  
?>  

 

Output:

Hello text multiple line text within double quoted string

Using double "quote" with backslash inside double quoted string

Using escape sequences in double quoted string

 

In double quoted strings, variable will be interpreted.

<?php  
$num1=10;   
echo "Number is: $num1";  
?>  

 

Output:

Number is: 10

 

PHP STRING FUNTIONS



addcslashes()

It is used to return a string with backslashes.

_______________________________


addslashes()

It is used to return a string with backslashes.

_______________________________

bin2hex()

It is used to converts a string of ASCII characters to hexadecimal values.

_______________________________

chop()

It removes whitespace or other characters from the right end of a string

_______________________________


chr()

It is used to return a character from a specified ASCII value.

_______________________________

chunk_split()

It is used to split a string into a series of smaller parts.

_______________________________

convert_cyr_string()

It is used to convert a string from one Cyrillic character-set to another.

_______________________________

convert_uudecode()

It is used to decode a uuencoded string.

_______________________________

convert_uuencode()

It is used to encode a string using the uuencode algorithm.

_______________________________

count_chars()

It is used to return information about characters used in a string.

_______________________________

crc32()

It is used to calculate a 32-bit CRC for a string.

_______________________________

crypt()

It is used to create hashing string One-way.

_______________________________

echo()

It is used for output one or more strings.

_______________________________

explode()

It is used to break a string into an array.

_______________________________

hebrev()

It is used to convert Hebrew text to visual text.

_______________________________

hebrevc()

It is used to convert Hebrew text to visual text and new lines (\n) into <br>.

_______________________________

hex2bin()

It is used to convert string of hexadecimal values to ASCII characters.

_______________________________

print()

It is used for output one or more strings.

_______________________________

fprint()

It is used to write a formatted string to a stream.

_______________________________

nl2br()

It is used to insert HTML line breaks in front of each newline in a string.

_______________________________

number_format()

It is used to format a number with grouped thousands.

_______________________________

htmlentities()

It is used to convert character to HTML entities.

_______________________________

html_entity_decode()

It is used to convert HTML entities to characters.

_______________________________

ord()

It is used to return ASCII value of the first character of a string.

_______________________________

parse_str()

It is used to parse a query string into variables.

_______________________________

Implode()

It is used to return a string from the elements of an array.

_______________________________

Lcfirst()

It is used to convert the first character of a string to lowercase.

_______________________________

Join()

It is the Alias of implode() function.

_______________________________

Levenshtein()

It is used to return the Levenshtein distance between two strings.

_______________________________

trim()

It is used to remove whitespace.

_______________________________

ltrim()

It is used to remove whitespace from the left side of a string.

_______________________________

rtrim()

It is used to remove whitespace from the right side of a string.

_______________________________

md5()

It is used to calculate the MD5 hash of a string.

_______________________________

md5_files()

It is used to calculate MD5 hash of a file.

_______________________________

metaphone()

It is used to calculate the metaphone key of a string.

_______________________________

Soundex()

It is is used to calculate the soundex key of a string.

_______________________________

setlocale()

It is used to set locale information.


_______________________________

money_format()

It is used to return a string formatted as a currency string.

_______________________________

printf()

It is used to show output as a formatted string.

_______________________________

sha1()

It is used to return the SHA-1 hash of a string.

_______________________________

sscanf()

It is used to parse input from a string according to a format.

_______________________________

str_getcsv()

It is used to parse a CSV string into an array.

_______________________________

sha1_file()

It is used to return the SHA-1 hash of a file.

_______________________________

similar_text()

It is used to compare the similarity between two strings.

_______________________________

str_pad()

It is used to pad a string to a new length.

_______________________________

str_ireplace()

It is used to replace some characters in a string (case-insensitive).

_______________________________

str_repeat()

It is used to repeat a string a specified number of times.

_______________________________

str_rot13()

It is used to perform the ROT13 encoding on a string.

_______________________________

str_shuffle()

It is used to randomly shuffle all characters in a string.

_______________________________

str_split()

It is used to split a string into an array.

_______________________________

str_word_count()

It is used to count the number of words in a string.

_______________________________

strcasecmp()

It is used to compare two strings.

_______________________________

strchr()

It is used to find the first occurrence of a string inside another string.

_______________________________

stscroll()

It is locale based string comparison.

_______________________________

strcmp()

It is binary safe string comparison.

_______________________________

strcspn()

It is used to reverses a string.

_______________________________

strip_tags()

It is used to strip HTML and PHP tags from a string.

_______________________________

stripcslashes()

It is used to unquote a string quoted with addcslashes().

_______________________________

stripos()

It is used to return the position of the first occurrence of a string inside another string.

_______________________________

strlen()

It is used to return the length of a string.

_______________________________

strnatcasecmp()

It is used to compare two strings using a "natural order" algorithm.

_______________________________

strnatcmp()

It is used to compare two strings using a "natural order" algorithm.

_______________________________

strncmp()

It is used to compare of the first n characters.

_______________________________

strpbrk()

It is used to search a string for any of a set of characters.

_______________________________

strpos()

It is used to return the position of the first occurrence of a string inside another string.

_______________________________

strrchr()

It is used to find the last occurrence of a string inside another string.

_______________________________

strrev()

It is used to reverse a string.

 _______________________________


Report Page