Labels

php (35) javascript (31) phpjavascript (30) jquery (23) html (20) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Tuesday, January 30, 2018

Most Commonly Used PHP Functions

htmlentities()

This PHP function is used to convert all applicable characters to HTML entities. When you want to display some HTML code in your page it will show the result of your code but not the actual code in that kind of situation you can use htmlentities() and one more function Htmlspecialchars() will be explaining more about this function down.
<?php
$str = "I like <b>PHP</b>";
echo "Actual result:<br>";
echo $str.'<br><br>';
echo "Result by using function:<br>";
echo htmlentities($str);
?>

Result:

Htmlspecialchars()
<?php
$str = "<a href='http://www.phpjavascript.com/'>PHP</a>";
echo "Actual result:<br>";
echo $str.'<br><br>';
echo "Result by using function:<br>";
echo htmlspecialchars($str, ENT_QUOTES);
?>
Result:

strip_tags()
This function is identical to htmlentities() function except strip_tags function strips html tags and html from a string.
<?php
$str = "<a href='http://www.phpjavascript.com/'>PHP</a>";
echo "Actual result:<br>";
echo $str.'<br><br>';
echo "Result by using function:<br>";
echo strip_tags($str, ENT_QUOTES);
?>
Result:

explode()
The explode function take its input as a string and convert it into an array. The function takes the first parameter as separator and second one is a string. It converts a string into an array based on the separator.
<?php
$cars = "BMW,Mercedes,Audi,Honda,Ferrari";
$brands = explode(",", $cars); //separate by comma(,)
echo 'Brand Name : '.$brands[0].'<br>'; //BMW
echo 'Brand Name : '.$brands[2]; //Audi
?>

Result:


implode()
This function is just opposite to explode function. As explode function converts string into array, but the implode function take its input as array and convert it into string.
<?php
$cars = array("BMW","Mercedes","Audi","Honda","Ferrari");
$brands = implode(",", $cars);
echo $brands;
?>
Result:

rand()
The rand() function outputs a random number. there are two optional parameters that you can add to set mi and max for the number.
<?php
echo rand();
echo '<br>';
echo rand(10,20);
?>
Result:

str_replace()
This function is useful when you want to do a simple match and replace. 
This function accepts four parameters,
 three required. find, replace, string, count(optional).
<?php
$string = 'aaa-bbb-ccc';
echo str_replace('-', '*', $string);
?>
Result:
date()
The date() function can have two parameters, 
format(required) and timestamp(optional).
<?php
echo date("d-m-Y h:i:s");
?>
Result:
strlen()
The strlen function will return the length of a given string. 
The spaces and characters are added to the count.
<?php
$stringOne = "Hello PHP";
echo 'stringOne Length = ' . strlen($stringOne) . '<br><br>';
$stringTwo = " Welcome code";
echo 'stringTwo Length = ' . strlen($stringTwo);
?> 
Result:
count() This function count the number of element in a array.
<?php
$Alphabets = array("A", "B", "C", "D", "E");
echo 'Count = '.count($Alphabets);
?>
Result: array_combine() This function creates an array by using one array for keys and another for its values. <?php $fruit_color = array("green", "red", "yellow"); $fruits = array("avocado", "apple", "banana"); $fruits_array= array_combine($fruit_color, $fruits); print_r($fruits_array); ?> Result:
array_unique()
You can use this function to removes duplicate values from an array.
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
Result:
print_r()
This function is used to print array variable in a way that’s readable by humans.
<?php
$fruits = array ("a" => "Banana", "b" => "Apple");
print_r ($fruits);
?>
Result:
strrev()
This function reverses the given string.
<?php
echo strrev("Hello PHP");
?>
Result:
trim()
This function removes characters, space from the string form both the ends.
<?php
$str = "Hello PHPJavaScript";
echo $str . "<br>";
echo trim($str,"Ht");
?>
Result:
ucfirst()
This function Converts the first character to uppercase.
<?php
echo ucfirst("hello php!");
?>
Result:
For all PHP built in functions, go to  PHP.NET
Please do share and fallow our blog for more intresting content.

6 comments:

  1. PHP has got you covered with over 80, that's right count them, functions to provide any type of operationReally appreciate this wonderful post that you have provided for us.
    Great site and a great topic as well i really get amazed to read this.Its really good.V as share good stuff with good ideas and concepts. lots of great information and inspiration.I am impressed with your site’s blog keep sharing the useful post . looking forward to many interesting and helpful articles from you.
    I appreciate your excellent post.

    ReplyDelete
  2. This is most most that you like your blog. SO MUCH DEEP INFORMATION.
    hotmail.com
    hotmail email

    ReplyDelete

  3. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
    Webdesining course in chennai

    ReplyDelete

More Posts

FOLLOW