Exemple 1-3-2 «Écrire en chiffres romains» - Script PHP

Initiation à la programmation avec le langage PHP, §1 Premiers pas

Structure de contrôle conditionnelle
if (condition) {action;}

Le bouton permet d'exécuter le script PHP.

<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1.0">
<meta name="robots" content="NoIndex,NoFollow">
<title>Écrire en chiffres romains</title>
</head>
<body>
<?php
/*
	Données:
*/
$n = 1976;
/*
	Écrire en chiffres romains
*/
$n0=$n;
$rom='';
if($n >= 1000){
	$n=$n-1000; $rom=$rom.'M';
}
if($n >= 1000){
	$n=$n-1000; $rom=$rom.'M';
}
if($n >= 1000){
	$n=$n-1000; $rom=$rom.'M';
}
if($n >= 900){
	$n=$n-900; $rom=$rom.'CM';
}
if($n >= 500){
	$n=$n-500; $rom=$rom.'D';
}
if($n >= 400){
	$n=$n-400; $rom=$rom.'CD';
}
if($n >= 100){
	$n=$n-100; $rom=$rom.'C';
}
if($n >= 100){
	$n=$n-100; $rom=$rom.'C';
}
if($n >= 100){
	$n=$n-100; $rom=$rom.'C';
}
if($n >= 90){
	$n=$n-90; $rom=$rom.'XC';
}
if($n >= 50){
	$n=$n-50; $rom=$rom.'L';
}
if($n >= 40){
	$n=$n-40; $rom=$rom.'XL';
}
if($n >= 10){
	$n=$n-10; $rom=$rom.'X';
}
if($n >= 10){
	$n=$n-10; $rom=$rom.'X';
}
if($n >= 10){
	$n=$n-10; $rom=$rom.'X';
}
if($n >= 9){
	$n=$n-9; $rom=$rom.'IX';
}
if($n >= 5){
	$n=$n-5; $rom=$rom.'V';
}
if($n >= 4){
	$n=$n-4; $rom=$rom.'IV';
}
if($n >= 1){
	$n=$n-1; $rom=$rom.'I';
}
if($n >= 1){
	$n=$n-1; $rom=$rom.'I';
}
if($n >= 1){
	$n=$n-1; $rom=$rom.'I';
}
if($n0 < 1 or $n0 > 3999 or $n !== 0){
	echo "ERREUR: la donnée doit être un entier
		compris entre 1 et 3999<br>";
	$rom = NULL;
}
echo '<p>'
	.$n0
	.' = '
	.$rom
	.'</p>';
?>
</body>
</html>

Contact |  Accueil   >   PHP   >   Initiation