How-to articles, tricks, and solutions about PHP

PHP Fatal error: Cannot access empty property

This error message typically occurs in PHP when you are trying to access a property of an object or variable that has not been initialized or does not exist.

Converting <br /> into a new line for use in a text area

In PHP, you can use the nl2br() function to convert line breaks (\n or \r\n) to <br /> tags.

Strict Standards: Only variables should be assigned by reference PHP 5.4

This error message is indicating that a value is being assigned to a variable by reference, which is not allowed in PHP version 5.4 or later

How to increment a number by 2 in a PHP For Loop

You can increment a number by 2 in a PHP for loop by using the increment operator (++) and adding 2 to it on each iteration.

UTF-8 problems while reading CSV file with fgetcsv

UTF-8 is a character encoding that supports a wide range of characters and is often used for handling multilingual text.

Run PHP Task Asynchronously

To run a PHP task asynchronously, you can use the pcntl_fork() function to create a new process that runs the task in parallel with the main script.

Get the first letter of each word in a string

You can use the preg_match_all function in PHP to match all the words in a string and then use the substr function to get the first letter of each word.

Laravel Eloquent - Attach vs Sync

Here's an example that demonstrates the difference between using attach() and sync() in Laravel Eloquent:

file_get_contents() Breaks Up UTF-8 Characters

The file_get_contents() function in PHP is used to read the contents of a file into a string.

Laravel - Using (:any?) wildcard for ALL routes?

In Laravel, you can use the (:any?) wildcard to match any URI for a specific route.

MySQL - How to select rows where value is in array?

You can use the IN operator to select rows where the value is in an array.

php exec() is not executing the command

There could be a few reasons why the exec() function in PHP is not executing the command as expected.

How do I install Composer on a shared hosting?

To install Composer on a shared hosting, you can use the following steps:

Laravel get name of file

In Laravel, you can use the getClientOriginalName() method to retrieve the original file name of an uploaded file.

Laravel Delete Query Builder

In Laravel, you can use the Query Builder to delete records from a database table.

Codeigniter: fatal error call to undefined function mysqli_init()

Here's an example of how the error might appear in a Codeigniter application:

PHP How to find the time elapsed since a date time?

You can use the DateTime class in PHP to find the time elapsed since a specific date time.

PHP how to go one level up on dirname(__FILE__)

You can use dirname(dirname(__FILE__)) to go one level up in the directory structure when using __FILE__ (which returns the current file's path).

PHP Fatal error: Call to undefined function curl_init()

This error message indicates that the PHP script is trying to use the cURL library, which is not installed or enabled on the server.

Interface or an Abstract Class: which one to use?

An interface defines a set of methods that a class must implement, but does not provide any implementation for those methods.

PHPUnit: assert two arrays are equal, but order of elements not important

You can use the assertEqualsCanonicalizing or assertEqualsIgnoringCase methods to compare two arrays and ignore the order of the elements.

Composer require local package

To require a local package in Composer, you can use the "path" option in the "require" section of your project's composer.json file.

In Twig, check if a specific key of an array exists

In Twig, you can use the exists function to check if a specific key of an array exists.

Redirecting to authentication dialog - "An error occurred. Please try again later"

This message typically indicates that there is an issue with the authentication process.

Guzzle 6: no more json() method for responses

In Guzzle 6, the json() method for parsing JSON responses has been removed.