Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Julien Pauli:
On PHP function calls
Jan 22, 2015 @ 09:58:39

Julien Pauli has a new post today sharing an interesting function optimization he found using the Blackfire execution profiler.

This blog post is a technical explanation of a PHP optimization found with BlackFire profiler into a PHP script. The related post is located here : http://blog.blackfire.io/owncloud.html

He found that a replacement of a call to strlen with an isset optimized the script by about 20%. It's not typical though, he explains. He points out that the optimization worked so well because the call was part of a loop. He gets into some of the "under the covers" details of why this speed boost happens and even includes the op code output showing the difference. He then starts getting deep into the internal code for PHP and walks through each step made in the evaluation of a string's length. He finishes the post looking at isset (not technically a function) and how it handles its data checking. He also includes information about opcode caching and how to best maximize its impact.

tagged: function call strlen loop isset internals opcode cache performance

Link: http://jpauli.github.io/2015/01/22/on-php-function-calls.html

Ilia Alshanetsky's Blog:
Performance Analysis of isset() vs array_key_exists()
Mar 06, 2012 @ 08:44:31

Ilia Alshanetsky has posted about a performance difference he's found between using the isset and array_key_exists functions in PHP to see if a value exists.

At Confoo I had an interesting conversation with Guilherme Blanco regarding the fact that in Doctrine 2 they had a performance issue due to usage of array_key_exists() and how it was significantly slower than isset(). His anecdotal example was that doing isset() took 0.5 seconds, while array_key_exists() for the same operation took 5 seconds! That seemed wrong [...] so, I've decided to do a quick benchmark using a 5,000 element array.

His benchmarking code is included - it just loads up a simple data set from a file of "words" and measures the microtime between the isset and array_key_exists calls. His results do show that isset is the faster of the two (by 2.5x) but it's still a super small micro-optimization that won't gain you much in the end.

The bottom line is that if your application does not need to distinguish between an array key that does not exist and one whose value happens to be NULL you should use isset() because it happens to be a little faster.
tagged: isset arraykeyexists benchmark large array code

Link:

Brandon Savage's Blog:
Avoiding Notices: When to Use isset() and empty()
Sep 23, 2009 @ 08:47:13

If you've ever been bothered by those pesky NOTICEs when running your code, you know that you can wrap evaluations or check things with an empty or isset call to make them go away. Brandon Savage has a new post that can help you decide which one to use when, though.

As developers, we want to develop code that never emits notices or warnings, and PHP gets a bit antsy when we develop code that utilizes uninitialized variables. Lucky for us, PHP makes it easy to test for these variables without getting these notices. [...] PHP (like most languages) evaluates a logical argument left to right. For an AND condition, both conditions have to be true; PHP stops evaluating if it finds any condition untrue.

He suggests that the case to use isset() is more when you just want to use another check in the conditional but don't want to be bothered if the variable isn't there. A call to empty(), however, also evaluates the contents of the variable if it exists. Be careful, though - empty() returns false if the value of the variable is false - so take care in your use and always test scripts with multiple values.

tagged: avoid notice tutorial isset empty

Link:

DevShed:
The Isset and Unset Magic Functions in PHP 5
Jun 02, 2009 @ 10:25:50

DevShed continues their series on the "magic functions" in PHP with this new tutorial, a closer look at the __isset and __unset functions.

Overloading properties is only one of the many useful things that can be accomplished with magic functions. It’s also possible to perform additional tasks when working with the "isset()" and "unset()" PHP functions, but in this case by using another set of complementary functions, called "__isset()" and "__unset()" respectively.

In the examples he shows how to use the functions to see if a property has been set or to unset it and remove it from the object (could be helpful when working with private class properties).

tagged: magicfunction unset isset tutorial

Link:

Debuggable Blog:
Supressing Errors in PHP
Jan 30, 2009 @ 11:14:58

Felix Geisendorfer has posted two new items to the Debuggable blog looking at suppressing errors in your applications - and no, that doesn't mean using the @ operator either.

