Catching multiple exception types in one catch block

Update: As of PHP 7.1, this is available. The syntax is: try { // Some code… } catch(AError | BError $e) { // Handle exceptions } catch(Exception $e) { // Handle the general case } Docs: https://www.php.net/manual/en/language.exceptions.php#example-294 RFC: https://wiki.php.net/rfc/multiple-catch Commit: https://github.com/php/php-src/commit/0aed2cc2a440e7be17552cc669d71fdd24d1204a For PHP before 7.1: Despite what these other answers say, you can catch AError … Read more

Simplest way to profile a PHP script

You want xdebug I think. Install it on the server, turn it on, pump the output through kcachegrind (for linux) or wincachegrind (for windows) and it’ll show you a few pretty charts that detail the exact timings, counts and memory usage (but you’ll need another extension for that). It rocks, seriously 😀

Remove a cookie

You May Try this if (isset($_COOKIE[‘remember_user’])) { unset($_COOKIE[‘remember_user’]); setcookie(‘remember_user’, null, -1, “https://stackoverflow.com/”); return true; } else { return false; }

Get the current script file name

Just use the PHP magic constant __FILE__ to get the current filename. But it seems you want the part without .php. So… basename(__FILE__, ‘.php’); A more generic file extension remover would look like this… function chopExtension($filename) { return pathinfo($filename, PATHINFO_FILENAME); } var_dump(chopExtension(‘bob.php’)); // string(3) “bob” var_dump(chopExtension(‘bob.i.have.dots.zip’)); // string(15) “bob.i.have.dots” Using standard string library functions is … Read more

Best way to initialize (empty) array in PHP

$myArray = []; Creates empty array. You can push values onto the array later, like so: $myArray[] = “tree”; $myArray[] = “house”; $myArray[] = “dog”; At this point, $myArray contains “tree”, “house” and “dog”. Each of the above commands appends to the array, preserving the items that were already there. Having come from other languages, … Read more

Explode PHP string by new line

Best Practice As mentioned in the comment to the first answer, the best practice is to use the PHP constant PHP_EOL which represents the current system’s EOL (End Of Line). $skuList = explode(PHP_EOL, $_POST[‘skuList’]); PHP provides a lot of other very useful constants that you can use to make your code system independent, see this … Read more

Insert string at specified position

$newstr = substr_replace($oldstr, $str_to_insert, $pos, 0); http://php.net/substr_replace In the above snippet, $pos is used in the offset argument of the function. offsetIf offset is non-negative, the replacing will begin at the offset’th offset into string. If offset is negative, the replacing will begin at the offset’th character from the end of string.

How can I sort arrays and data in PHP?

Basic one dimensional arrays $array = array(3, 5, 2, 8); Applicable sort functions: sort rsort asort arsort natsort natcasesort ksort krsort The difference between those is merely whether key-value associations are kept (the “a” functions), whether it sorts low-to-high or reverse (“r“), whether it sorts values or keys (“k“) and how it compares values (“nat” … Read more

JSON encode MySQL results

$sth = mysqli_query($conn, “SELECT …”); $rows = array(); while($r = mysqli_fetch_assoc($sth)) { $rows[] = $r; } print json_encode($rows); The function json_encode needs PHP >= 5.2 and the php-json package – as mentioned here NOTE: mysql is deprecated as of PHP 5.5.0, use mysqli extension instead http://php.net/manual/en/migration55.deprecated.php.