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.

Related Topics

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP doubleval() Function

PHP doubleval() Function The doubleval() function in PHP is used to give the float value of a variable. This function is an alias of floatval(). Syntax doubleval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that...

1 minute read.

PHP krsort() Function

PHP krsort() Function The krsort() function in PHP sorts an array by key in reverse order while maintaining the keys. Syntax krsort (array & $array[,int $sort_flags= SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This...

3 minutes read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP stripos() Function

PHP stripos() Function The stripos() function in PHP finds the position of the first occurrence of a substring in the specified string. It is a case-insensitive a binary-safe function. The related functions are as follows: strripos() Functionstrpos() Functionstrrpos() Function Syntax stripos ( string $haystack , mixed $needle [, int $offset...

1 minute read.

PHP sizeof() Function

PHP sizeof() Function The sizeof() function in counts all elements and return the size of the array. This function is an alias of count(). Syntax sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)-...

1 minute read.

PHP metaphone() Function

PHP metaphone() Function The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling...

1 minute read.

PHP array_udiff() Function

PHP array_udiff() Function The array_udiff() function in PHP calculates the difference of arrays by using a callback function for data comparison. Syntax array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func ) Parameter array1(required)- This array represents the first input array. array2(required)- This array represents the second input array. array3….(optional)- It...

1 minute read.

PHP floatval() Function

PHP floatval() Function The floatval() function in PHP is used to give the float value of a variable. Syntax floatval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that is to be converted. Return This function...

1 minute read.

PHP echo() Function

PHP echo() Function The echo() function in PHP outputs or displays one or more strings. The major differences between print() function and echo() function is that echo accepts an argument list and doesn't have a return value. Also, it is slightly...

1 minute read.

PHP str_shuffle() Function

PHP str_shuffle() Function The str_shuffle() function in PHP randomly shuffles all the characters of the specified string. Syntax str_shuffle ( string $str ) Parameter str(required)- This parameter represents the input string to shuffle. Return This function returns the shuffled string. Example 1 ...

1 minute read.

WordPress Theme

Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...

2 minutes read.

PHP in_array() Function

PHP in_array() Function The in_array() function in PHP checks if the value exists in the specified array or not. Syntax in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )  Parameter needle(required)- This parameter represents the searched value. haystack(required)- This parameter represents the input array. strict(optional)- This parameter...

1 minute read.

PHP quoted_printable_encode() Function

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.

PHP is_integer() Function

The PHP is_integer() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it returns...

1 minute read.

PHP is_int() Function

The PHP is_int() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it...

1 minute read.

PHP strcasecmp() Function

PHP strcasecmp() Function The strcasecmp() function in PHP compares two strings. It is a binary-safe case-insensitive string function. Syntax strcasecmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than( <...

1 minute read.

PHP sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP Example

We can create a simple program by writing the source code inside the php tag and save it with .php extension. Syntax: <?php // your code here ?> Let us take an example of PHP. <!DOCTYPE html> <html> <head>            ...

1 minute read.

PHP unset() Function

PHP unset() Function The unset() function in PHP is used to unset a given variable. This function destroys the specified variables. If this function is called inside any user-defined function, then it unsets the value associated with...

1 minute read.