Trying to use undefined properties in PHP stdClass objects gives you a "warning" as of PHP 8 (it used to be a "notice" that, being realistic, you ignored).

One way to fix code that looks like the following is to use the new "null coalescing" operator (??):

$a = new stdClass;
$a->first = 1;

// This will give you a warning
$b = array($a->first, $a->second ?: NULL);

// This is fine
$c = array($a->first, $a->second ?? NULL);
Read more

When a development team is working on a PHP project that uses Composer with Git, it often happens that there are conflicts on the composer.lock file, on the "content hash" line.

To solve this, Composer provides a way to update the lock file from the installed packages, including the content hash value: composer update --lock. This does not update every package to the latest versions (as composer update would do).

Read more

PHP supports the "data: stream wrapper" natively, so you don't have to do ugly string manipulation to get, for example, the data from a string such as data://text/plain;base64,SSBsb3ZlIFBIUAo=. Instead, you can do this:

$string = 'data://text/plain;base64,SSBsb3ZlIFBIUAo=';
$source = fopen($string, 'r');
$destination = fopen('myfile.txt', 'w');

stream_copy_to_stream($source, $destination);

fclose($source);
fclose($destination);
Read more

Apparently, PHP can work just fine without loading a php.ini configuration file. I have no idea where it gets its default configuration from (for example, a value of 128M for memory_limit), but luckily phpinfo() will tell you where it's looking for a php.ini (search for "Configuration File" in its output), so you can just add the file in that path and it will get picked up.

Read more

There's a simple algorithm that uses the Bayes theorem that can be used to classify documents, using their text tokenized into individual words, into categories (e.g. tags on a website). The classifier needs to be trained with existing data, and then it will return which categories a new document probably belongs to.

Read more
Subscribe to PHP