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

Michelangelo van Dam:
Speeding up database calls with PDO and iterators
Jul 27, 2015 @ 13:45:28

In a post to his site Michelangelo van Dam shows you how to speed up database calls with PDO and iterators in a "no framework" environment.

When you review lots of code, you often wonder why things were written the way they were. Especially when making expensive calls to a database, I still see things that could and should be improved.

When working with a framework, mostly these database calls are optimized for the developer and abstract the complex logic to improve and optimize the retrieval and usage of data. But then developers need to build something without a framework and end up using the basics of PHP in a sub-optimal way.

He points out some of the common issues with a simple approach using just PDO and simple arrays including performance issues. Instead he recommends the use of iterators that wrap a PDO connection and allow for much simpler fetching and iteration of the found results. He includes code examples for a base iterator instance and a way to extend it to get the customized results. He also includes a few benchmarks showing the difference between a foreach loop and this iterator method.

tagged: database pdo iterator foreach benchmark compare

Link: http://www.dragonbe.com/2015/07/speeding-up-database-calls-with-pdo-and.html

PHPMaster.com:
Using SPL Iterators, Part 2
May 22, 2012 @ 13:23:17

On PHPMaster.com today they've posted the second part of the series covering the Iterators that come with PHP as a part of the SPL.

In part one of this series I introduced you to some of the SPL Iterators and how to use them. There may be times however when the provided iterators are insufficient for your needs and you’ll want to roll your own custom iterator. Luckily, SPL provides interfaces that will allow you to do just that. For an object to be traversable in a foreach loop, PHP requires that it be an instance of Traversable. You cannot however implement this interface directly (though you can use it in instaceof checks); instead you’ll need to implement either SPL’s Iterator or IteratorAggregate interfaces.

He shows you how to implement these two interfaces in your own custom classes, looping through a set of books for the Iterator example and a "getIterator" method that creates an ArrayIterator when executed. The results of both are used in foreach loops showing how they can be used just like any other iteratable variables.

tagged: spl iterators tutorial array iterator iteratoraggregate foreach

Link:

PHPMaster.com:
Learning Loops
Dec 15, 2011 @ 16:15:00

PHPmaster.com has a new introductory tutorial for those just starting out with PHP (or with programming really) talking about using looping structures for sets of data - for, while/do-while and foreach.

A significant advantage of computers is that they can perform repetitive tasks easily and efficiently. Instead of writing repetitive code you can write a set of statements that processes some data and then have the computer execute them repeatedly by using a construct known as a loop. Loops come in several different flavors in PHP: for, while, do-while, and foreach. I'll introduce you to each of them and show you how they can making repetitive tasks straightforward and easy to maintain.

The tutorial explains a use case for each of the loop types and includes a bit of sample code showing how to put it into practice. It also shares two special keywords that can be used to bypass or break out of your current loop - break and continue.

tagged: introduction tutorial loop dataset for while foreach break continue

Link:

Johannes Schluter's Blog:
References and foreach
Aug 20, 2010 @ 17:44:35

To reinforce a point he's made before (references in PHP are bad) Johannes Schluter has posted an example to his blog of a specific instance that causes an (expected) issue with references and foreach loops.

Now there is one use case which leads to an, at first, unexpected behavior which I didn't see as a real live issue when I stumbled over it at first, but then there were a few bug reports about it and recently a friend asked me about it ... so here it goes.

He show a code snippet of looping over an array with two foreaches and a print_r that shows the bug - the array changed from the original for no clearly apparent reason. To understand why this happens, he goes into detail on how variables are handled - complete with graphs.

tagged: references foreach array bug handling variable

Link:

Giorgio Sironi's Blog:
Stop writing foreach() cycles
Feb 18, 2010 @ 16:58:28

Giorgio Sironi has a recommendation for developers out there - stop writing foreach loops, there's something better in PHP 5.3+ - closures

There are some array functions which have already been supported at least from Php 4, and that take as an argument a callback whose formal parameters have to be one or two elements of the array. [...] In Php 5.3, callbacks may also be specified as anonymous functions, defined in the middle of other code. These closures are first class citizens, and are treated as you would treat a variable, by passing it around as a method parameter.

