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_map() Function

PHP array_map() Function The array_map() function  in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] )  Parameter callable(required)- This parameter...

1 minute read.

PHP strval() function

PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...

1 minute read.

PHP substr_count() Function

PHP substr_count() Function The substr_count() function in PHP counts the number of times a substring occurs in the specified string. Syntax substr_count ( string $haystack , string $needle [, int $offset  [, int $length ]] ) Parameter haystack(required)- This parameter signifies the string to search in. needle (required)- This parameter represents the substring to search...

1 minute read.

PHP Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.

PHP array_uintersect() Function

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.

PHP strnatcasecmp() Function

PHP strnatcasecmp() Function The strnatcasecmp() function in PHP compares two strings using a "natural" algorithm. It is a case-insensitive function. Syntax strnatcasecmp ( 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...

1 minute read.

PHP array_diff() Function

The array_diff () function in PHP compares array1 against other specified arrays and returns the values in array1 that are not present in any of the other arrays. Syntax array_diff(array$array1,array$array2[,array$...] ) Parameter array1(required)- This parameter signifies the input array to...

1 minute read.

Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. PHP does...

3 minutes read.

PHP array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP array_multisort() Function

PHP array_multisort () Function The array_multisort() function in PHP sorts multiple or multi-dimensional arrays at once. Syntax array_multisort ( array &$array1 [, mixed $array1_sort_order [, mixed $array1_sort_flags [, mixed $... ]]] ) Parameter array1(required)- This parameter specified the input array to sort. array1_sort_order(optional)- This parameter specifies the order (ascending or descending)...

1 minute read.

PHP is_double() Function

The PHP is_double() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP is_resource() Function

PHP is_resource() Function The is_resource() function in PHP finds whether a variable is a resource or not. It returns a Boolean value TRUE if the parameter var is resource, else it returns FALSE Syntax is_resource ( mixed $var ) Parameter var(required)- This parameter represents the value to check. Return This function...

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 strtoupper() Function

The strtoupper() function in PHP is used to convert a string to uppercase. This function is binary-safe. The related functions are as follows: strtolower()lcfirst()ucfirst()ucwords() Syntax strtoupper ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

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.

PHP sha1() Function

PHP sha1() Function The sha1() in PHP calculates the SHA-1 hash of a string. Syntax sha1 ( string $str [, bool $raw_output = FALSE ] )  Parameter str (required)- This parameter represents the input string. raw_output(optional)- This parameter takes Boolean value and returns the digest in raw binary format...

1 minute 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 uksort() Function

PHP uksort() Function The uksort() function in PHP sorts an array by keys using a user-defined comparison function. Syntax uksort ( array &$array , callable $key_compare_func ) Parameter array(required)- This parameter represents the input array. key_compare_func(required)- This parameter represents a string that describe a callable comparison function....

1 minute read.

PHP array_shift() Function

PHP array_shift() Function The array_shift() in PHP shifts the first value of the array off. This function returns the same value after shortening the array by one element and moving everything. Syntax array_shift ( array &$array )  Parameter array(required)- This parameter represents the input array Return This function returns...

1 minute read.

PHP soundex() Function

PHP soundex() Function The soundex() function in PHP Calculates the soundex key(four character long alphanumeric string that represent English pronunciation of a word) of a string. Syntax soundex ( string $str ) Parameter str(required) – This parameter represents the input string. Return This function returns the soundex...

1 minute read.