Для получения полного доступа
зарегистрируйтесь.

Section:
Sub-section::
Languages:


samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

It's possible to shuffle array once a week without storing anything. I've found the solution at StackOverflow.

It's based on initializing random number generator which is used by shuffle() with a seed that's constant for each week. date('W') is a week number in a year which fits the purpose well. After shuffling is done RNG is put back to normal by calling srand() with no argument i.e. initilizing it with random seed.

srand(date('W'));
shuffle($array);
srand();
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

In many cases it's useful to know if environment uses Suhosin.

if (extension_loaded('suhosin')) {
   echo 'Installed as PHP extension';
} elseif (defined("SUHOSIN_PATCH")) {
   echo 'Patched into PHP';
} else {
   echo 'Not detected';
}
dignityinside
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Check whether haystack ends with needle.

function ends_with($haystack, $needle) {

	if ($needle === '') {
    	return true;
	}

	return strtolower(substr($haystack, -strlen($needle))) === $needle;

}
dignityinside
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Check whether haystack starts with needle.

function starts_with($haystack, $needle) {
	return $needle === '' || stripos($haystack, $needle) === 0;
}
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

Despite it's quite common requirement to assert that elements in two arrays are the same while order doesn't matter, it's not that obvious on how to do it in PHPUnit.

$array = ['b', 'a'];
$this->assertEquals(['a', 'b'], $array, '', 0.0, 10, true);
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

Since PHP 5.4 it's possible to get script execution time without prior recording of script initial timestamp.

echo microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
dignityinside
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Run the following command to reveal your IP address.

ifconfig eth0 | grep inet | awk '{ print $2 }'
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

In order to delete item from fragment cache in Yii 2.0 you need to form a key in a special way.

function deleteFragmentCacheByKey($key)
{
    return Yii::$app->cache->delete(['yii\widgets\FragmentCache', $key]);
}
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

A short and reliable way to detect if the script is run in Windows environment.

if (DIRECTORY_SEPARATOR === '\\') {
    // ...
}
samdark
  • Reputation: 349
  • Snippets: 57
  • Revisions: 5

There are cases when you need to make sure keys specified are in the beginning of an array in the exact order they are specified.

$data = [
    'orange' => 'orange',
    'apple' => 'tasty',
    'carpet' => 'old',
    'car' => 'fast',
];

$result = orderByKeys($data, ['car', 'carpet']);

Would result in:

$data = [
    'car' => 'fast',
    'carpet' => 'old',
    'orange' => 'orange',
    'apple' => 'tasty',    
];
function orderByKeys(array $array, array $keys)
{
    foreach ($keys as $k => $v) {
        if (!array_key_exists($v, $array)) {
            unset($keys[$k]);
        }
    }

   return array_replace(array_flip($keys), $array);
}