He includes some code examples to show you how closures used in callbacks can replace a lot of the other looping normally done by a separate bit of code. Most of the instances are in array functions that take in a callback and apply it to each element in the array (some recursively). The last example shows how to use it in a usort call to make the custom sorting of an array simpler.

tagged: foreach closure tutorial array callback

Link:

Leonid Mamchenkov's Blog:
Perl vs. PHP : variable scoping
Dec 12, 2008 @ 14:49:14

Leonid Mamchenkov has compared Perl versus PHP in this new blog post - specifically how they handle variable scoping.

I've mentioned quite a few times that I am a big fan of Perl programming language. However, most of my programming time these days is spent in PHP. The languages are often similar, with PHP having its roots in Perl, and Perl being such a influence in the world of programming languages. This similarity is often very helpful. However there are a few difference, some of which are obvious and others are not.

His example compares looping (a foreach in both) and how, after the Perl loop the $value variable is no longer accessible. In PHP, however, it's passed back out into the current scope and can be read just like any other variable. While this can be useful, it can also cause headaches when trying to track down elusive bugs.

tagged: variable scope foreach loop local outside compare

Link:

Felix Geisendorfer's Blog:
Sorting Challenge
Oct 26, 2007 @ 13:42:00

Felix Geisendorfer has a quick little sorting example posted today showing on way to sort a multi-dimensional array.

Quick challenge, lets say you have an array and you want to iterate through your products by [the key of each subarray in $products] Product.ordering ASC. Whats the fastest way to do this?

His solution involves using an array_flip call on the extracted information (using Set::extract), ordering it with ksort and pushing the values back into the $product array in the right order.

Check out the comments for more examples including ones that make use of array_multisort.

tagged: sort challenge arraymultisort arrayflip foreach array sort challenge arraymultisort arrayflip foreach array

Link:

Felix Geisendorfer's Blog:
Sorting Challenge
Oct 26, 2007 @ 13:42:00

Felix Geisendorfer has a quick little sorting example posted today showing on way to sort a multi-dimensional array.

Quick challenge, lets say you have an array and you want to iterate through your products by [the key of each subarray in $products] Product.ordering ASC. Whats the fastest way to do this?

His solution involves using an array_flip call on the extracted information (using Set::extract), ordering it with ksort and pushing the values back into the $product array in the right order.

Check out the comments for more examples including ones that make use of array_multisort.

tagged: sort challenge arraymultisort arrayflip foreach array sort challenge arraymultisort arrayflip foreach array

Link:

HowToForge.com:
Loops In PHP
Jan 30, 2007 @ 18:26:00

On the HowToForge website, there's a new tutorial that teaches one of the fundamentals of working with PHP (or any language for that matter) - looping.

Let's move towards our today's lecture which is about Loops. There are certain conditions in which you need to execute the same block of code again and again. For example if you want to print ten consecutive equal signs in three lines to make a separator then you could do it with different methods.

The tutorial covers:

  • Basic Concept Of Loops
  • Types Of Loops (for, while, do/while, and foreach)
  • Assignment
  • some Related Articles
Code examples are given for all, and a good overview of working with the loops is given.

tagged: loop tutorial beginner foreach for while loop tutorial beginner foreach for while

Link:

HowToForge.com:
Loops In PHP
Jan 30, 2007 @ 18:26:00

On the HowToForge website, there's a new tutorial that teaches one of the fundamentals of working with PHP (or any language for that matter) - looping.

Let's move towards our today's lecture which is about Loops. There are certain conditions in which you need to execute the same block of code again and again. For example if you want to print ten consecutive equal signs in three lines to make a separator then you could do it with different methods.

The tutorial covers:

  • Basic Concept Of Loops
  • Types Of Loops (for, while, do/while, and foreach)
  • Assignment
  • some Related Articles
Code examples are given for all, and a good overview of working with the loops is given.

tagged: loop tutorial beginner foreach for while loop tutorial beginner foreach for while

Link:


Trending Topics: