Posts Tagged with "php"

Detect an Ajax request in PHP

Posted by Stanislav Furman on August 24, 2017

If you would like to use same PHP code to handle both AJAX and non-AJAX requests, here is a quick and simple trick that you can use to check if the incoming request is AJAX. For our trick we will use a HTTP header called HTTP_X_REQUESTED_WITH. It is supported by all modern browsers that support AJAX. Therefore, it should work in 99% of cases.

Continue reading

Regular expressions? What's that? Part 2.

Posted by Stanislav Furman on June 2, 2014

Thanks to nightbloos I can continue posting funny code samples that developers meet/write from time to time. :)

Here is another good example of how NOT to do! Please, do not try this at home! This stunt was performed by untrained professionals.  :)

$forReplace = array(",","."," ","-", "+", "#""/");
foreach($forReplace as $repl){
	$find = str_replace($repl,'',$find)

Obviously, in this case a regular expression function must have been used.

If you have another good examples of a funny code samples, please leave it in the comments. ;)

PHP NG, significant speed-up features coming in PHP 6

Posted by Stanislav Furman on May 15, 2014

Some exciting and promising coming changes in PHP 6 or 7 have been anounced recently by Dmitry Stogov from Zend. A detailed article has been postd here

Briefly, Zend is working on PHP NG (next generation) which will bring better performance and better memory management. According to Dmitry, the PHP application execution typically takes a significant part of the execution time dealing with memory allocations, and that affects PHP performance significantly as well.

I spent a significant amount of time experimenting with JIT, and even created a PoC of transparent LLVM based JIT compiler embedded into OPCache. The results on bench.php was just amazing – (0.219 seconds against 2.175 – *10 times speedup of PHP 5.5*), but on real-life apps we got just few percent speedup., - says Dmitry in his report.

According to his tests PHP developers can gain up to 20% more requests per second (in case with Wordpress for example).

So far it looks like upgrading to PHP NG should be painless (that's the idea). However, some of PHP extensions wil might require some "massage".

Looking forward to test the new PHP 6. Or maybe 7? ;)

Static vs Non-static methods in PHP: what is faster?

Posted by Stanislav Furman on May 4, 2014
What is faster? Comparison of static and non-static class methods in PHP.

Regular expressions? What's that?

Posted by Stanislav Furman on April 29, 2014
A funny example of some silly PHP code.

Backward version compatibility in PHP web application

Posted by Stanislav Furman on February 17, 2014
How to handle backward PHP version compatibility in your web application

PHP 5.5.5 has been released

Posted by Stanislav Furman on October 17, 2013
PHP 5.5.5 has been released ¶

Meet AMPPS - a good alternative for XAMPP and WAMP

Posted by Stanislav Furman on October 7, 2013
Meet AMPPS - a good free alternative for XAMPP and WAMP

Loose comparison in PHP. Example of breakable functionality.

Posted by Stanislav Furman on September 12, 2013
Example of breakable functionality using the loose comparison in PHP

Floating point comparisons or why prices need to be stored in cents

Posted by Stanislav Furman on September 5, 2013
Comparisons of floating point numbers and potential problem that may occur