'php' Category

  • Easy swapping PHP variables

    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]; }

  • 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) […]

  • 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  

  • Why using getArrayCopy and exhangeArray ?

    January 7, 2017

    Because that is what php are using in ArrayObject

  • Monty Hall Problem in PHP

    November 23, 2016

    define('HAS_PRICE', true); define('HAS_NO_PRICE', false); $tries = 0; $won = 0; $wonFirst = 0; $i = 0; $pick = 0; /** * Bottom line, you play against being wrong the first time */ while (++$i

  • PHP Games – Solution to The Impossible Bet

    April 8, 2016

    /** * 100 people put a number in box. * every one must find his box within 50 tries */ $allTries = 100000; $usedTries = $allTries; $players = 50; $tries = 25; $settings = []; $people = []; for ($i = 0; $i < $players; $i++) { $settings[$i] = $i; $people[$i] = $i; } $mostPeopleWon […]

  • Set php error_log dynamicaly in .htaccess

    September 20, 2015

    1. Open your .htaccess file and add this lines to be bottom. 2. Create your php.error.log file. php_value log_errors 1 php_value error_log ./php.error.log If your file is still empty, add in your php error_reporting(E_ALL); Simple as that

  • Creating issues in Jira for multiple users with Jira API

    September 12, 2015

    1. Creating Jira issue $jiraUsername = 'Jira login username'; $jiraPassword = 'Jira login password'; $jiraHost = 'http://my-jira-host.com/'; $data = array( 'fields' => array( 'project' => array('key' => 'PR'/* Project Key*/), 'summary'=> 'Issue title', 'description' => 'Some text here, no html tags', 'labels' => array('API', 'IvanGospodinow'/* Put the username so you know which one added it […]

  • Copy big MySQL database with php in 5 steps

    July 17, 2015

    1. Dump structure of the big database. 2. Open the sql file and cut the constraints. 3.  Import the structure to another database. 4. Run this code: /** * Sync from live database */ ini_set('memory_limit','512M'); $limit = 100000; $time = time(); $sleep = 1; $sleepEvery = 60; $link = new mysqli( “__HOST__”, “__USER__”, “__PASSWORD__”, “__DATABASE__” […]

  • Warning: Division by zero – easy way to get arround?

    September 26, 2014

    $sum = [0, 0, 0, 0]; echo count($sum) / array_sum($sum); // Warning: Division by zero $arraySum = 0.00001 + array_sum($sum); $number = count($sum) / $arraySum; echo round($number, 2); // Result is 0; Just add some small number to the one you are not sure that it will be bigger than zero. Easy as that.


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