Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 PHP


  Resolve high CPU usage issue caused by file_get_contents in PHP

Sometimes a Linux server which runs Nginx + PHP-CGI(php-fpm) web service may experience sudden system load increase and the CPU usage is around 100% for many php-cgi processes when checking with top command. If this happens, file_get_contents may be the cause if it's used in the PHP script.In lots of web applications, normally there are lots of API requests based on HTTP. Many PHP developers like to use file_get_contents("http://example.com/") to get the API response because it's simple to use. But the problem of using file_get_contents is that it will block if the remote server responds ...

12,014 0       PHP FILE_GET_CONTENTS PHP-CGI


  Date interval add and sub prior to PHP 5.3

After PHP 5.3, we can use DateInterval object or date_interval_create_from_date_string() function to add or subtract days,weeks,months or years to or from a DateObject.But prior to PHP 5.3,we cannot do this. If we want to achieve the same effect. We need to use some skills. Here is one:We can use strtotime() function to achieve this. For example:$date=date('Y-m-d',time());$datestamp=strtotime(date('Y-m-d',strtotime($date)).' +1 day'); $datestamp=strtotime(date('Y-m-d',strtotime($date)).' +1 week');  $datestamp=strtotime(date('Y-m-d',strtotime($date)).' +1 month');  Her...

11,685 0       PHP CLASS DATEINTERVAL PHP 5.3 PHP 5.2


  A trap about PHP random number

The method to get random number in PHP is very simple, we only need to use rand() function.int rand ( int $min , int $max ) One function call can return the random number in a specified range. But in fact, the random number in computer is actually pseudorandomness, generally to increase the randomness, we may set a random seed before calling rand().void srand ([ int $seed ] ) According to other language features, we should pass a time value as a parameter to the srand() function, generally we may pass millisecond or microsecond. Starting from PHP 4.2, srand() will be automatically called whe...

11,565 0       PHP RAND SRAND MT_RAND


  New features in PHP 5.5

Just a few days ago, the PHP official website was redesigned. It seems we directly go from Web 1.0 era to Web 2.0 era. In addition to this new change, PHP 5.5 was also released. Some new features are added in this release. Here we summarize some of them.Enable OPCache by defaultWhen installing PHP 5.5, the Zend OPCache will be compiled as OPCache by default and OPCache is enabled by default.Some changes to the language itselfAdd Generatorfunction getLinesFromFile($fileName) { if (!$fileHandle = fopen($fileName, 'r')) { throw new RuntimeException('Couldn\'t open file "' . $fileName...

11,353 0       NEW FEATURES PHP 5.5 GENERATOR


  Handle ”cannot modify header information – headers already sent by”

Man PHP developers should encounter ”Warning: Cannot modify header information – headers already sent by ….” error before when executing a PHP script.Here are some solutions to this error.1. Blank lineCheck whether there is any blank line after , especially in the files which are used in include() and require(0, some problems are caused by blank lines.2. Add exit() after head()header (“Location: xxx”);exit();3. Use output cache<?php ob_start(); ?>… HTML codes …<?php… PHP codes …header (“Location: ….&rdquo...

11,292 2       HEADER SENT PHP ERROR HANDLE


  jsonSerialize() in extended class

Since PHP 5.4.0, there is a convenient interface JsonSerializable which can be used to indicate that a PHP object can be serialized to JSON object. It has only one method which should be implemented by any class which implements this interface. The method is named jsonSerialize().It's really easy to serialize an object to JSON object. Here is one example:<?phpclass ArrayValue implements JsonSerializable { public function __construct(array $array) { $this->array = $array; } public function jsonSerialize() { return $this->array; }}$array = [1, 2, 3];echo json_e...

10,321 0       JSON INHERITANCE JSONSERIALIZE EXTENDS


  Why cannot compare double values directly for equality?

A question in PHP: Get some value from the database and use floatval() to convert it to floatint point value, then compare it with another value which is also converted to floating point value with floatval(). These two values are the same when using var_dump() to output, the two outputs are float(8.87), they are the same. But when comparing them for equality, the result is weird., they are not equal. Why?To analyze this question, we need to start with PHP data types. PHP uses weak types. In PHP, floating point values are stored using C's double precision type, so we need to understand how dou...

10,286 0       PHP FLOATING PRECISION COMPARE EQUALITY


  How does PHP session work?

This article is about how PHP session works internally. Below are the steps :1. Session in PHP is loaded into PHP core as an extension, we can understand it as an extension. When session extension is loaded, PHP will call core functions to get the session save_handler, i.e interface or functions for reading and writing session data. By default, PHP will handle session data by writing and reading files on the server. But PHP also supplies custom methods for handling session data, we can use session_set_save_handler() to register the save_handler. At the same time, PHP will check whether session...

10,286 0       PHP SESSION MECHANISM