As of late I am getting sick of some best practices I have taught myself. Never using the @-error suppressing operator quickly moving to the top of the list. Before you start crying out loud (I know you will), let me say this: I do not mean to encourage anybody to use the @-operator. Applying the practice herein introduced may result in permanent damage to your coding habits and could serve as a gateway behavior to writing shitty code.

He gives an example in the first post of a place where he failed to properly check to ensure an element existed before checking a element of it. The second post provides an interesting solution to the same problem - using empty on the element/subelement to check its existence.

tagged: suppress error empty isset check exist shutup operator symbol

Link:

Brian Moon's Blog:
Null vs. isset()
Jan 29, 2009 @ 09:34:59

In this new post to his blog, Brian Moon compares two things that, on the outside, might seem a lot alike but do have their differences under the hood - a null value and the isset function.

I am working with a newcomer to PHP and he asked me about setting a variable to null and how to check that. He had found some example or information that showed that setting a varaible equal to null would unset the variable. So, he was unclear if he could then reliably check if the variable was equal to null. Having avoided null like the plague in my years of PHP, I was not sure. So, I mocked up a quick script to see what the states of a variable are in relation to null.

His test verified that a variable, set equal to null will be found to be equal to null, will be set (isset) and will be found empty by PHP's empty

tagged: null value variable compare isset empty

Link:

Brian Moon's Blog:
in_array is quite slow
Jun 06, 2008 @ 09:36:47

Brian Moon had a problem - one of his cron jobs was lasting for much longer (hours!) than it should have been. He tweaked, tested and debugged the script and finally came down to a call to in_array, something he comments on as being "quite slow".

See, this job is importing data from a huge XML file into MySQL. After it is done, we want to compare the data we just added/updated to the data in the table so we can deactivate any data we did not update. [...] We then compared the two arrays by looping one array and using in_array() to check if the value was in the second array. [...] So, that was running for hours with about 400k items. Our data did not contain the value as the key, but it could as the value was unique.

He method, replacing the in_array call that had to do a full array scan for each time through the loop with an isset/unset combo on the unique key, changed the execution time down from about 4 hours to 0.8 seconds.

tagged: inarray compare array unset isset unique key execution time

Link:

Phil Thompson's Blog:
7 PHP functions that saved my life
Jan 22, 2008 @ 11:08:00

On his blog, Phil Thompson lists seven PHP functions that "saved his life" when developing his apps:

From time to time, I've struggled with minor pieces of coding for what seems like an age and then I've discovered PHP has a ready-made function whose express purpose seems to be to fix my exact problem. Today, I name and honour those PHP functions which saved my life, my career and my sanity.

Functions that made the cut include:

He has his own reasons for each - all of them being handy little functions that fill a specific niche in PHP's vast array of abilities.

tagged: handy function numberformat arrayvalues isset arraydiff

Link:

Phil Thompson's Blog:
7 PHP functions that saved my life
Jan 22, 2008 @ 08:12:50

On his blog, Phil Thompson lists seven PHP functions that "saved his life" when developing his apps:

From time to time, I've struggled with minor pieces of coding for what seems like an age and then I've discovered PHP has a ready-made function whose express purpose seems to be to fix my exact problem. Today, I name and honour those PHP functions which saved my life, my career and my sanity.

Functions that made the cut include:

He has his own reasons for each - all of them being handy little functions that fill a specific niche in PHP's vast array of abilities.

tagged: handy function numberformat arrayvalues isset arraydiff handy function numberformat arrayvalues isset arraydiff

Link:

Phil Thompson's Blog:
7 PHP functions that saved my life
Jan 22, 2008 @ 08:12:50

On his blog, Phil Thompson lists seven PHP functions that "saved his life" when developing his apps:

From time to time, I've struggled with minor pieces of coding for what seems like an age and then I've discovered PHP has a ready-made function whose express purpose seems to be to fix my exact problem. Today, I name and honour those PHP functions which saved my life, my career and my sanity.

Functions that made the cut include:

He has his own reasons for each - all of them being handy little functions that fill a specific niche in PHP's vast array of abilities.

tagged: handy function numberformat arrayvalues isset arraydiff handy function numberformat arrayvalues isset arraydiff

Link: