PHP Menu


Strings in PHP


In PHP, A String is a sequence of characters which are enclosed with single quotes or double quotes.

There are 2 ways to specify string

  • Single Quotes      Ex: $str=' Hello PHP ';
  • Double Quotes      Ex: $str=" Hello PHP ";

Where in Single quoted string, we can store multiline text, special characters and escape sequences.Where in Double quoted string, we can't use special characters directly.

☞ Double quoted string :

Example:

$age = 20; 
$str1="Your age is : $age";		// string1
echo $str1;
$str2="Your age is : '$age'";	// string2
echo $str2;

Output:

Your age is : 20
Your age is : '20'


☞ Single quoted string :

Example:

$age = 20; 
$str1='Your age is : $age';		//string1
echo $str1;
$str2='Your age is : "$age"';	//string2
echo $str2;

Output:

Your age is : $age
Your age is : '$age'

String Function in PHP

PHP provides various string functions to perform different operations on strings i.e. to access and manipulate strings. Those are,

☞ strtolower() :

It returns string in lowercase letters i.e. it converts given string into lowercase.

Example:

$str="StudyGLANCE";
$str1=strtolower($str);
echo $str1;

Output:

studyglance


☞ strtoupper() :

It returns string in uppercase letters i.e. it converts given string into uppercase.

Example:

$str="StudyGLANCE";
$str1=strtoupper($str);
echo $str1;

Output:

STUDYGLANCE


☞ ucwords() :

It converts first character of each word in given string into uppercase.

Example:

$str="study glance";
$str1=ucwords($str);
echo $str1;

Output:

Study Glance


☞ strlen() :

It returns the length of given string.

Example:

$str="study glance";
$len=strlen($str);
echo "Length of given string : $len";

Output:

Length of given string : 12


☞ strrev() :

It returns the given string in reverse.

Example:

$str="study glance";
$str1=strrev($str);
echo $str1;

Output:

ecnalg yduts


☞ str_word_count() :

It returns the number of words in the given string.

Example:

$str="study glance";
$wc=str_word_count($str);
echo $wc;

Output:

2


☞ strpos() :

It searchs for a specific text within a string. If a match is found, this function returns the character position of the first match. If no match is found, it will return false.

Example:

$str="study glance";
$pos=strpos($str,"glance");
echo $pos;

Output:

6


☞ str_replace() :

It replaces some characters with another characters in the given string.

Example:

$str="study glance";
$str1=str_replace("glance","world",$str);
echo $str1;

Output:

study world


☞ substr() :

It returns the sub-string from the given string.

Example:

$str="study glance";
$str1=substr($str,6);
$str2=substr($str,0,4);
echo $str1;
echo $str2;

Output:

glance
study


Example: "Strfundemo.php"


<html>

<head>
<title>String Functions</title>
</head>
<body>
<h1>String Functions Demo</h1>
<?php
$str
="I am study GLANCE";
echo 
"<strong>String in Lower case : </strong>".strtolower($str)."<br/>";
echo 
"<strong>String in Upper case :</strong> ".strtoupper($str)."<br/>";
echo 
"<strong>String in Word case :</strong> ".ucwords($str)."<br/>";
echo 
"<strong>Length of the String :</strong> ".strlen($str)."<br/>";
echo 
"<strong>String reverse :</strong> ".strrev($str)."<br/>";
echo 
"<strong>No of Words in String :</strong> ".str_word_count($str)."<br/>";
echo 
"<strong>Position of Sub-string :</strong> ".strpos($str,"am")."<br/>";
echo 
"<strong>String replace :</strong> ".str_replace("GLANCE","World",$str)."<br/>";
echo 
"<strong>Sub-string from given String :</strong> ".substr($str,5,5)."<br/>";
?>
</body>
</html>

Output :

image
Next Topic :Functions in PHP