How to avoid global variables in PHP

19 Jun 2020 21:09 | Geplaatst door Marcel

In PHP variables are usually only available in the scope of a function or a class. For ‘easyness’ people sometimes (often?) revert to global variables. Variables can then become available in the global scope and can be pulled into any function where you might need it.

Problem is dat you might easily overwrite that variable. All you need is a typo like this:

And only because you use the = for assigning a value to the variable you have now overwritten it, while you only wanted to test it with ==.

A good way to not use global variables is to use static variables in a function that is both setter and getter. Like this function:

You can see here that if the variable $css_class_static was not set before, it gets assigned a value and saved as a static variable. The next time the function gets called, it is already set and it gets returned without having to calculate it again.
This function is both a setter (first time it’s called) and a getter (anytime it is called).
The advantage is that your variable is not in the global scope and is thus modularized and not easily overwritten.
Do pay attention to the fact that this static variable only exists during the same request, just like any global variable.

If you are using WordPress and want to go one step further you can even use the cache by using the wp_cache_add() and wp_cache_get() functions. Make sure to use keys that are unique to that data.
Also be aware that in most setups this data will only exist during the same request. Be sure to test this though, software like Memcached and Redis might be involved that make cache data survive requests.
Most software systems will have a cache system like this, and you might want to look into it.

In PHP variables are usually only available in the scope of a function or a class. For ‘easyness’ people sometimes (often?) revert to global variables. Variables can then become available in the global scope and can be pulled into any…
Read more...

Chess quote

06 Mar 2020 18:57 | Geplaatst door Marcel

Chess quote:

There are two moves you can make, one is good, one is bad. When you don’t look, you will play the bad move.

Chess quote: There are two moves you can make, one is good, one is bad. When you don’t look, you will play the bad move.
Read more...

Idea density in chess and programming

26 Oct 2019 19:20 | Geplaatst door Marcel

Ideas are a dime a dozen they say, and I can agree with that. Ideas can be cheap and easy, the execution is what matters. An idea can sound great in theory, but in practice it can turn out to be quite different.
Not every idea that gets thought of or spoken out loud will be executed. Most will remain not executed, and for some that might be better.

In chess most ideas can be executed. During a game you can think of hundreds of ideas. Inside the game itself you can execute some, and afterwards in the analysis all of them can be executed on the board, looked at and spoken about. You can talk them over with your opponent; what did you think here, how did you like this move, how would you respond, how do you see this position. You can also talk about the game with other people, be they clubmates or teammates. Positions are easy to put on the board, and the pieces are easy to make moves with and see a new position.
I would assume, the stronger of a chessplayer you are, the more ideas you can think of, make better decisions which ideas are the best, execute them on the board, and from the new position start again with that process. For stronger chessplayers this might cost less mental energy. I am only a clubplayer at a level of about 1500 ELO, and often in my games I see small series of weaker moves, where apparently I am not sharp enough.

I have wondered for some time about chess rating and people who program. I have the anecdotal experience that most people who play chess and are also programmer/developer, are more at clublevel, not master or grandmaster. Just today I thought of something that you could call idea density. In programming you can have maybe a hundred ideas on a day, but you can only execute maybe one, a few, but not often more than a handfull. I can imagine for a strong chessplayer, like around 2000 ELO and above, this can become a bit boring. They might enjoy seeing more ideas come to fruition, and choose a different kind of job that brings them more of what they enjoy and what they are good at.

Ideas are a dime a dozen they say, and I can agree with that. Ideas can be cheap and easy, the execution is what matters. An idea can sound great in theory, but in practice it can turn out to…
Read more...

Classic Stylo theme for PHP-Fusion 9

03 Oct 2019 12:35 | Geplaatst door Marcel

The Stylo theme was a classic desktop theme for PHP-Fusion version 7 and 8. PHP-Fusion 9 comes with themes that are following the modern responsive design. Some users prefer the classic desktop design of Stylo, and in my experience it is mostly users on mobile who prefer Stylo.

On Github I placed a version of Stylo that works on PHP-Fusion version 9.
Take a look at Github for Stylo.

The Stylo theme was a classic desktop theme for PHP-Fusion version 7 and 8. PHP-Fusion 9 comes with themes that are following the modern responsive design. Some users prefer the classic desktop design of Stylo, and in my experience it…
Read more...

Wallpaper with the bridge for trains in Zwolle

29 May 2019 22:35 | Geplaatst door Marcel

I could not find good pictures of the recent train bridge in Zwolle, so I decided to make some pictures. And yes, I do know it is seven years after building the bridge :)

I could not find good pictures of the recent train bridge in Zwolle, so I decided to make some pictures. And yes, I do know it is seven years after building the bridge :)
Read more...

Updates for the Jednotka WordPress theme

17 Feb 2019 13:27 | Geplaatst door Marcel

The Jednotka WordPress theme was for some time quite a popular theme for WordPress. However, the last update for it was published in December 2014, and there were no more subsequent updates. That basically meant it wasn’t compatible with PHP 7, and it broke down on anything that recent.

Up untill October 2018 it was still for sale on that Envato website. To me that was quite in bad taste. People would buy a theme that wasn’t even working on current PHP versions. Apparently that is how you make money, by cheating people out of it.

Last October I met someone at a local WordPress meetup who was using the Jednotka theme on het website, but had problems with it. A lot of admin pages broke down and the WordPress dashboard was quite useless.

I helped her with a few bugfixes and updates for the this theme that make it work with PHP 7.1 and 7.2. You can find that updated theme over at Github.

The Jednotka WordPress theme was for some time quite a popular theme for WordPress. However, the last update for it was published in December 2014, and there were no more subsequent updates. That basically meant it wasn’t compatible with PHP…
Read more...