Why Editor reformats my code

I suggest you to use htmlspecialchars() before sending content andhtmlspecialchars_decode() before showing content on page, here is functions that you need to copy/paste to your functions.php :

function wp_po9568($content)
{
    return htmlspecialchars($content);
}
add_filter('content_save_pre','wp_po9568');

And:

function wp_po5689($content)
{
    return htmlspecialchars_decode($content);
}
add_filter( 'the_content','wp_po5689');

WordPress by default comments out php tags.