Easy swapping PHP vairables

February 18, 2022

Using list() function to swap variables values without adding more variables.

$url = 'blog:5';
list($type, $id) = explode(':', $url);
// in case that the url is 5:block
if (!is_numeric($id)) {
    list($type, $id) = [$id, $type];

MySQL select rows where the sum of the rows is at least a value

January 7, 2020

Example table:

  (`id` int, `value` decimal(5,2))
  (`id`, `value`)
  (1, 5),
  (2, 1.25),
  (3, 3.7),
  (4, 17.3)

Goal – getting the rows in which the sum of value is 9.5

SET @counter := 0;

  IF(@counter <= 9.5, 1, 0) AS included,
  (@counter := @counter + a.value) AS valueSum

HAVING included = 1

The result:

id value included valueSum
1 5.00 1 5.00
2 1.25 1 6.25
3 3.70 1 9.95

Simple JavaScript seconds counter

November 19, 2019
 * @author Ivan Gospodinow
 * @site http://ivangospodinow.com/?p=471
 * @param {callback} onUpdate
 * @return {callback} onStop
export function countSeconds(onUpdate) {
    const start = new Date();
    const interval = setInterval(() => {
        onUpdate(parseInt((new Date().getTime() - start.getTime()) / 1000));
    }, 1000);

    return () => {


let seconds = 0;
let stopCounter = countSeconds(function(counter) {
  seconds = counter;

setTimeout(stopCounter, 5 * 1000);
// 2

Software and teams

October 16, 2019

“Big teams create big software slowly” by Kevlin Henney

How is Game of Thrones going to end ?

April 16, 2019

Everybody dies and winter covers all of Westeros.

PHP multidimensional array to form hidden inputs

March 22, 2019
 * Corvert array data to hidden inputs
 * to pass as form data
 * Example: [data => [sub1 => 1, sub2 => 2]]
 * @author Ivan Gospodinow
 * @see http://ivangospodinow.com/?p=455
 * @param array $array
 * @param [array | string] $exclude
 * @return string
function arrayToHiddenInput(array $array, $exclude = null)
    $hiddens = [];
    $strParams = urldecode(http_build_query($array));

    if (!empty($strParams)) {
        $params = explode('&', $strParams);
        foreach ($params as $param) {
            list($name, $value) = explode('=', $param);

            if (is_array($exclude) && in_array($name, $exclude)) {
            } else if ($name == $exclude) {

            $hiddens[] = sprintf(

    return implode(PHP_EOL, $hiddens);


echo arrayToHiddenInput(['data' => ['sub1' => 1, 'sub2' => 2]]);


Simple as that

Ethereum, Smart Contracts is a scam, sorry

December 2, 2017

Well, for 2 years online and zero working real live services, I have to call it – scam.

Code execution is limited to 15 seconds, which makes integration with 3rd party services not possible, thus falls most claims.


How Agatha Christie writes her novels

November 11, 2017

Minutes after we watched “Murder on the Orient Express” we started to think how the author of the book came up with such a brilliant ideas, not just for that story but all of her novels. Her stories are always so complex, you can never guess what will happen in the end.

Agatha Christie is one of the greatest crime authors with 66 detective novels, 14 short stories and longest-running play – a murder mystery“The Mousetrap”.

So we started talking, and came up with the idea that she probably read curtain newspaper stories and then wrote the story behind the titles of them. Which is quite brilliant because she used only the headlines and then wrote the story. Most of the authors focus on the story and the end came as a result of it. We started to realize how different and genius is to have the end and to let the imagination cover the rest. She did something more, she put her most famous character in the center of most of the stories – the detective Hercule Poirot. She wrote him as world-renowned private detective, unsurpassed in his intelligence and understanding of the criminal mind. Famous as much for his moustaches as his little grey cells. He became her label at the same time with herunique type of writing. To write the story after you have only the headline of something completely different happen somewhere, is brilliant.

Or this is just our theory.

In the end, we enjoyed the movie, (I read all of her books during the years) and the discussion after it were inspiring and fun.


credits: Antoniya Dyakova Gospodinova

Email: Suggestion and a idea inspired from last Peter Schiff and Joe Rogan podcast

September 4, 2017

Hello Peter Schiff,

Today I am thinking about what you said on the podcast and that Bitcoin is not backed with anything and does not have a real value (which is true), but what if Gold Money adds a service, where Bitcoin can be exchanged for gold.

Solution 1.
Gold Money cryptocurrency, which can be exchanged only for gold
– generating coins from mining and value is based on market exchange for gold
– generating coins based on the available gold, 1 gram = 1 Gold Money Coin
Solution 2
Account that can exchange Gold Money <> Bitcoin (available trough goldmoney.com)
Solution 3
Account that can exchange Gold Money <> USD, EUR… <> Bitcoin (available trough goldmoney.com)
Solution 4
Gold Money Wallet on top of Bitcoin, where each received Bitcoin is converted to gold when received and converted to Bitcoin when sent.

Thank you for your time and I hope to get your thoughts on this with reply to this email or in one of your podcasts.

Best regards,


Ivan Gospodinow,
Web Developer,
Web Consultant,


here it is, 26 days later : https://www.youtube.com/watch?v=QzYsO7N7YRc

Dynamic sleep for infinite loops based on cpu

May 11, 2017

Based on this function http://ivangospodinow.com/php-how-to-calculate-system-processors-load-in-percent/

we are getting current cpu load.

usleep(1000000 * systemLoadInPercent() / 100);

Result: 54.3% cpu load, sleep 0.543 sec


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /home/c2kblate/sites/ivangospodinow.com/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org