News Feed
Jobs Feed
Sections




News Archive
feed this:

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

Michael Dowling:
Favor Hash Lookups Over Array Searches
March 21, 2014 @ 10:47:34

Michael Dowling has a recent post to his site comparing the performance of hash lookups versus array searches.

A common programming requirement is to match a string against a set of known strings. For example, let's say you were iterating over the words in a forum post and testing to see if a word is in a list of prohibited words. A common approach to this problem is to create an array of the known prohibited words and then use PHP's in_array() function to test if the string is found in the list. However, there's a simple optimization you can make to significantly improve the performance of the algorithm.

He includes two pieces of sample code - one showing the searching of an array using in_array and the other running an isset to locate a key. He points out that the in_array method is quite a bit slower than the hash (key) lookup and includes a benchmark script to prove it.The results are pretty clear, with the hash lookup coming in about 480% faster than the in_array. He also points out that as the size of the strings you're comparing grows, the performance of in_array drops even more.

0 comments voice your opinion now!
hash lookup search array inarray benchmark

Link: http://mtdowling.com/blog/2014/03/17/hash-lookups-over-array-search/

Nikita Popov:
The case against the ifsetor function
January 13, 2014 @ 09:22:52

In his latest post Nikita Popov aims to make a case against the introduction of the "ifsetor" function to be introduced into the PHP language. This function takes in a variable to find and, if found returns it. If not, it doesn't produce an error (or warning).

Recently igorw wrote a blog post on how to traverse nested array structures with potentially non-existing keys without throwing notices. The current "idiomatic" way to do something like this, is to use isset() together with a ternary operator. [...] Someone on /r/PHP pointed out that there is an alternative approach to this problem, namely the use of an ifsetor function.

He goes on to talk about by-reference argument passing, why requesting an undefined array index doesn't really throw an error and how writes don't have the same issues as reads. He then gets into his own issues around the "ifsetor" function, namely:

  • Creation of dummy values
  • No notices for nested indices
  • Null values treated as non-existing
  • Default is always evaluated
  • By-reference passing often forces a copy

He summarizes most of the issues in one statement - "there is way too much by-ref magic involved". He then looks at some of the ways that this could be helped but opts instead for something more like "get_in" as proposed by Igor.

0 comments voice your opinion now!
ifsetor getin array read write problem byreference

Link: http://nikic.github.io/2014/01/10/The-case-against-the-ifsetor-function.html

SitePoint PHP Blog:
Fun with Array Interfaces
December 09, 2013 @ 10:53:06

On the SitePoint PHP Blog a tutorial has been posted recently about having some fun with array interfaces via some of the functionality provided through the SPL (Standard PHP Library).

As a programmer who works with different languages every day, I find a lot of joy in learning how things are done differently in other languages and seeing if I can do the same in PHP. One thing I liked in particular in Python was how one can emulate features of native data types in custom classes. [...] I thought it would be nice if you could do the same in PHP on an instance of your custom classes and not only arrays. PHP lets us do this with array interfaces.

He illustrates his intent with some basic Python functionality and shows how to use various PHP interfaces to achieve a similar functionality. He talks about SPL interfaces like Countable, ArrayAccess and Iterator to make objects more useful in an array handling environment. His example uses the idea of a set of user's tweets (from Twitter) and shows the implementation of these three interfaces.

0 comments voice your opinion now!
array interface countable arrayaccess iterator tutorial

Link: http://www.sitepoint.com/fun-array-interfaces

Leve.rs:
Array Manipulation in PHP, the Correct Way
November 08, 2013 @ 10:08:04

On the Leve.rs blog there's a recent post showing what they consider is the right way to work with arrays in PHP. It shows three things you can do to use PHP's own functionality to more correctly perform some common array operations.

More often than not, when PHP developers debug others' code, the majority of each method is taken up by array manipulations. Spending this much time on array manipulations is a huge hassle when trying to understand any given method. In order to speed up the debugging process, it's important to start writing array manipulations the correct way, first. Here are a few examples of code I have either written myself or had to fix in the past. It is time every PHP developer knows how to manipulate arrays properly!

Their three tips relate to:

  • Merging Arrays
  • Array Value Manipulation (with array_walk)
  • Array Value Manipulation (append with the plus sign, "+")
0 comments voice your opinion now!
array manipulation correct merge value walk plus

Link: http://leve.rs/blog/correct-array-manipulation-in-php/

Chris Hartjes:
Data Providers and Arrays
October 28, 2013 @ 11:49:36

Chris Hartjes, testing guru, has a post talking about using arrays in data providers for your unit tests. More specifically about some odd behavior one developer was seeing in their tests.

I was asked a question on Twitter by Tex Morgan about a problem he was having with PHPUnit data providers. He was trying to pass in some data and kept wondering why PHPUnit was serializing the data instead of doing what he was expecting.

The issue (example code included) was in how the data providers are expecting the data to be returned. His test was expecting an array but the data provider was returning things incorrectly. As Chris points out, the provider should return an array of arrays. The fix is easy, but could be confusing to someone not used to this slightly unusual return format.

