PHP Purchase Price – count final price for a purchase

Simple PHP class which counts final prices for a purchase and its product. It counts with VAT and with discount properly.


Some quick notes for the class

  • Product price is a price already with VAT
  • Tax is a % of VAT
  • Discount can be fix or percentage
  • If discount is fix, its value must be lower than product price
  • If discount is percentage, its value must be between 0 and 100

You need composer for auto-loading

composer install


Quick example how it works. I recommend to check PurchasePriceTest.php and its tests to get better picture what else can be done.

require_once './vendor/autoload.php';

$products_data = array(
        'price'          => '79.96',
        'tax_percentage' => 19,
        'quantity'       => 5,
        'discount'       => 20,
        'discount_type'  => 'fix'),
        'price'          => '67.79',
        'tax_percentage' => 19,
        'quantity'       => 10,
        'discount'       => 10,
        'discount_type'  => 'percentage'));

$purchase_price = new \PTX\PurchasePrice();
$total_price = $purchase_price->count_total($products_data);

 * array(
 *    'total' => 909.9,
 *     'subtotal' => 764.62,
 *     'tax' => array(
 *         19 => 145.28),
 *     'discount' => 167.8);

Download PTX-PurchasePrice

The project is on GitHub, download it from there.

Realtime online .htaccess tester

I can’t even count how many hours (days) I have spent over the .htaccess. Everyone know the situation that you are trying to set up .htacess and it doesn’t work exactly as you want. Especially when you are trying to set up some more complex rewrite conditions.

I searched over the internet and I found an easy, realtime testing tool for .htaccess.

It’s really worthy to try this. Not only that you will find e.g. the final url, but it tells you more information about each block of your .htaccess. E.g. certain rule was not applied because one of the condition was not met. And not only you will get this info, it even tells you which condition was not met.

I can’t imagine better tool for testing .htaccess online.


One day with iPhone

I am not a person who would change his mobile all the time or who is thirsty to have the latest model the market. I am from the old school and I still think that main job for a mobile is to handle calls and SMS.

If I remember correctly, I have had 2 mobile phones during last 7 years – Nokia E61i and BlackBerry 9000 Bold. I was happy with both of them, just they became very slow – especially the BlackBerry. But I love the normal keyboard so I carried on.

2 days ago my nephew brought old iPhone 4 and asked only 100 euro for it. So I decided to give him a try and see what these Apple guys have really done.

What I really miss is a keyboard. I am trying to use the keyboard on the screen, but fingers are maybe quite big and I made a lot of mistakes. On the other had I have to say that built in auto-correction works more than good. So by its help, my messages look correct.

What I love now is the fact I have all my favorite tools in my mobile – like Evernote, Harvest, Viber etc. when I installed Viber on BlackBerry, the mobile simply stacked and I was not able to use it.

So my opinion after first day with iPhone is that it’s not a bad phone and I will give him more change for couple of weeks and I will see later whether I will keep it or go back to BlackBerry.

Continuos Integration for PHP projects using Jenkins, Composer, PHPUnit

This morning I have found a great video tutorial how to set up Continuos Integration for PHP projects using Jenkins, Composer, PHPUnit. It makes it really easy.

You do not need BitBucket or hosting from Digital Ocean, like me I have my own Git server + own VPS servers. I really recommend it for everyone who loves to learn from videos (like me). It’s really worthy to spend these 40 minutes.

Simple CakePHP REST API application

I’ve got a task to create simple WebService which communicates over REST API with another applications. The whole idea behind is to have 2 applications

  1. Application which will provide information via REST API
  2. Application which will communicate with first one and present data to the end user

Believe or not, CakePHP can handle both applications.

WebService REST API

Simple REST application to return data about user. The application responses in JSON / XML style. Demo WS Application

Web Application

Simple Web Application which communicates with REST Application running on by using JSON / XML. Demo Application.

Pretty easy :).

SOLVED: PHPStorm – FTP: Could not list the contents of folder

I had a problem with FTP connection in my PHPStorm application. Each time I was trying to login to the remote server, I could see the content of all the folders there except public_html. So I could not upload directly from the IDE. This annoyed me quite a lot.


I was searching and most of the manuals / helps were saying to tick Passive mode in Advance Options for my server. Unfortunately this didn’t help me. After another 15 minutes I came across a help saying, that I have to tick also Always use LIST command. I followed the instruction and the problem became part of my history :D.

I was wrong – PHP IDE is useful

