Good PHP tips not everyone knows

Tram Ho

Hi guys, when I am wandering on some tech blogs, I have read the article with pretty good tips on PHP, I want to share it with everyone to learn together. 😄

1. Create a process that runs in the background

In the process of coding, there will be times when you need processes in the background to send mail or run batches, or simply handle the database, … use the exec function as follows:

=> This command will run the command $ cmd and output the variable $ outputfile, then write the process id to $ pidfile.

2. Delete the entire folder and its contents

It’s simple, but if possible, save it somewhere for quick use 😄

3. Log the error log to the file

Surely sometimes you will need to log to a file to debug the code, right? The short code below will help you log the time and error to the file for you 😄


4. Get a day list from the day name of the year and month

Regarding dates, here’s the function to get the array of dates in a month, by the name of the day:


5. Encrypt and decrypt passwords (Encrypt, Decrypt)

In your project, an essential function is to encode and decode the user’s password or any code that needs to be encoded right?

The code below uses PHP’s openssl_encrypt() and openssl_decrypt() to handle:


6. Create random Unique Key

A function is quite brief but very good for you need to create a unique key.

Although concise, not many people know how to use it 😄

7. Display number with suffix

You often see facebook has the feature to display count list users ex: 10K, right? Here is a small function to handle adding suffix after some:


8. Multiple array sorting

While code if there are many arrays that need sorting, you can use PHP’s array_multisort() function to make the code better:


9. Get the file size from the URL or file

PHP’s filesize() function allows you to get the size of the file so you can check the heavy or light file to give the appropriate data processing direction:

Note: Because PHP’s int () type has been signed and many platforms use 32-bit integers, some file system functions may return unexpected results for files larger than 2GB.

10. Small tip

Never used

which should use:

In the above example, the first method will call the counting function on each iteration for the loop, while in the second, the count function will only be called once.

=> If the array needs to count larger, the run time will be significantly reduced.




Hopefully the above tips will help you while working with PHP, please save it somewhere. 😄

Thank you for reading my article, hello!

Share the news now

Source : Viblo