Frequently Asked PHP Questions.

There are times when you want to know the best possible way to do a certain task in PHP. For example, you might want to check if a word or substring exists inside another string or you might be interested in getting the URL of current webpage.

We will be providing detailed answers to all the questions on this page. We will generally show you multiple methods of solving the same problem so that you can thoroughly understand the different approaches.

Best way to strip all whitespace characters in a string in PHP

Many times, the strings that you are manipulating in a PHP project will have extra spaces that need to be removed. For instance, you might want to replace multiple spaces with a single space or you might want to get rid of all the whitespace in a string. Similarly, you might be planning on stripping all the whitespace from either the left or the right end of a string. In this tutorial, you will learn about different techniques to do all that quickly and efficiently.

Best way to resize an image programmatically in PHP

If you are working on a website or project which deals with images, there will most probably come a time when you will have to programmatically resize those images. Maybe you have to create three different sizes of the same image to serve on different devices. Maybe you need to resize images that your users are uploading. Loading a 1920*1080 image when 96*54 image will do is just a waste of resources.

In this article, we will write different functions that will make use of the PHP’s GD library to resize images in PHP. GD has all the necessary functions to manipulate images so you won’t have to use any other 3rd party library.

Best way to check if a string starts with given word or substring in PHP

When looking for a word, character or substring inside another string, it is very common to check if the main string starts with given substring. Basically, every technique that worked when looking for a subtring inside another string will also work when we need to check if the substring only occurs at the beginning. However, this is a more specific situation and we can perform the check more efficiently using slightly different methods. In this tutorial, you will learn about different methods which can be used to check if a string starts with given word or substring.

Best way to remove or delete empty array elements in PHP

Many times, when working on a PHP project, you deal with an array which has some unwanted or empty array elements. This is especially true if you have no control over the values populated into the array. For example, you might have to store user input into an array and some of that input might contain unwanted empty strings. In this tutorial, you will learn about different methods which can be used to remove empty elements from an array. We will also learn how to remove other elements with a specific value.

Best way to remove first or last characters from a string in PHP

Every now and then, we have to deal with strings which contain unwanted characters either at their beginning or the end. PHP provides a lot of methods which can be used to remove these characters. Each of them has its advantages and disadvantages. In this tutorial, we will discuss all these methods in detail so that you can choose the one that works best for your situation.

Best way to calculate the difference between two dates in PHP

Calculating the difference between two dates is not as easy as it sounds. There are a lot of things that can go wrong if you are not careful. For example, you have to take both leap years and daylight saving time into consideration. Simply converting two dates to a timestamp and subtracting the values is not reliable in all cases.

Fortunately, the PHP DateTime class takes care of all this for us. In this tutorial, we will use the different methods from this class to get the difference between two dates.

Best way to check if a string contains a specific word or substring in PHP

Let’s say you have a string and and you want to check if it contains a specific word, character or substring. This can be done with the help of strpos(), regular expressions, strstr() and a few more functions. In this article, we will discuss the pros and cons of each of these methods in detail.

Best way to read a large file line by line in PHP

Every now and then, you will have to read and process a large file in PHP. If you are not careful when reading a large file, you will often end up exhausting all the memory of your server. However, there is no need to worry because PHP already provides a lot of inbuilt functions to read large files (by large I mean files with size over 1GB) either line by line or one chunk at a time. In this article, you will learn about the pros and cons of all these techniques.

Best way to delete an element from an array in PHP

Let’s say you have an array and you want to delete one or more elements from that array using PHP. Maybe you have to delete an element with a specific value or maybe you have to delete multiple consecutive elements. In this article, you will learn about different inbuilt PHP functions that can help you remove one or more elements based on their key or value.

Best way to get the first element of an array in PHP

At first glance, getting the first element of an array seems to be a simple job. However, getting the first array of any array without any knowledge of its keys is tricky. In this tutorial, you will learn about several methods that can help you get the first element of differet arrays without ever needing the keys.

Best way to generate a random alphanumeric string in PHP

The need to generate a unique random alphanumeric string of a given length is very common. You may need it to create a random username or filename. There are a lot of methods and functions like str_shuffle(), md5() and random_bytes() that can be used to generate a random string. In this article, you will learn about all the random alphanumeric string generation techniques in detail.

Best way to get the current date and time in PHP

Let’s say you are working on a project that requires you to get the current date and/or time in PHP. This can be done using a variety of functions like date() and time() or $_SERVER['REQUEST_TIME'].In this tutorial, you will learn about all these techniques to get the current date or time with or without timezones.

Best way to do a redirect to different url before page load in PHP

Often times, you are required to redirect a visitor from current page to another page for a variety of reasons. You can use a combination of PHP and JavaScript techniques in order to perform the redirect based on your needs. In this tutorial, we will discuss all these methods as well as their pros and cons in detail.

Best way to get the full url of current page in PHP

There are a lot of reasons to get the full URL of a page the user is currently visitng. You might use it to generate the title of your page or for creating social share links etc. In this tutorial, you will learn how to use the $_SERVER array in order to get the full URL of current page.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top