How-to articles, tricks, and solutions about PHP

curl posting with header application/x-www-form-urlencoded

To make a POST request using curl and include a header of Content-Type: application/x-www-form-urlencoded, you can use the following command:

Declaration of Methods should be Compatible with Parent Methods in PHP

In PHP, when a class inherits from another class, it is said to be a subclass or derived class.

Delete particular word from string

In PHP, you can use the str_replace function to delete a particular word from a string.

Deprecated: Function split() is deprecated. How to fix this statement?

The split() function is deprecated, which means it is no longer supported and should not be used in new code.

Detecting request type in PHP (GET, POST, PUT or DELETE)

In PHP, you can use the $_SERVER['REQUEST_METHOD'] superglobal to detect the request type.

Difference between 2 dates in seconds

In PHP, you can use the built-in function strtotime() to convert a date string to a Unix timestamp, which is the number of seconds since the Unix epoch (January 1, 1970).

Difference between require, include, require_once and include_once?

In PHP, require and include are two statements that are used to include a file in a PHP script.

Directory index forbidden by Options directive

This error message typically appears when you are trying to access a directory on a web server that does not contain an index.html or index.php file and the web server is configured to prevent users from browsing the directory.

Disabling Strict Standards in PHP 5.4

To disable strict standards in PHP 5.4, you can add the following line of code to your PHP script:

Display an array in a readable/hierarchical format

Here is an example of how you could display an array in a readable/hierarchical format in PHP:

Displaying the Error Messages in Laravel after being Redirected from controller

In Laravel, you can use the withErrors method to pass error messages to a view after a redirect.

Divide integer and get integer value

In PHP, you can divide two integers using the forward slash (/) operator.

Docker MYSQL '[2002] Connection refused'

The '[2002] Connection refused' error in Docker when trying to connect to a MySQL container typically indicates that the MySQL server is not running or is not accessible on the network.

Doctrine 2: Update query with query builder

To update an entry in a database using Doctrine's query builder, you can use the update method of the QueryBuilder class.

Does file_get_contents() have a timeout setting?

Yes, the file_get_contents function in PHP does have a timeout setting.

Does UTC observe daylight saving time?

UTC (Coordinated Universal Time) is a standardized time that does not observe daylight saving time (DST).

Doing HTTP requests FROM Laravel to an external API

To make an HTTP request from Laravel to an external API, you can use the HTTP client of your choice.

Downloading a large file using curl

To download a large file using curl, use the following command:

Efficiency of using foreach loops to clear a PHP array's values

Using a foreach loop to clear the values of an array in PHP is an efficient way to do so.

Elegant way to get the count of months between two dates?

There are multiple ways to get the count of months between two dates in PHP, but one elegant way is to use the DateTime class and the diff() method.

Eloquent model mass update

To update multiple records in the Eloquent ORM (Object-Relational Mapping) in Laravel, you can use the update() method on the query builder or on a model instance.

enable cors in .htaccess

To enable Cross-Origin Resource Sharing (CORS) in an .htaccess file, you can add the following lines:

Enable PHP Apache2

To enable PHP for Apache2 on Ubuntu, follow these steps:

Enabling/installing GD extension? --without-gd

To enable the GD extension in PHP, you will need to install the extension and then enable it in your PHP configuration file.