JSON Encoding of number problem

- Uncategorized -

You can get quite surprising results if you try to json_encode something like json_encode($n[“number”]=3.53).

echo json_encode($n["number"]=3.53);

Seems there is a bug in PHP, that parse floats with wrong precision.

Currently json_encode() uses EG(precision) which is set to 14.

The solution is to cast the number to string:

echo json_encode($n["number"]=(string) 3.53);

Another solution is to set the precision explicitly:

ini_set( 'serialize_precision', -1 );
echo json_encode($n["number"]=3.53);

Weird bug, that’s causing quite some headscratching.

You could also set it in php.ini:

serialize_precision = -1


How to format JSON in VIM/GVIM

- Tutorial -

The strength and beauty of VIM is the simple interaction with lots of external programs.

To format any JSON file in VIM/GVIM:

:% !jq .

% means the file you’re working on, and `!jq` is run the external command `jq`

That’s all.

You need to have `jq` installed:
sudo apt install jq
whatis jq
jq (1) - Command-line JSON processor


The ease and beauty of VIM

- Uncategorized -

Never thought that I would write a post with the ease of VIM in the title. But honestly I tried a lot of editors still coming back to VIM. VIM is a lot more hackable then Atom.

Probably because I like bash, python and javascript, while Atom is javascript hackable only.

VIM is not easy at first sight, and certainly not easy at first touch. Counterintuitive to the noob, all the difference modes VIM can be in, you somehow love it after a while.

Try the VIM editor

If you’re interested just try it out.
apt install vim

Run vimtutor after you installed VIM. I surely recommend the great VIM casts, of Drew Neil. Excellent learning stuff. Best to watch after you installed VIM and tried the VIMTutor.

Back to the title, the ease of VIM for productivity. Sure you need to extend it and use plugins. UltiSnips is one of my favorite.

Use snippets with UltiSnips

A good setup of snippets are a real time saver. There are pre-installed snippets, but you need to setup your own snippets, because they reflect how you think, and they should suit into your workflow.

For testing sites on Apache, or protecting a WordPress login from, you often use access control,  to restrict access to portions of your site based on the host address of your visitors.

You create a Deny Allow directive  for that in .htaccess.

order deny,allow
deny from all
allow from [your-ip]

Create a deny allow snippet

To speed up inserting earlier code in any .htaccess file your editing in VIM/GVIM, create a apache.snippets file in your UltiSnips directory (~/.vim/UltiSnips by default), and add this to the file:

snippet deny "Deny allow snippet" b
order deny,allow
deny from all
allow from `curl ipinfo.io/ip`

Done, every time you type `deny` hit tab and the snippet is inserted with the ip-address of the computer you’re working on. Through  `bash interpolation`, the snippets insert the output of the command `curl ipinfo.io/ip` which is the linux way of getting your ip-adress. Compare that to Microsofts Windows way. Never say again that Linux is not easy.

So, easy after all, isn’t it? That’s why I call VIM easy. Of course it isn’t actually, but there is beauty in the way it works, and the way you can make it work, fast en easy.


Creating a key for a new Lumen Micro Framework install

- PHP -

Lumen is the smaller but faster brother of the popular Laravel Framework. And faster means less features out of the box.

Starting a new Lumen framework is quite easy, once you have the Lumne installer globally installed. I can recommend that.

Install Lumen installer globally

composer global require "laravel/lumen-installer=~1.0"

Once you have installed a new Lumen framework, you actually have to do a few things extra:

  • copy the example.env to .env
  • generate an app key

With Laravel you can issue the command php artisan key:generate to generate the key, but Lumen doesn’t have that feature. Remember it’s smaller to be faster.

Of course you can do it manually, but let’s create a small shell script to do a few things manually.

A helper-script for a new Lumen install

if [ "$1" != "" ]; then

#setup default directory
cd /home/user/web

lumen new $1

cd $1

cp .env.example .env

key=`php -r "echo md5(uniqid()).\"\n\";"`;

sed -i "s/APP_KEY=SomeRandomKey!!!/APP_KEY=$key/" .env


Save the script as newlumen.sh in ~/bin, set the appropriate execute permission and you can install a new Lumen framework, by running:

newlumen.sh website

It will run all the mentioned steps above automatically:

  1. create a new Lumen install
  2. copy the .env file
  3. generate an application key in the .env file

Laravel 5.1 development with the Atom editor

- Linux, PHP, Tutorial -

The brand new Atom editor is heavily inspired by Sublime, build as an open source project on webtechnology, so CSS, JavaScript and HTML, Coffeescript, LESS and NodeJS, it’s founded by Github and really hackable into the next generation.

It sports a package installer, and a nice repository full of plugins, more then 2100 at the moment.

It just released version 1.0, for Linux, Windows and OS-X. Atom is free and open source.

In short I like it. It has good support for syntax-highlighting, code-completion and snippets. A code beautifier is available as package, and so is Emmet. Atom is more an editor than a full-blown IDE, like Netbeans, but packages are really easy to build, so I guess, a lot of extending is possible.

For Laravel you need some tweaks.

Improve Laravel support in Atom

Install some extra Laravel related packages:

  • language-blade
  • atom-laravel

There are more packages like blade-snippets, but actually you don’t need them. Snippets are include in the language-blade package, although the documentation lacks to mention that. The documentation just says:

Blade templating support in Atom

Actually it adds snippets too.

Make Atom act as a IDE

A great feature of Atom is the build in Tree View. Open a projectfolder, and Atom will show you a nice tree folder view.
But to browse through your files, use another feature. Atom will index all files. CTRL P will open an findAll dialog, or a fuzzy finder and that is the fastest way to change and find files in your project. Much like Sublime or Netbeans, or CTRLP plugin for VIM.

Another IDE feature Goto Declaration doesn’t seem to work. And that is true, it doesn’t work out of the box. That did confuse me and apparently others. It needs a CTAG file. CTAG files are created by another program Exuberant Ctags. Atom’s approach is much like the Unix philosophy. Don’t invent the wheel, or

  1. Small is beautiful.
  2. Make each program do one thing well.

Exuberant Ctags does something well, so don’t imitate it. Use it.

How to install Exuberant Ctags for Atom support

sudo apt-get install exuberant-ctags

Then CD into your working directory, the root of your project:

cd ~/project

Then run CTAGS to create the TAGS file:

ctags -R --languages=php

That’s it. Goto Declaration will now work on Laravel helper functions and classes.

Great. Netbeans lacks support for Laravel, and especially editing blade files is much easier in Atom with the language-blade package.

Atom isn’t the fastest editor, it’s isn’t optimized yet that much according to the developers, and it is build upon web technologies. That means it’s hackable and extendible for nearly everyone. For that it already got a sweet spot here and no doubt the community will build every package you can think of. It´s so easy and well integrated with Github. Bright future.