Afficher/Cacher les sources

Comparaison binaire / Convertisseur décimal à binaire

N: M:
Code source :

<?php

# Tab length: 4 spaces

#############################################################
#                                                            #
# Binary comparison by JeromeJ (webmaster of Olissea.com)    #
#                                                            #
# Licence: If possible, just gimme some credits ^-^            #
#                                                            #
#############################################################

# TODO: Add namespaces?

define('DEFAULT_LANG''fr');

$available_langs = array('fr''en');

$langs = array(
    
'binaryComparison' => array(
        
'fr' => 'Comparaison binaire / Convertisseur décimal à binaire',
        
'en' => 'Binary comparison / Decimal to binary converter',
    ),
    
'mustBeIntegers' => array(
        
'fr' => 'N et M doivent être des entiers !',
        
'en' => 'N and M must be integers!',
    ),
    
'decimal' => array(
        
'fr' => 'Décimal',
        
'en' => 'Decimal',
    ),
    
'binary' => array(
        
'fr' => 'Binaire',
        
'en' => 'Binary',
    ),
    
'operator' => array(
        
'fr' => 'Opérateur %s',
        
'en' => '%s operator',
    ),
    
'showSources' => array(
        
'fr' => 'Afficher/Cacher les sources',
        
'en' => 'Display/Hide sources',
    ),
    
'sourceCode' => array(
        
'fr' => 'Code source :',
        
'en' => 'Source code:',
    ),
);

$lang = (isset($_GET['lang']) && in_array($_GET['lang'], $available_langs) ? $_GET['lang']:DEFAULT_LANG);

function 
d($msg# "d" for "display"
{
    global 
$langs$lang# TODO: Can global work into a namespace? I wish closures could be used with non lambda functions…
    
    
return $langs[$msg][$lang];
}

################### ACTUAL CODE

if(isset($_GET['n']) && ctype_digit($_GET['n'])) $n = (int)$_GET['n'];
else 
$n '';

if(isset(
$_GET['m']) && ctype_digit($_GET['m'])) $m = (int)$_GET['m'];
else 
$m '';

echo 
'<head>',
        
'<title>',d('binaryComparison'),'</title>',
        
'<meta charset="UTF-8" />',
    
'</head>',
    
    
'<div style="position:fixed; bottom:0; right:0; background-color: white"><a href="?',(isset($_GET['showSources']) ? '':'showSources#sourceCode'),'">',d('showSources'),'</a></div>',
    
    
'<h1>',d('binaryComparison'),'</h1>',
    
    
'<form action="?" method="get">',
        
'N: <input type="text" name="n" value="',$n,'" /> M: <input type="text" name="m" value="',$m,'" /> <input type="submit" value="Go !" />',
        
'<input type="hidden" name="lang" value="',$lang,'" />',
    
'</form>';

if(
$n !== '' XOR $m !== '') echo '<strong style="color: red">',d('mustBeIntegers'),'</strong>'# Something wrong happened!
else if($n !== '' && $m !== '')
{
    if(
$m $n)
    {
        
# Swapping n and m
        
        
$z $m# Using a tempory var
        
        
$m $n# m takes n's value
        
$n $z# n takes the old m's value
    
}
    
    
$n_bin decbin($n);
    
$m_bin decbin($m);
    
    
$diff mb_strlen($n_bin)-mb_strlen($m_bin);
    
    
# Note to myself: Idea: Make a class Obj2table?
    
    
echo '<table border="1" width="250px">',
            
'<tr>',
                
'<th width="50%">',d('decimal'),'</th>',
                
'<th>',d('binary'),'</th>',
            
'</tr>',
            
'<tr>',
                
'<td>',$n,'</td>',
                
'<td>',$n_bin,' (',mb_strlen($n_bin),' bit)</td>',
            
'</tr>',
            
'<tr>',
                
'<td>',$m,'</td>',
                
'<td>',str_repeat('0',$diff),$m_bin,' (',mb_strlen($m_bin),' bit)</td>',
            
'</tr>',
        
'</table><br />';
    
    
$and = ($n $m);
    
$or = ($n $m);
    
    echo 
'<table border="1" width="250px">',
            
'<caption>',sprintf(d('operator'), '& (and)'),'</caption>',
            
'<tr>',
                
'<td width="50%">',$and,' (dec)</td>',
                
'<td>',decbin($and),' (bin)</td>',
            
'</tr>',
        
'</table><br />';
    
    echo 
'<table border="1" width="250px">',
            
'<caption>',sprintf(d('operator'), '| (or)'),'</caption>',
            
'<tr>',
                
'<td width="50%">',$or,' (dec)</td>',
                
'<td>',decbin($or),' (bin)</td>',
            
'</tr>',
        
'</table>';
}

if(isset(
$_GET['showSources'])) echo '<div id="sourceCode" style="margin-top: 1em"><strong>',d('sourceCode'),'</strong><br /><br />',highlight_file(__FILE__),'</div>';

?>
1