Forcing PHP to Sort Like MySQL

If you ever have to do a case-insensitive array sort in PHP, you will eventually notice that the results don’t match the MySQL latin1_swedish_ci collation. They just aren’t the same. The difference comes from a set of six characters that fall between the upper-case and lower-case alpha characters of ISO-8859-1.

Specifically, [ \ ] ^ _ ` are the troublemakers. A simple example would involve sorting the phrases “Hello” and “[Hello]”. In MySQL, “Hello” comes first. In PHP, “Hello” comes last.

If this is driving you crazy, all you will need to do is trick PHP into using an upper-case sort instead of a lower-case sort.

$strings = ['hello', '[Hello]', 'Hello'];
usort( $strings, 'mysql_simulator' );
var_dump( $strings );

function mysql_simulator( $a, $b ) {
return strcmp( strtoupper( $a ), strtoupper( $b ) );
}

I hope this will save you some of the research in solving that pesky little difference.

Cannot Use Dynamic Class Name

It seems I’ve discovered a new kind of PHP error that isn’t documented anywhere on the web yet. Here is a simple example and explanation to help correct your code.

class MyClass {}
$obj = new MyClass;
$test1 = MyClass::class;    // Right
$test2 = get_class( $obj ); // Right
$test3 = $obj::class;       // Wrong

That last line in my code sample produces the following error in PHP 7.4.

Fatal error: Cannot use ::class with dynamic class name in […][…] on line 5

This is the PHP way of saying that you have to switch to the get_class() function when retrieving the fully qualified class name of an object.

Note: An existing RFC proposes to change this behavior in PHP 8, so this particular error message might not exist in some future versions.

Server Crippled by Updates Again

February update cycle again sent my server into a reboot loop, shutting down all services until I could diagnose the problem on site.

Following the same steps as in my previous post, I switched the boot choice to Safe Mode, and observed another boot failure. This time instead of getting into the weeds of troubleshooting the update system with a second Safe Mode boot, I decided to let the server go back to the normal boot mode, because some other websites have reported this as a good solution.

In this case, the failed Safe Mode boot followed by no other action did successfully restore the server.

After reviewing the Event Viewer logs, I could only find a repeated Event ID 1074, “TrustedInstaller.exe has initiated the restart”. KB2992611 and KB890830 both installed successfully before the loop, then KB4502496, KB2822241, and KB4537814 installed after the loop.

My current recommendation is to disable automatic updates for Windows servers and only perform update checks while on site. Also, run the update check twice in a row. The servicing stack update from December didn’t show up until after recovering from the reboot loop and then checking again for more updates.