How-to articles, tricks, and solutions about PHP

How can I print all the values of an array?

In PHP, you can use a foreach loop to iterate through an array and print each value.

PHP sort array alphabetically using a subarray value

In PHP, you can use the "usort" function to sort an array by a specific value of a subarray.

How check if a String is a Valid XML with-out Displaying a Warning in PHP

In PHP, you can use the simplexml_load_string function to check if a string is a valid XML without displaying a warning.

Adding Query string params to a Guzzle GET request?

To add query string parameters to a GET request using Guzzle, you can use the query option when creating the request.

Use different PHP version CLI executable for one command

You can use the alias command in your terminal to create a new command that runs a different version of PHP.

Add 30 seconds to the time with PHP

You can use the strtotime function in PHP to add a specified number of seconds to a given time.

PHP - setcookie(); not working

There are several reasons why the setcookie() function may not be working.

file upload php $_FILES undefined index error

The error "Undefined index: (name of the file input)" in PHP usually occurs when the form that is used to upload the file is not properly configured.

artisan migration error "Class 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver' not found"

This error message is indicating that the PHP extension for PDO MySQL is not installed or not enabled on your system.

How I can put composite keys in models in Laravel 5?

In Laravel 5, you can specify a composite key on a model by defining a protected $primaryKey property on the model and setting it to an array of the column names that make up the primary key.

Short unique id in php

In PHP, you can use the function uniqid() to generate a short unique ID.

TCPDF ERROR: [Image] Unable to get image

This error message is typically associated with the TCPDF library, which is a PHP class for generating PDF files.

Assets not referencing to public folder (Laravel)

In the Laravel framework, the "public" folder is the web server's document root and it's where assets such as CSS, JavaScript, and images should be stored.

php - Unknown: Failed opening required on line 0. laravel 5.6

This error message is typically caused by a problem with the file path specified in the "require" or "include" statement.

Proper Repository Pattern Design in PHP?

The Repository pattern in PHP is a design pattern that allows for the separation of the business logic of an application from the storage and retrieval of data.

Include constant in string without concatenating

In PHP, you can include a constant in a string without concatenating it by using double quotes (") instead of single quotes (') to define the string.

Unique and temporary file names in PHP?

In PHP, you can use the tempnam() function to generate a unique and temporary file name.

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.

How do you get php working on Mac OS X?

To get PHP working on Mac OS X, you can follow these steps:

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.

JSON_ENCODE of multidimensional array giving different results

JSON_ENCODE is a function that converts a PHP data structure, such as an array, into a JSON (JavaScript Object Notation) string.

can't write image data to path in laravel using the Intervention library

There could be several reasons why you are unable to write image data to a path in Laravel using the Intervention library.

Proper URL forming with a query string and an anchor hashtag

To form a proper URL with a query string and an anchor hashtag in PHP, you can use the http_build_query() function to create the query string and concatenate it to the base URL using the ?

Get name of caller function in PHP?

In PHP, the name of the caller function can be obtained using the debug_backtrace() function.