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

The addcslashed() function in PHP is used to add quote string with slashes in a C style. It is case-sensitive function.  Syntax addcslashes (str, charlist ) Parameter str(required)- This parameter represents the string to be escaped. charlist(required)-...

1 minute read.

PHP String get_html_translation_table() Function

The get_html_translation_table() function in PHP is used to return the translation table used by the htmlentities() and htmlspecialchars() functions. Syntax get_html_translation_table ([ int $table = HTML_SPECIALCHARS [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" ]]] ) Parameter table (optional)- This parameter specifies which translation table to return. It...

2 minutes read.

PHP pos() Function

PHP pos() Function The pos() function in PHP returns the current element in an array which is initialized to the first element of the array. This function is an alias of current(). Syntax pos ( array...

1 minute read.

PHP strripos() Function

PHP strripos() Function The strripos() function in PHP finds the position of the last occurrence of a substring in the specified string. It is a case-insensitive and binary-safe function. Syntax strripos ( string $haystack , mixed $needle [, int $offset ] ) Parameter haystack(required)- This parameter signifies the...

1 minute read.

PHP Associative Arrays

PHP associate array is used to access the elements with keys by “=>” symbol. There are two ways to create an associative array. 1st way: $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); 2nd way: $age['john'] = "20"; $age['Helena'] =...

1 minute read.

PHP Data Types

PHP data type is used to hold the various types of data or value.  There are three types of data types in PHP. It is a loosely data typed language, so...

2 minutes read.

PHP implode() Function

PHP implode() Function The implode() function in PHP joins the specified array elements with a string. Syntax implode ( string $glue, array $pieces ) Parameter glue(optional)- It specifies what to put between the array elements. pieces(required)- This parameter represents...

1 minute read.

PHP array_merge() Function

PHP array_merge() Function The array_merger() Function in PHP merges the elements of one or more arrays wherein the values of one are appended to the end of the previous one. It returns the resulting array...

1 minute read.

PHP array_combine() Function

PHP array_combine() Function The array_combine() function in PHP is used to create an array by combining two arrays i.e., one array for keys and another array for its values. Syntax array_combine ( array $keys , array $values ) Parameter keys(required)- This parameter represents an array which acts...

1 minute read.

PHP strtolower() Function

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

1 minute read.

PHP vfprintf() Function

PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args )  Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...

3 minutes read.

PHP array_diff_uassoc() Function

PHP array_diff_uassoc() Function The array_diff_uassoc () Function in PHP is used to compare the keys and values of two (or more) arrays and returns the differences (an array containing the entries from the first array that...

1 minute read.

PHP array_count_values() Function

PHP array_count_values() Function The array_count_values() function in PHP counts all the values present in the specified array and returns an array using the values of the array as keys and their frequency in the array as values. Syntax array_count_values ( array $array ) Parameter array(required)- This parameter represents the...

1 minute read.

PHP print_r() Function

PHP print_r() Function The print_r() function in PHP displays the information stored in a variable. Syntax print_r (mixed $expression [, bool $return] ) Parameter expression(required)- This parameter represents the expression to be printed. return(optional)- This parameter is used to store the output of...

2 minutes read.

PHP key_exists() Function

PHP key_exists() Function The key_exists() function in PHP checks if the specified key or index exists in the array or not. Syntax key_exists ( mixed $key , array $array )  Parameter key(required)- This parameter represents the value to check. array(required)- This parameter signifies an input array with...

1 minute read.

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

2 minutes read.

PHP key() Function

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP Multidimensional Array

An array of arrays is called multidimensional array. It is used to store the data in a tabular form. It is represented in the form of matrix (row*column). Example: We can store...

2 minutes read.

PHP strtok() Function

PHP strtok() Function The strtok() function in PHP splits a string into smaller strings or tokenize the specified string.  Syntax strtok ( string $str , string $token ) Parameter str(required)- This parameter represents the input string to split into smaller strings or tokens. token(required)- It signifies...

1 minute read.

PHP array_diff_ukey() Function

The array_diff_ukey() function in PHP compares the keys from array1 against the keys from array2 and returns the difference containing all the entries from array1 that are not present in any of the other arrays. Syntax array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) Parameter array1(required)- This parameter signifies the...

1 minute read.