0% found this document useful (0 votes)
3 views

String Function

The document provides examples of various PHP string functions, including strtolower(), strtoupper(), ucfirst(), lcfirst(), ucwords(), strrev(), strlen(), and strcmp(). Each function is explained with its syntax, a code example, and the expected output. The strcmp() function specifically compares two strings and returns an integer indicating their relative order.

Uploaded by

raut.tanhaji1956
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

String Function

The document provides examples of various PHP string functions, including strtolower(), strtoupper(), ucfirst(), lcfirst(), ucwords(), strrev(), strlen(), and strcmp(). Each function is explained with its syntax, a code example, and the expected output. The strcmp() function specifically compares two strings and returns an integer indicating their relative order.

Uploaded by

raut.tanhaji1956
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PHP String Function Examples

1) PHP strtolower() function

The strtolower() function returns string in lowercase letter.

Syntax

string strtolower ( string $string )

Example

<?php
$str="My name is KHAN";
$str=strtolower($str);
echo $str;
?>
Output:

my name is khan

2) PHP strtoupper() function

The strtoupper() function returns string in uppercase letter.

Syntax

string strtoupper ( string $string )

Example

<?php
$str="My name is KHAN";
$str=strtoupper($str);
echo $str;
?>

Output:

MY NAME IS KHAN

3) PHP ucfirst() function

The ucfirst() function returns string converting first character into uppercase. It doesn't change the
case of other characters.

Syntax

string ucfirst ( string $str )

Example
<?php
$str="my name is KHAN";
$str=ucfirst($str);
echo $str;
?>

Output:

My name is KHAN

4) PHP lcfirst() function

The lcfirst() function returns string converting first character into lowercase. It doesn't change the
case of other characters.

Syntax

string lcfirst ( string $str )

Example

<?php
$str="MY name IS KHAN";
$str=lcfirst($str);
echo $str;
?>

Output:

mY name IS KHAN

5) PHP ucwords() function

The ucwords() function returns string converting first character of each word into uppercase.

Syntax

string ucwords ( string $str )

Example

<?php
$str="my name is Sonoo jaiswal";
$str=ucwords($str);
echo $str;
?>
Output:

My Name Is Sonoo Jaiswal


6) PHP strrev() function

The strrev() function returns reversed string.

Syntax

string strrev ( string $string )

Example

<?php
$str="my name is Sonoo jaiswal";
$str=strrev($str);
echo $str;
?>

Output:

lawsiaj oonoS si eman ym

7) PHP strlen() function

The strlen() function returns length of the string.

Syntax

int strlen ( string $string )

Example

<?php
$str="my name is Sonoo jaiswal";
$str=strlen($str);
echo $str;
?>

Output:

24

PHP strcmp() Function

Comparing two strings is one of the most commonly used string operation in programming and web
development practices. The strcmp() is an inbuilt function in PHP and is used to compare two
strings. This function is case-sensitive which points that capital and small cases will be treated
differently, during comparison. This function compares two strings and tells us that whether the first
string is greater or smaller than the second string or equals to the second string.
Syntax:

strcmp($string1, $string2)

Parameters: This function accepts two parameters which are described below:

$string1 (mandatory): This parameter refers to the first string to be used in the comparison
$string2 (mandatory): This parameter refers to the second string to be used in the comparison.
Return Values: The function returns a random integer value depending on the condition of match,
which is given by:

Returns 0 if the strings are equal.


Returns a negative value (<0), if $string2 is greater than $string1.
Returns a positive value (>0) if $string1 is greater than $string2.

Example

<?php

// PHP program to illustrate the working of strcmp()


$string1 = "Welcome to GFG";
$string2 = "Welcome to GeeksforGeeks";
$string3 = "Welcome to GFG";

// In this case both the strings are equal


print_r(strcmp($string1, $string3));
echo "\n";

// In this case the first is greater


print_r(strcmp($string2, $string1));
echo "\n";

// In this case the second is greater


print_r(strcmp($string3, $string2))

?>
Output:

0
31
-31

You might also like