PHP String

tutorial and example
tutorial and example

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

Output

Hello string with single quote

Double quoted:We can also specify string in double quotes.

Example1

Output

Hello php with Double Quotes.

Example2

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

Output

hello world php

PHP strtoupper() function: The strtoupper()function is used to return string in uppercase letter.

Example

Output

HELLO WORLD PHP

PHP strlen() function:The strlen() function is used to return the length of a string.

Example

Output

15

PHP strrev() function:The strrev() function is used to reverse a string. Example

Output

phpdlrowolleh

PHP md5()function:The md5() function is used to calculate the MD5 hash of a string.

Example

Output

3e25960a79dbc69b674cd4ec67a72c62

Note:you can try it yourself, someabove functions of string.