Php/docs/pht-queue.front

来自菜鸟教程
跳转至:导航、​搜索

pht\Queue::front

(PECL pht >= 0.0.1)

pht\Queue::frontReturns the first value from a queue


说明

public pht\Queue::front ( ) : mixed

This method will remove a value from the front of the queue (in constant time). Attempting to return the front value from an empty queue will result in an Error exception.

Caution Due to the fact that all values in a pht\Queue are serialised, extracting a value from the queue will require it to be deserialised. This can incur a noticeable performance hit if the inspection of the queue's front value is performed within a loop.


参数

此函数没有参数。


返回值

The value on the front of the queue.


范例

Example #1 Retrieving the front value of a queue

<?phpuse pht\Queue;$queue = new Queue();$queue->push(1);var_dump($queue->front());

以上例程会输出:


int(1)

Example #2 Retrieving the front value in a loop (bad example - don't do this)

<?phpuse pht\Queue;$queue = new Queue();$queue->push(array_fill(0, 2000, 0));for ($i = 0; $i < count($queue->front()); ++$i); // quadratic runtime

Example #3 Retrieving the front value in a loop (good example)

<?phpuse pht\Queue;$queue = new Queue();$queue->push(array_fill(0, 2000, 0));$front = $queue->front(); // create a separate variablefor ($i = 0; $i < count($front); ++$i); // linear runtime