Check out the first post on passing variables in WordPress using WP_Query.

In the previous post, I shared how I used to use the following rule of them when passing variables in WordPress (and other programming languages, really):

“Only if I need to use this value more than once, then I will store it in a variable.”

And though I still think it’s a fair rule to follow, there are exceptions.

The more time I’ve spent writing code and having to maintain codebases from both myself and others, the more I’ve appreciated having variables defined – even if they are used only once – that hold more complex information for the sake of readability, clarity, and maintainability.

In the last post, I looked at an example as it relates to `WP_Query` since it’s one of the more powerful aspects of the WordPress API, but I’ve found that the same is true in simpler complex such as loops.

Passing Variables in WordPress

Though the last post covered passing variables specifically to part of the WordPress API, the same can be true for other language constructs.

And though these aren’t necessarily relegated only to WordPress, that’s where I spend the majority of my time working – obviously – so the examples are driven by my time spent when working in exactly that.

1. for Loops

Typically, `for` loops follow the convention of something like this:

Easy enough, right?

But there are ways to make this slightly more readable, and performant especially over larger data sets:

Obviously, this isn’t anything terribly complicated. For anyone who has learned programming either through books or in a class knows that loops are taught very, very early on in the material.

Yet, here we are discussing them. Perhaps that says more about me than anything else, though :).

2. foreach Loops

Anyway, `for` loops can only be optimized for readability so much, but PHP also offers the `foreach` construct which is often used more frequently, at least in WordPress-based code, than a vanilla `for` loop.

Personally, I find this format more readable because it defines two things:

  1. The name of the collection through which we’re iterating
  2. A definition for the current value of the element in the loop on which we’re working

Take, for example, the posts array that we’re using above, but look at it within the context of a `foreach` loop:

There’s very little difference, I know, but the fact that you can iterate through a collection in a more concise format with variables that are defined once, and that are a bit easier to read makes a big different, in my opinion.

This is especially useful when you’re working on objects for which you know their properties and can write a function that contains a loop to process each individual item of the array based on, say, some specific property.

But I digress on that for now.

You May Not Agree

Yes, this is relatively beginner stuff, but the fact that programmers still share ways in which they are finding ways to make code more readable, maintainable, and clear should transcend how simple (or complex) the given construct is.

I know – not everyone will agree.

After all, these are the types of discussions that programmers have had since programming has existed, and I personally think there are strong, viable cases for one strategy over the other on both sides of the argument.

In fact, the arguments for or against the stance are probably strong enough to where no ultimate conclusion on which is better can probably be reached, thus it’s left up to the environment’s coding standards to define what should be written within the context of said environment.

But the more code I’ve read, written, and maintained, the more I’ve appreciated single variable declarations in certain cases (not always, of course) where the data that they hold can be more difficult to read if defined in any other way.