Couple of months ago I had a discussion with friend of mine about IDE like PHPStorm or Eclipse and simple tools like Sublime Text. I was telling him, that as PHP Developer – Sublime Text is fair enough for me and the IDE should be use by Java Developers or C++ Developers. But I didn’t see the point of using it for PHP development. I know all my classes, Sublime makes it easy to open new file by pressing Ctrl+P which gives you an extra easy way how to find whatever file you are looking for. Sublime came with great features which I used on daily basics etc.

So the questions for me was – do I really need a PHP IDE for my job? How I will learn all new hot-keys again – that’s always my biggest worry. Will the IDE give me some help with CakePHP applications? I made some research and I decided to try PHPStorm – not because it’s done by czech company ;).

After one month of using this tool, I must say I felt in love with it. It speeds up my ever day job and it gives me almost all the features that I had in Sublime Text. But the greatest thing is, that PHPStorm is fully customizable – so I could map my hot-keys from Sublime Text to do exactly same job in PHPStorm.

I’m going to use this for most of my development tasks from now on.

PHPUnit + CakePHP + PhpStorm 8

PHPStorm is a great IDE which I use everyday together with our platform build over CakePHP Framework.  CakePHP comes with his own CakeUnit Test, which is an extension of PHPUnit and makes unit testing even more easier.

If you need to enable CakePHP2 Unit Test in your PHPStorm IDE, I’d recommend to follow instructions presented here: Running CakePHP2 Unit Tests in PhpStorm.

If you are using PHPStorm 8.x, you must make a modification into cakeunit4phpstorm.php file.

// Just simply replace handleArguments(array $argv) function by this one 
protected function handleArguments(array $argv)

    $printer = null;
    if (isset($this->arguments['printer'])) {
        $printer = $this->arguments['printer'];
    $printer = new IDE_PHPUnit_TextUI_ResultPrinter($printer);
    $this->arguments['printer'] = $printer;
    $this->arguments['listeners'][] = new IDE_PHPUnit_Framework_TestListener($printer);

That’s all what you have to do and you can start enjoying unit testing in CakePHP.

PHP Operator precedence with example

I came across interesting issue during last days. I was ask to solve following task.

$a = 2;
$b = 3;
$c = 0;

if($a || $b & $c) {
    echo 'first ';

if($a || $b && $c) {
    echo 'second ';

if($a || $b AND $c) {
    echo 'third ';

Question 1: What will be the output?

The output of script above will be: first second

Question 2: Why?

The first two are very trivial. They will pass because $a = 2 so the rest of the condition will be forgotten.

The interesting part comes with conditions number 3. To be honest, I have seen this style maybe only once in my life during all my years of development. But this is exactly a good example for interview.

The solution is hidden in Operator precedence in PHP . Operators &, &&, || have much more priority than AND, OR. So PHP would transform the condition to this:

if($a || $b) {
    if($c) {
        return true;
    } else {
        return false;
} else {
    return false;

I hope it’s clear now and I hope no-one can trick you again ;)

PHP Snake game – will a snake survive?

I was put in front of this task:

Probably all of us remember a game called Snake, where you have to eat as many apples as you can. As you may guess your task is to write a program which determines whether our snake wins the game or it ends up eating itself. Snake moves along an infinitely large board. It can turn left, right or go straight. Also, the snake – when going straight – can eat an apple under condition it is in front of him. If such a scenario occurs our friend extends onto a cell where the meal took place. The initial snake length is 1.

For each test you should print YES if the snake survives the whole game without dying or otherwise print number of the step in which the snake bites itself.

PHP Code

Sample code

require_once './bootstrap.php';
use \Ptx\Snake;

$snake_obj = new Snake();
$moves_2_test = [

foreach($moves_2_test as $move) {
    try {
        $result = $snake_obj->move_snake($move);

        echo $move . ': ';
        echo ($result == -1) ? 'YES' : $result;
        echo "\r\n";
    } catch(\Ptx\SnakeException $e) {
        echo $e->getMessage();

Console application

We can also play this game by using PHP Cli (php from command line)

$ php snake.php FLERFF
Welcome to snake 1.0
F - Forward
L - Left
R - Right
E - Forward & Eat

FLERFF: Survived

Considered Notable by

Today I’ve got an email from saying, that the code was considered notable by them.
Notable packages are carefully hand picked by a site moderator. The moderator that picked your package wrote the following comment:

“Interesting PHP class that can simulate the moves of the snake game to determine if the snake would survive.”

Award nominee notification

Today I’ve got another email from saying:

This is a notification message to let you know that your package PHP Snake Game was nominated to the PHP Programming Innovation Award among all the classes approved for publishing in the PHP Classes site during the month of March of 2015.

If you want to vote for the package, you can do it here: (unfortunately you have to be registered user)

© 2015 Tomas Pavlatka

Theme by Anders NorenUp ↑