Wednesday, 13 June 2018

PHP Basic Functions - splits, strcmp, md5, ucwords, strstr, trim, number_format

<?php
//splits a string into an array
echo "splits a string into an array -- ";
$a="this is test string";
$m=str_split($a,4);
print_r($m);

//Compare two strings
echo "<br><br>";
echo "Compare two strings -- ";
$a="gfhgfhgfhfghgfhfghg";
$b="g";
if(strcmp($a,$b)==0)
{
echo "both are same";
}
else
{
echo "both are not same";
}

//calculates the MD5 hash of a string.
echo "<br><br>";
echo "calculates the MD5 hash of a string -- ";
$a="testString";
echo md5($a);

//Convert the first character of each word to uppercase
echo "<br><br>";
echo "Convert the first character of each word to uppercase -- ";
$a="this is test string";
echo ucwords($a);

//searches for the first occurrence of a string inside another string
echo "<br><br>";
echo "searches for the first occurrence of a string inside another string -- ";
$a="teststring";
echo strstr($a,'n');

//removes whitespace and other predefined characters from both sides of a string.
echo "<br><br>";
echo "removes whitespace and other predefined characters from both sides of a string -- ";
$a="   this isTest    ";
echo trim($a);

//formats a number with grouped thousands.
echo "<br><br>";
echo "formats a number with grouped thousands -- ";
$a = 1235655.9857985;
echo number_format($a,4);
?>

-------------------------------------------------
Let me know your thoughts and questions in the comments.
Email: vyasankit2008@gmail.com

No comments:

Post a Comment