News Feed
Sections




News Archive
feed this:

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

Ilia Alshanetsky's Blog:
Performance Analysis of isset() vs array_key_exists()
March 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.
0 comments voice your opinion now!
isset arraykeyexists benchmark large array code


Brandon Savage's Blog:
Avoiding Notices When to Use isset() and empty()
September 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.

0 comments voice your opinion now!
avoid notice tutorial isset empty


DevShed:
The Isset and Unset Magic Functions in PHP 5
June 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).

0 comments voice your opinion now!
magicfunction unset isset tutorial


Debuggable Blog:
Supressing Errors in PHP
January 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.

0 comments voice your opinion now!
suppress error empty isset check exist shutup operator symbol


Brian Moon's Blog:
Null vs. isset()
January 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

0 comments voice your opinion now!
null value variable compare isset empty


Brian Moon's Blog:
in_array is quite slow
June 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.

1 comment voice your opinion now!
inarray compare array unset isset unique key execution time


Phil Thompson's Blog:
7 PHP functions that saved my life
January 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.

0 comments voice your opinion now!
handy function numberformat arrayvalues isset arraydiff


Phil Thompson's Blog:
7 PHP functions that saved my life
January 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.

0 comments voice your opinion now!
handy function numberformat arrayvalues isset arraydiff handy function numberformat arrayvalues isset arraydiff



Community Events





Don't see your event here?
Let us know!


language zendserver api unittest interview introduction deployment framework symfony voicesoftheelephpant community release opinion podcast library install bugfix package laravel series

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework