Today's Question:  What does your personal desk look like?        GIVE A SHOUT

10 less known but useful PHP functions

  sonic0002        2014-01-20 08:01:31       6,526        0    

PHP has abundant built in functions. As PHP developers, we may have used many of them. But there are still some useful functions we may not be so familiar with. In this post, we will introduce some of them.

levenshtein()

Have you ever wondered how to check differences between two works? This function just does what you want. It can tell you how much the difference is between two words.

<?php
$str1 = "carrot";
$str2 = "carrrott";
echo levenshtein($str1, $str2); //Outputs 2
?>

get_defined_vars()

This is a very useful function when you wants to debug your codes when there are some issues. It will return an array of all variables defined.

<?php
print_r(get_defined_vars());
?>

php_check_syntax()

This function is to check the syntax of your PHP codes. But somehow this function is deprecated since PHP 5.05.

<?php
$error_message = "";
$filename = "./php_script.php";
if(!php_check_syntax($filename, &$error_message)) {
   echo "Errors were found in the file $filename: $error_message";
} else {
   echo "The file $filename contained no syntax errors";
}
?>

ignore_user_abort()

This function is to ignore the abort request from the client. Normally the client abort request will stop the execution of server side PHP code.

<?php
ignore_user_abort();
?>

highlight_string()

This function will call the built in PHP code renderer to highlight the PHP codes passed in. This function can take two parameters, the first one is the string to be highlighted while the second one is to indicate whether the highlighted string to be returned or displayed. If it's TRUE, the string will be returned,

<?php
highlight_string(' <?php phpinfo(); ?>');
?>

highlight_file()

This functions is similar to highlight_string(), but it takes the name of the file to be highlighted. And it will return the highlighted file as the string.

<?php
highlight_file("php_script.php");
?>

php_strip_whitespace()

This function is to strip the comments and white spaces in the file.

<?php
echo php_strip_whitespace("php_script.php");
?>

get_browser()

This function will read the browscap.ini file specified by browscap in the php.ini file. It will return the browser compatibility information.

<?php
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
?>

memory_get_usage(),memory_get_peak_usage(),getrusage()

These functions relate to the memory and CPU usage of the server. When we want to check the performance of our PHP codes, these functions will be very useful. However, these functions cannot be used on Windows.

<?php
echo "Initial: ".memory_get_usage()." bytes \n";
echo "Peak: ".memory_get_peak_usage()." bytes \n";
$data = getrusage();
echo "User time: ".
	($data['ru_utime.tv_sec'] +
	$data['ru_utime.tv_usec'] / 1000000);
echo "System time: ".
	($data['ru_stime.tv_sec'] +
	$data['ru_stime.tv_usec'] / 1000000);

?>

gzcompress(), gzuncompress()

These two functions are to compress and uncompress strings. The compression rate can be as much as 50%.

<?php
$string =
"Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Nunc ut elit id mi ultricies
adipiscing. Nulla facilisi. Praesent pulvinar,
sapien vel feugiat vestibulum, nulla dui pretium orci,
non ultricies elit lacus quis ante. Lorem ipsum dolor
sit amet, consectetur adipiscing elit. Aliquam
pretium ullamcorper urna quis iaculis. Etiam ac massa
sed turpis tempor luctus. Curabitur sed nibh eu elit
mollis congue. Praesent ipsum diam, consectetur vitae
ornare a, aliquam a nunc. In id magna pellentesque
tellus posuere adipiscing. Sed non mi metus, at lacinia
augue. Sed magna nisi, ornare in mollis in, mollis
sed nunc. Etiam at justo in leo congue mollis.
Nullam in neque eget metus hendrerit scelerisque
eu non enim. Ut malesuada lacus eu nulla bibendum
id euismod urna sodales. ";

$compressed = gzcompress($string);
$original = gzuncompress($compressed);

?>

Did you use these functions before.

Source : http://www.aqee.net/10-little-known-but-useful-php-functions/

PHP  FUNCTION 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.