9 Useful PHP Functions and Features You Need to Know

9 Useful PHP Functions and Features You Need to Know

Significantly subsequent to utilizing PHP for a considerable length of time, we unearth capacities and highlights that we didn’t think about. A portion of these can be very valuable, yet underused. In view of that, I’ve assembled a rundown of nine unimaginably valuable PHP capacities and highlights that you ought to be comfortable with.

You may definitely realize that PHP permits you to characterize capacities with discretionary arguments. Yet, there is likewise a technique for permitting totally self-assertive number of capacity arguments.

Initially, here is a model with simply discretionary arguments:

Presently, how about we perceive how we can construct a capacity that acknowledges any number of arguments. This time we will use func_get_args():

Numerous PHP capacities have long and elucidating names. Anyway it might be difficult to determine what a capacity named glob() does except if you are now acquainted with that term from somewhere else.

Consider it like a more capable form of the scandir() work. It can let you look for records by utilizing designs.

You can fetch multiple file types like this:

Note that the files can actually be returned with a path, depending on your query:

If you want to get the full path to each file, you can just call the realpath() function on the returned values:

By watching the memory utilization of your contents, you might be capable advance your code better.

PHP has a garbage authority and a quite unpredictable memory chief. The measure of memory being utilized by your content. can go here and there during the execution of a content. To get the current memory utilization, we can utilize the memory_get_usage() work, and to get the most noteworthy measure of memory utilized anytime, we can utilize the memory_get_peak_usage() work.

For this, we will use the getrusage() work. Remember this isn’t available on Windows stages.

That may look somewhat obscure except if you as of now have a system organization foundation. Here is the clarification of each worth (you don’t have to retain these):

  • ru_oublock: square yield activities
  • ru_inblock: square information tasks
  • ru_msgsnd: messages sent
  • ru_msgrcv: messages got
  • ru_maxrss: most extreme occupant set size
  • ru_ixrss: vital shared memory size
  • ru_idrss: vital unshared information size
  • ru_minflt: page recovers
  • ru_majflt: page deficiencies
  • ru_nsignals: signals got
  • ru_nvcsw: willful setting switches
  • ru_nivcsw: automatic setting switches
  • ru_nswap: trades
  • ru_utime.tv_usec: client time utilized (microseconds)
  • ru_utime.tv_sec: client time utilized (seconds)
  • ru_stime.tv_usec: system time utilized (microseconds)
  • ru_stime.tv_sec: system time utilized (seconds)

To perceive the amount CPU power the content has devoured, we have to take a gander at the ‘client time’ and ‘system time’ values. The seconds and microseconds divides are given independently as a matter of course. You can separate the microseconds esteem by 1 million, and add it to the seconds esteem, to get the all out seconds as a decimal number.

How about we see a model:

Despite the fact that the content took around 3 seconds to run, the CPU use was incredibly low. Since during the rest activity, the content really doesn’t consume CPU assets. There are numerous different undertakings that may take constant, however may not utilize CPU time, such as hanging tight for plate tasks. So as you see, the CPU utilization and the genuine length of the runtime are not generally the equivalent.

Here is another model:

That took about 1.4 seconds of CPU time, practically which was all client time, since there were no system calls.

System Time is the measure of time the CPU spends performing system requires the bit for the program’s sake. Here is a case of that:

Presently we have a considerable amount of framework time utilization. This is on the grounds that the content calls the microtime() work ordinarily, which plays out a solicitation through the working framework to bring the time.

Additionally you may see the numbers don’t exactly signify 3 seconds. This is on the grounds that there were most likely different processes on the worker too, and the content was not utilizing 100% CPU for the entire span of the 3 seconds.

PHP provides useful magic constants for fetching the current line number (__LINE__), file path (__FILE__), directory path (__DIR__), function name (__FUNCTION__), class name (__CLASS__), method name (__METHOD__) and namespace (__NAMESPACE__).

We won’t spread every single one of these in this article, yet I will show you a couple of utilization cases.

When including other scripts, it is a good idea to utilize the __FILE__ constant (or also __DIR__ , as of PHP 5.3):

Using __LINE__ makes debugging easier. You can track down the line numbers:

There might be situations where you have to create a special string. I have seen numerous individuals utilize the md5() work for this, despite the fact that it’s not actually implied for this reason:

There is really a PHP function named uniqid() that is intended to be utilized for this.

You may see that despite the fact that the strings are remarkable, they appear to be comparative for the initial a few characters. This is on the grounds that the created string is identified with the worker time. This really has a decent reaction, as each new produced id comes later in sequential order request, so they can be arranged.

To lessen the odds of getting a copy, you can pass a prefix, or the subsequent parameter to build entropy:

This function will create shorter strings than md5(), which will likewise spare you some space.

Have you at any point expected to store an unpredictable variable in a database or a book record? You don’t need to concoct an extravagant answer for convert your clusters or items into designed strings, as PHP as of now has capacities for this reason.

There are two mainstream strategies for serializing factors. Here is a model that utilizes the serialize() and unserialize():

This was the local PHP serialization strategy. Be that as it may, since JSON has gotten so mainstream as of late, they chose to include support for it in PHP 5.2. Presently you can utilize the json_encode() and json_decode()functions too:

It is more smaller, and the best part is that good with javascript and numerous different languages. Be that as it may, for complex items, some data might be lost.

When discussing compression, we normally consider records, for example, ZIP files. It is conceivable to pack long strings in PHP, without including any file documents.

In the accompanying model we will use the gzcompress() and gzuncompress() capacities:

We had the option to achive practically half size reduction. Additionally the capacities gzencode() and gzdecode() achive comparative outcomes, by utilizing an alternate pressure calculation.


There is a capacity called register_shutdown_function(), which will let you execute some code directly before the content completes the process of running.

Envision that you need to catch some benchmark insights toward the finish of your content execution, for example, how long it took to run:

From the start this may appear to be paltry. You simply add the code to the base of the content and it runs before it wraps up. Notwithstanding, on the off chance that you ever call the leave() work, that code will never run. Likewise, if there is a lethal mistake, or if the content is ended by the client (by squeezing the Stop button in the program), again it may not run.

At the point when you use register_shutdown_function(), your code will execute regardless of why the content has stopped running:

Leave a Reply

Your email address will not be published. Required fields are marked *