PHP String

PHP string is a sequence of characters. It is used to store and manipulate text. We can specify a string literal in four ways:

  • single quoted
  • double quoted
  • heredoc syntax
  • nowdoc syntax (since PHP 5.3.0)
Single quoted: We can specify string in single quotes (the Character). Example
<?php
$s='Hello string with single quote'; 
echo $s; 
?>
Output

Hello string with single quote

Double quoted:We can also specify string in double quotes. Example1
<?php
$s="Hello php with Double Quotes.";
echo $s;
?>
Output Hello php with Double Quotes. Example2
<html>
<body>
<?php
$s="Using double "quotes" directly inside double quoted string"; 
echo $s; 
?>
</body>
</html>
Output Parse error: syntax error, unexpected 'quotes' (T_STRING) in C:\xampp\htdocs\phpfolder\index.php on line 4 PHP String There are various string functions in PHP.  These are given below: Following are string functions
 String Functions Description
strtolower() It returns string in lowercase letter.
strtoupper() It returns string in uppercase letter.
strlen() It returns the length of a string.
str_word_count() It counts the number of words in a string.
strrev() It reverses a string.
strpos() It searches for a specific text within a string.
str_replace() It replaces some characters with some other characters in a string.
ltrim() It removes characters from the left side of a string.
md5() It calculates the MD5 hash of a string.
strtok() It splits a string into smaller strings (tokens). etc
range() It is an array that contains a range of elements.
PHP strtolower() function: The strtolower() function is used to return string in lowercase letter. Example
<!DOCTYPE html>
<html>
<body>
<?php
    $str="HELLO WORLD PHP";
echostrtolower($str);
  ?>
</body>
</html>
Output hello world php PHP strtoupper() function: The strtoupper()function is used to return string in uppercase letter. Example
<!DOCTYPE html>
<html>
<body>
<?php
    $str="hello world php";
echomb_strtoupper($str);
  ?>
</body>
</html>
Output HELLO WORLD PHP PHP strlen() function:The strlen() function is used to return the length of a string. Example
<!DOCTYPE html>
<html>
<body>
<?php
    $str="hello world php";
echostrlen($str);
  ?>
</body>
</html>
Output 15 PHP strrev() function:The strrev() function is used to reverse a string. Example
<!DOCTYPE html>
<html>
<body>
<?php
$str="hello world php";
echostrrev($str);
?>
</body>
</html>
Output phpdlrowolleh PHP md5()function:The md5() function is used to calculate the MD5 hash of a string. Example
<!DOCTYPE html>
<html>
<body>
<?php
echo md5("Hello world");
?>
</body>
</html>
Output 3e25960a79dbc69b674cd4ec67a72c62 Note:you can try it yourself, someabove functions of string.