How to set HTTP header to UTF-8 using PHP which is valid in W3C validator

Use header to modify the HTTP header: header(‘Content-Type: text/html; charset=utf-8’); Note to call this function before any output has been sent to the client. Otherwise the header has been sent too and you obviously can’t change it any more. You can check that with headers_sent. See the manual page of header for more information.

What is ‘YTowOnt9’?

It seems to be a PHP-serialized empty array, base 64 encoded. $ base64 -D <<< ‘YTowOnt9’ a:0:{} $ php -r ‘var_dump(unserialize(base64_decode(“YTowOnt9”)));’ array(0) { } There are many scripts that serialize arrays of data. When the arrays have data, they vary greatly, so the Base64 encoded PHP-serialized values do too, but when they are empty they … Read more

Laravel – Eloquent or Fluent random row

Laravel >= 5.2: User::inRandomOrder()->get(); or to get the specific number of records // 5 indicates the number of records User::inRandomOrder()->limit(5)->get(); // get one random record User::inRandomOrder()->first(); or using the random method for collections: User::all()->random(); User::all()->random(10); // The amount of items you wish to receive Laravel 4.2.7 – 5.1: User::orderByRaw(“RAND()”)->get(); Laravel 4.0 – 4.2.6: User::orderBy(DB::raw(‘RAND()’))->get(); Laravel … Read more

Converting string to Date and DateTime

Use strtotime() on your first date then date(‘Y-m-d’) to convert it back: $time = strtotime(’10/16/2003′); $newformat = date(‘Y-m-d’,$time); echo $newformat; // 2003-10-16 Make note that there is a difference between using forward slash / and hyphen – in the strtotime() function. To quote from php.net: Dates in the m/d/y or d-m-y formats are disambiguated by … Read more

Tracking the script execution time in PHP

If all you need is the wall-clock time, rather than the CPU execution time, then it is simple to calculate: //place this before any script you want to calculate time $time_start = microtime(true); //sample script for($i=0; $i<1000; $i++){ //do anything } $time_end = microtime(true); //dividing with 60 will give the execution time in minutes otherwise … Read more

PHP – Check if two arrays are equal

$arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs. $arraysAreEqual = ($a === $b); // TRUE if $a and $b have the same key/value pairs in the same order and of the same types. See Array Operators. EDIT The inequality operator is != while the non-identity operator is … Read more

PHP append one array to another (not array_push or +)

array_merge is the elegant way: $a = array(‘a’, ‘b’); $b = array(‘c’, ‘d’); $merge = array_merge($a, $b); // $merge is now equals to array(‘a’,’b’,’c’,’d’); Doing something like: $merge = $a + $b; // $merge now equals array(‘a’,’b’) Will not work, because the + operator does not actually merge them. If they $a has the same … Read more