GET A QUOTE

5 Important, but overlooked PHP Functions

php development serviceI have been developing applications in PHP for years, but recently I came across upon functions and features that I did not know about. Some of these can be quite useful and help a PHP developer during their projects . With that in mind, I’ve compiled a list of five extremely useful PHP functions that a PHP developer should be familiar with.

1) Arbitrary Arguments

You might have heard the word ‘Arbitrary Arguments’ for the first time. In php or other development you create many functions with fixed arguments and at the time of calling function you pass same number of arguments or less then that. But now you can pass as many arguments as you want using func_get_args().the perameters are not limited. For this function arguments are empty.

Check following example:

 function test() {
 		   $args = func_get_args();
		   
		   foreach ($args as $k => $v) {
		   echo "arg".($k+1).": $v\n";
		   }
		 }
		   
		   test();
		   Output:
		   /* prints nothing */
                   
		   test('hello');
		   Output:
		   arg1: hello
		   
                     
		   test('hi','goodmorning','john');
		   Output:
		   arg1: hi
		   arg2: goodmorning
		   arg3: john

2) Using Glob() to Find Files

In php you find many different functions to find file from directory. Limitation of those functions are it’s display all files from folder.

Glob() function is different from all other functions, you can find files with different patterns like following.

$files = glob('*.js');

	print_r($files);
	output:
	Array
	(
	    [0] => jquery.js
	    [1] => datepicker.js
	    [2] => bootstrep.js
	    [3] => custom.js
	)

	$files = glob('*.{js,css}', GLOB_BRACE);

	print_r($files);
	output:
	Array
	(
	    [0] => jquery.js
	    [1] => datepicker.js
	    [2] => bootstrep.js
	    [3] => custom.js
	    [4] => custom.css
	    [5] => style.css
	)
	
	$files = glob('c*.js');
 	print_r($files);

	output:
	Array
	(
	    [0] => calender.js
	    [1] => custom.js
	)

3) Predefined Magic Constants

In code, many time you find some text between double underscore. You have defined too many custom constant in script. PHP provides number of predefined constants. Many of those are created by different extensions. If respective extension is available then only you can used that extensions constantly.

Following are some common predefined constants:

__CLASS__
	__DIR__
	__FILE__
	__FUNCTION__
	__LINE__
	__METHOD__
	__NAMESPACE__
	__TRAIT__

Example for __FILE__

// this is relative to the loaded script’s path it may cause problems when running scripts from different directories require_once(‘config/database.php’);

// this is always relative to this file’s path no matter where it was included from

require_once(dirname(__FILE__) . ‘/config/database.php’);

4) Register Shutdown Function

At times, we need to execute some code right before the script finishes running. For that we normally add code at the end of the script.

But in between code if exit() is called or if any fatal error or script is terminated by user, then at end of file code is not executed. Solution for this use register_shutdown_function(), your code will execute no matter why the script has stopped.

Example:

$start_time = microtime(true);
 	register_shutdown_function('my_shutdown');
	
	function my_shutdown() {
	    global $start_time;
	
	    echo "execution took: ". (microtime(true) - $start_time). " seconds.";
	}

5) Compressing Strings

When talking about compression, we usually think about files ZIP archives. It is also possible to compress long strings in PHP.

To compress string in php use gzcompress() and uncompress use gzuncompress().

Example:

$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 string:

$compressed = gzcompress($string);
	
	echo "Original size: ". strlen($string)."\n";
	Output:
	Original size: 800
	
	
	echo "Compressed size: ". strlen($compressed)."\n";
	Output:
	Compressed size: 418
	
	Uncompressed string:
	$original = gzuncompress($compressed);

Well I have shared some of functionalities that’s been overlooked by me. Hope these tips help you in your development. If you have such cool tricks please share. cmsMinds is a php development company who can assist you with your PHP based projects.

Posted in PHP

Quick Inquiry