How to sort an array of associative arrays by value of a given key in PHP?

You are right, the function you’re looking for is array_multisort(). Here’s an example taken straight from the manual and adapted to your case: $price = array(); foreach ($inventory as $key => $row) { $price[$key] = $row[‘price’]; } array_multisort($price, SORT_DESC, $inventory); As of PHP 5.5.0 you can use array_column() instead of that foreach: $price = array_column($inventory, … Read more

New self vs. new static

will I get the same results? Not really. I don’t know of a workaround for PHP 5.2, though. What is the difference between new self and new static? self refers to the same class in which the new keyword is actually written. static, in PHP 5.3’s late static bindings, refers to whatever class in the … Read more

Change the maximum upload file size

You need to set the value of upload_max_filesize and post_max_size in your php.ini : ; Maximum allowed size for uploaded files. upload_max_filesize = 40M ; Must be greater than or equal to upload_max_filesize post_max_size = 40M After modifying php.ini file(s), you need to restart your HTTP server to use the new configuration. If you can’t … Read more

How to create custom helper functions in Laravel

Create a helpers.php file in your app folder and load it up with composer: “autoload”: { “classmap”: [ … ], “psr-4”: { “App\\”: “app/” }, “files”: [ “app/helpers.php” // <—- ADD THIS ] }, After adding that to your composer.json file, run the following command: composer dump-autoload If you don’t like keeping your helpers.php file … Read more

How do I catch a PHP fatal (`E_ERROR`) error?

Log fatal errors using the register_shutdown_function, which requires PHP 5.2+: register_shutdown_function( “fatal_handler” ); function fatal_handler() { $errfile = “unknown file”; $errstr = “shutdown”; $errno = E_CORE_ERROR; $errline = 0; $error = error_get_last(); if($error !== NULL) { $errno = $error[“type”]; $errfile = $error[“file”]; $errline = $error[“line”]; $errstr = $error[“message”]; error_mail(format_error( $errno, $errstr, $errfile, $errline)); } } … Read more

htmlentities() vs. htmlspecialchars()

htmlspecialchars may be used: When there is no need to encode all characters which have their HTML equivalents. If you know that the page encoding match the text special symbols, why would you use htmlentities? htmlspecialchars is much straightforward, and produce less code to send to the client. For example: echo htmlentities(‘<Il était une fois … Read more