0 comments voice your opinion now!
data provider unittest array return value example

Link: http://www.littlehart.net/atthekeyboard/2013/10/26/data-providers-and-arrays/

Procurios Tech Blog:
Autocompleting a lot of parameters
October 16, 2013 @ 11:14:05

Pim Elshoff has a recent post on the Procurios tech blog looking at autocompletion on function calls and an alternative to the "too many parameters" problem.

Some methods have many parameters. Sometimes they start out like that, sometimes they grow like that over time. Even though a maximum of two parameters is preferable, configuration for a method that does a big thing is difficult. Take curl for example; curl has a lot of options and so several wrappers around curl have arisen to deal with configuring it in a more humane manner. How can we keep the clutter of many parameters as low as possible, while maintaining autocompletion?

He gives an example of a function that takes too many arguments and how it's difficult to read (and remember the right order/types to give). He does mention one way that's sometimes used - arrays - but you lose typing checks with that. His best recommendation is to use a fluent interface instead. Not only does it make it more readable but it also works with the autocompletion in most IDEs.

0 comments voice your opinion now!
autocomplete parameters suggestion array fluent interface

Link: http://tech.procurios.nl/archief/2013/10/11/Autocompleting-a-lot-of-parameters

SitePoint PHP Blog:
Collection Classes in PHP
September 30, 2013 @ 12:21:30

On the SitePoint PHP blog a new tutorial introduces you to collection classes in PHP, replacing the more basic array with something with a bit more power.

Applications frequently have objects that contain a group of other objects, and this is a great place to make use of collections. [...] A Collection class is an OOP replacement for the traditional array data structure. Much like an array, a collection contains member elements, although these tend to be objects rather than simpler types such as strings and integers.

He mentions some of the common problems with arrays (and the data they contain) and points out that the structure a "Collection" class wraps around it can help keep things sane. He includes an example of a basic collection class that adds/gets/deletes items from an internal (private) array. He fleshes out this class with code inside those methods and a few others: keys, length and keyExists.

0 comments voice your opinion now!
collection class oop array tutorial

Link: http://www.sitepoint.com/collection-classes-in-php/

Eric Ritz:
BOTW PHP Array Filtering (Book Review)
July 25, 2013 @ 10:35:30

In a new post to his site Eric Ritz shares a book review of Sam Hennessey's "PHP Array Filtering" ebook in his "Use Case" series.

This week I read "PHP Array Filtering" by Sam Hennessy. My intent was to read another book but in the middle of the week Mr. Hennessy asked me to check out his book. I don't know the author and we'd never spoken before, so I was surprised. I find it humbling when anyone asks my thoughts on anything programming related, so because of that - and since the book is a short read anyways - I decided to write about it today and write about my previous choice next Sunday.

Eric walks through some of the contents of the book noting that, because of the vast amount of array support in PHP, is probably a viable topic on its own for an ebook. He goes through the sections of the book, picking out specifically picking out the filtering section (main topic of the book and all). He also points out a few problems with the book, two things that were suggestions, not really errors. He recommends it for what it is, though - a reference book for those working with arrays.

0 comments voice your opinion now!
ebook review samhennessey array filtering

Link: http://ericjmritz.wordpress.com/2013/07/21/botw-php-array-filtering

PHPMaster.com:
Array Operators in PHP Interesting but Less Spoken
July 01, 2013 @ 10:56:43

On PHPMaster.com there's a new tutorial about array operators in PHP, more so how the usual operators interact with arrays.

Operators in PHP can be organized into seven different categories: arithmetic, assignment, bitwise, comparison, error control, execution, incrementing/decrementing, logical, string, array, and type operators. This article details working with array operators, but also covers how some of the other operators work when used with arrays.

They talk about the operators for:

  • array union (plus)
  • equality (double equals)
  • identity (triple equals)

They also talk briefly about some of the other PHP operators with arrays (like subtraction, multiplication and division) and some of the conversions that PHP does when they're used.

0 comments voice your opinion now!
array operator union equality identity other tutorial

Link: http://phpmaster.com/array-operators-in-php-interesting-but-less-spoken

Ben Ramsey:
Introducing Array_column() in PHP 5.5
March 21, 2013 @ 10:46:01

Ben Ramsey has a new post talking about a feature that will become available in the PHP 5.5.x release series of the language - the array_column function. This function will extract all values from an array matching a given key.

My original patch for array_column() was written for PHP 5.2, but it sat around collecting dust for many years, until April of last year, when PHP moved to git and GitHub. That's when it became easy enough to apply the patch and send a pull request, which I did. [...] My goal for array_column() was simplicity. Many implement the functionality in different ways, and many call the function by other names (such as "pluck"), but I wanted to keep it simple and recognizable.

He includes an example of some sample data and how the function could be used to pull out the "last_name" field from each of the records and return just those as an array. If you're interested in seeing the original proposal and the RFC that was created for it, you can see it on the PHP wiki.

0 comments voice your opinion now!
arraycolumn array function index feature language



Community Events











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


composer performance facebook introduction project component install hack release language podcast application example database framework package unittest security symfony2 hhvm

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