Work with Strings in PHP

PHP is a Hypertext-preprocessor. A string can be used directly in a function or it can be stored in a variable. Here in this articles we will use the XAMPP sever to run the php scripting.

Strings in PHP
With a string variable we can store and manipulate text. String variable are used to contain the characters.

Let us see the various kind of functionality available with strings in PHP.

  • String variable
  • String Length
  • String Position
  • String Replace
  • String Toupper
  • String ucfirst
  • String Trim


This is the php scripting to understand the functionality.  

body bgcolor="#ffffCC">
<h3> PHP STRINGS </h3>
echo " String variable myString : ";
$myString =
"Text of a String variable !";
echo $myString;
?> <br><br><hr>
echo "String Length(): length of DEEPAK DWIJ is -->";
echo strlen("DEEPAK DWIJ");
?> <br><br><hr>
echo "String Position() : Position of dwij in Deepak dwij is -->";
echo strpos("Deepak dwij","dwij");
?> <br><br><hr>
echo "Replace() : Deep is replaced by Deepak dwij -->";
$myString =
"My name is Deep";
echo str_replace("Deep", "Deepak dwij", $myString);
?> <br><br><hr>
echo "String Toupper() : deepak is change to DEEPAK --> ";
$myString =
echo strtoupper($myString);
echo "UCfirst() : deepak is change to Deepak --> ";
$myString =
echo ucfirst($myString);
?> <br><br><hr>
echo"StringTrim() : ";
$myString =
" Deepak! ";
echo trim($myString);

Saved it by string.php.

Result of string.php scripting

The above script look like as under when it runs on browser:


Various String functions
The string functions allow you to manipulate strings and you can add functionality according to your requirement. Here you can find some of important predefined functions, here they are:

addcslashes()Returns a string with backslashes in front of the specified characters
addslashes()Returns a string with backslashes in front of predefined characters
chop()Alias of rtrim()
chr()Returns a character from a specified ASCII value
crypt()One-way string encryption (hashing)
explode()Breaks a string into an array
fprintf()Writes a formatted string to a specified output stream
strcoll()Locale based string comparison
Returns the number of characters found in a string before any part of some specified characters are found
str_word_count()Count the number of words in a string
str_split()Splits a string into an array

Note : many more inbuilt-functions are available.

PHP string constant

Now you can find some the predefined constants.

Constant Description
CRYPT_SALT_LENGTH Contains the length of the default encryption method for the system. For standard DES encryption, the length is 2
CRYPT_STD_DES Set to 1 if the standard DES-based encryption with a 2 character salt is supported, 0 otherwise
CRYPT_EXT_DES Set to 1 if the extended DES-based encryption with a 9 character salt is supported, 0 otherwise
CRYPT_BLOWFISH Set to 1 if the Blowfish encryption with a 16 character salt starting with $2$ or $2a$ is supported, 0 otherwise0
CRYPT_MD5 Set to 1 if the MD5 encryption with a 12 character salt starting with $1$ is supported, 0 otherwise

Note : many more are also available.