Skip to main content

Hello World, Hello 2022!

I have to say, for someone being a blogger since 2005, it feels tremendously awkward to write a Hello World post in 2022.

Truth to be told, it’s been a while, literally years since I last posted something to this (English) blog. It meant to be used for advanced, programmers topics, because writing specific advanced technical topic to small Serbo-Croatian speaking readers felt like … well not like talking to the walls, but let’s say talking to really small bunch of people, part of local dev communities. So I decided, I’ll keep it separate, posting engineers topic to EN for broader public, and everything else to SR blog. Most of my fellow Serbian devs speak perfect English anyway. And it was fine.

But then it came 2017, I’ve moved to Germany and all of sudden had plenty of material for my Serbian (non) IT friends. How’s in Germany, how to find a job, how much are taxes, why the hell are rents so crazy expensive in Munich etc. And the good old Serbian blog was fully back in business.

But it’s been 5 years since then, and I think I’m done. Nothing more to say, nothing left that wouldn’t include politics, pollution, god know which reason why we left, and reopening that book would just … hurt.

So I thought, let’s start blogging again and let’s start fresh new. Because irony is, when I moved to Munich, I met plenty of new people and some of them I convinced to start blogging, while for some of them were already doing it, I successfully convinced them that Medium is not the right place to be. The right place is that fuzzy little warm page with domain name you picked on whatever tech stack you want, where you have everything, just everything under control!

So clean plate was initial idea – get rid of everything and start fresh new! But then I saw, I can’t just remove all those prehistoric posts. Maybe they are from 2008, maybe they are useless these days, but hey, they are part of me, an old chap trying to stay on top of everything in his late 40’s.

So damn. Let’s roll the damn thing over again, put shiny new things on the top and see how it’s gonna be.

* Traffic lights taken from my first website iteration in 2003, thank you Web Archive!

Curl HTTP Client 2.0

It’s been a while since I last updated my Curl HTTP Client class. That’s the class that we’ve been using for years now, for all kinds of site scrapping, bulk domain registration without API, … and even today we use it as a part of core in our brand new Payment system.

Since I had some spare time this weekend, I finally managed to merge some of updates we’ve created during all these years and put a class to github so I can do frequent updates regularly. Don’t worry, class has retained most of it’s previous functionality explained here so update to latest version shouldn’t cause any problem.

Here’s the GitHub page: https://github.com/dinke/curl_http_client. Feel free to use it in your own projects and send in your comments.

Validating an integer with PHP

When I started writing this post I wasn’t sure should I put this into programming category or fun … I mean, validating if passed variable is integer aka whole number, how hard can it be? It appears not really easy in PHP 🙂

First off some background, I needed to validate an array with db key values (like array(1,2,6,7,…n)), so I thought of using simple function like array_filter with callback, something like:

array_filter($foo_array, 'is_int');

where is_int is callback, calling built in is_int function for each array value in order to filter out non int values.

The problem is (yes after 10 years dealing with PHP I am aware of that), PHP doesn’t treat int and string numbers the same, so string ’42’ wouldn’t be recognized as integer.

is_int('42'); //false
is_int(42); //true

To make things more “interesting” for programmers, if you have integer form data like age, year or whatever, they will be sent trough $_POST/$_GET/$_REQUEST arrays as ‘string numbers’ (so string ’42’ not int 42).

There is nice function to deal with such things and it’s name is is_numeric … but it only checks if string is actually number, so float values will be evaluated to true as well. ctype_digit on the other hand is opposite of is_int, it will only return true if test variable is string number, so ’42’ would evaluate to true, but 42 to false.

ctype_digit('42'); //true
ctype_digit(42);//false

And to make things even worse, PHP is silently converting really big integers to float (those bigger than PHP_INT_MAX const) so guess what would you get for number like is_int(23234234234235334234234)? Yep, false 🙂

$var = PHP_INT_MAX;
var_dump($var++); //true
var_dump($var); //false it's float now!

Yeah I know, you could cast var to int and do is_int … or cast var to string and do ctype_digit … and other dirty hacks… but what if someone smart from PHP team had decided to let say add 2nd argument to is_int check so you can check for type in some kind of ‘non strict’ mode, so string ’42’ is actually evaluated as integer? Something like this in C I guess:

function is_int(int var, int strict = 1)
{
   //if strict is false evaluate string integers like '42' to true!
}

All in all (at least to me), the easiest way to validate whether a variable is whole number (aka string or integer string) is with regular expression. Something like this:

/**
 * Test if number is integer, including string integers
 * @param mixed var
 * @return boolean
 */
function isWholeNumber($var)
{
	if(preg_match('/^\d+$/', $var))
	{
		return true;
	}
	else
	{
		return false;
	}
}

Someone from PHP dev team should really consider fixing this.