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

Section:
Sub-section::
Languages:


protonalex
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Check whether haystack starts with needle.

function starts_with($haystack, $needle) {
	return $needle === '' || stripos($haystack, $needle) === 0;
}
protonalex
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Check whether value an array is not empty.

$arr = ['', 'apple', ''];

if (!empty(array_filter($arr))) {
    // do something
}
protonalex
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Small bash script to delete redis cache.

 redis-cli KEYS "username*" | while read key; do redis-cli DEL $key; echo $key; done
protonalex
  • Reputation: 26
  • Snippets: 21
  • Revisions: 1

Function to sort an array according to its keys which are floats casted as strings.

function sort_string_float($a, $b) {

	$a = (float) $a;
	$b = (float) $b;

	if ($a == $b) {
		return 0;
	}

	return $a < $b ? -1 : 1;

}

// use
uksort($l1_data, 'self::sort_string_float');
dfatt
  • Reputation: 260
  • Snippets: 53
  • Revisions: 7

In C++, C# etc. you know what will be returned from a method. No surprises such as getting a string instead of expected int.

Using PHP 7 you can finally specify return type!

class Post extends CI_Model {

	public function __construct() {
		$this->load->database();
		$this->load->helper(['url', 'date']);
	}

	/**
	 * Getting post by ID
	 * @param $id
	 * @return array
	 */
	public function get_post_by_id($id): array {
		$query = $this->db->where('id', $id)->get('post');
		return ! is_null($query->row()) ? $query->row() : [];
	}
}