Changing over the string to lowercase in PHP: Here, we will become familiar with the rationale to change over the given string is a lowercase string without utilizing the library function in PHP.
Given a string and we need to change over it into a lowercase string without utilizing any library function.
PHP code:
<?php
//function definition
//this function accepts a string/text, converts
//text to lowercase and return the lowercase converted string
function lowercase($str)
{
$chars = str_split($str);
$result = '';
//loop from 0th character to the last character
for ($i = 0; $i < count($chars); $i++) {
//extracting the character and getting its ASCII value
$ch = ord($chars[$i]);
//if character is a lowercase alphabet then converting
//it into an lowercase alphabet
if ($chars[$i] >= 'A' && $chars[$i] <= 'Z')
$result .= chr($ch + 32);
else
$result .= $chars[$i];
}
//finally, returning the string
return $result;
}
//function calling
$text = "HELLO WORLD";
echo lowercase($text);
echo "<br>";
$text = "Hello world!";
echo lowercase($text);
echo "<br>";
$text = "Hello@123.com";
echo lowercase($text);
echo "<br>";
?>
Output
hello world
hello world!
hello@123.com
Code clarification:
We convert the string ($str) into a variety of characters ($chars) at that point figure their ASCII value utilizing ord() function. Since we realize that in ASCII, the lowercase characters come precisely 32 places after the capitalized proportionate, we add 32 to the ASCII value and afterwards convert it back to the character utilizing the chr() function. The output is put away in the $result variable.
This program is a decent verification of the idea.