Correction exercices les fonctions en php : Série 01
Correction exercices les fonctions en php : Série 01
-
Objectifs
-
Exercice 01
-
Énoncé
- Vous pouvez visualiser l’énoncé de l’activité
-
Solution
-
Exercice 02
-
Énoncé
- Vous pouvez visualiser l’énoncé de l’activité
-
Solution
-
Exercice 03
-
Énoncé
- Vous pouvez visualiser l’énoncé de l’activité
-
Solution
<!doctype html>
<html lang="fr">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Afficher le tableau multiplication</title>
<style>
fieldset {
border: 2px solid #dcdcdc;
border-radius: 10px;
padding: 10px 20px;
text-align: leftt;
width:50%;
}
legend {width:25%}
</style>
</head>
<body>
<div class="container">
</br>
<?php
//Définition de la fonction
function factorielle($nbr)
{
if($nbr === 0) // condition d'arret
return 1;
else
return $nbr*factorielle($nbr-1);
}
if(isset($_POST["submit"]) && isset($_POST["txt_nbrfact"])){
$nbrfactorielle=$_POST["txt_nbrfact"];
$resultatFactorielle=factorielle($nbrfactorielle);
}
?>
<fieldset class="border p-4">
<legend class="w-auto">Factorielle</legend>
<form class="form-block input-group-sm" method="post">
<div class="form-group row">
<div class="col-lg-12">
<label for="nbrligne" class="mr-sm-2">Saisir un nombre</label>
<input type="number" class="form-control mb-2 mr-sm-2" name="txt_nbrfact" value="<?php if(isset($_POST['txt_nbrfact'])) echo $_POST['txt_nbrfact'];?>">
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<label for="nbrcolonne" class="mr-sm-2">Affichage de résultat</label>
<input type="text" class="form-control mb-2 mr-sm-2" name="txt_resultat" disabled style="font-size: 30px;
color: #999;"
value="<?php if(isset($_POST['submit'])) echo $resultatFactorielle;?>">
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<button type="submit" class="btn btn-primary btn-block mb-2" name="submit">Calculer la factorielle</button>
</div>
</div>
</form>
</fieldset>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
<!doctype html>
<html lang="fr">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Tableau de conversion</title>
<style>
fieldset {
border: 2px solid #dcdcdc;
border-radius: 10px;
padding: 10px 20px;
text-align: leftt;
width:50%;
}
legend {width:50%}
</style>
</head>
<body>
<div class="container">
</br>
<?php
//Fahrenheit to celsius
function fahrenheit_vs_celsius($valeurSaisie)
{
$celsius=5/9*($valeurSaisie-32);
return $celsius ;
}
//Fahrenheit to kelvin
function fahrenheit_vs_kelvin($valeurSaisie)
{
$kelvin=fahrenheit_vs_celsius($valeurSaisie) + 273.15;
return $kelvin ;
}
//Celsius to fahrenheit
function celsius_vs_fahrenheit($valeurSaisie)
{
$fahrenheit=$valeurSaisie*9/5+32;
return $fahrenheit ;
}
//Celsius to kelvin
function celsius_vs_kelvin($valeurSaisie)
{
$kelvin=$valeurSaisie+273.15;
return $kelvin ;
}
//Kelvin to fahrenheit equation
function kelvin_vs_fahrenheit($valeurSaisie)
{
$fahrenheit=9/5*($valeurSaisie-273.15)+32;
return $fahrenheit ;
}
//Kelvin to celsius equation
function kelvin_vs_celsius($valeurSaisie)
{
$celsius=$valeurSaisie-273.15;
return $celsius ;
}
?>
<html>
<head>
<title>Temp. Conv.</title>
</head>
<body>
<fieldset class="border p-4">
<legend class="w-auto">Tableau de conversion</legend>
<form class="form-block input-group-sm" method="post">
<div class="form-group row">
<div class="col-lg-12">
<input type="radio" name="depart" value="fahrenheit"> Fahrenheit
<input type="radio" name="depart" value="celsius"> Celsius
<input type="radio" name="depart" value="kelvin"> kelvin
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<input type="text" class="form-control mb-2 mr-sm-2" name="valeurSaisie"
style="font-size: 30px;color: #999;"
value="<?php if(isset($_POST['btnConversion'])) echo $_POST["valeurSaisie"];?>">
<div class="form-group row">
<div class="col-lg-12">
<input type="radio" name="fin" value="fahrenheit"> Fahrenheit
<input type="radio" name="fin" value="celsius"> Celsius
<input type="radio" name="fin" value="kelvin"> kelvin
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<button type="submit" class="btn btn-primary btn-block mb-2" name="btnConversion">
Convertir le nombre</button>
</div>
</div>
<?php
if(isset($_POST['btnConversion']))
{
$first_temp_type_name=$_POST['depart'];
$second_temp_type_name=$_POST['fin'];
$valeurSaisie=$_POST['valeurSaisie'];
//Fahrenheit to celsius and kelvin
if ($first_temp_type_name=='fahrenheit')
{
if ($second_temp_type_name=='celsius')
{
$celsius=fahrenheit_vs_celsius($valeurSaisie);
echo "$valeurSaisie Fahrenheit = $celsius Celsius";
} elseif ($second_temp_type_name=='kelvin')
{
$kelvin=fahrenheit_vs_kelvin($valeurSaisie);
echo "$valeurSaisie Fahrenheit = $kelvin Kelvin";
} else
{
echo "$valeurSaisie Fahrenheit";
}
}
//Celsius to fahrenheit and kelvin
if ($first_temp_type_name=='celsius')
{
if ($second_temp_type_name=='fahrenheit')
{
$fahrenheit=celsius_vs_fahrenheit($valeurSaisie);
echo "$valeurSaisie Celsius = $fahrenheit Fahrenheit";
} elseif ($second_temp_type_name=='kelvin')
{
$kelvin=celsius_vs_kelvin($valeurSaisie);
echo "$valeurSaisie Celsius = $kelvin Kelvin";
} else
{
echo "$valeurSaisie Celsius";
}
}
//Kelvin to fahrenheit and celsius
if ($first_temp_type_name=='kelvin')
{
if ($second_temp_type_name=='fahrenheit')
{
$fahrenheit=kelvin_vs_fahrenheit($valeurSaisie);
echo "$valeurSaisie Kelvin = $fahrenheit Fahrenheit";
} elseif ($second_temp_type_name=='celsius')
{
$celsius=kelvin_vs_celsius($valeurSaisie);
echo "$valeurSaisie Kelvin = $celsius Celsius";
} else
{
echo "$valeurSaisie Kelvin";
}
}
}
?>
</form>
</fieldset>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
<?php
function alerte($ch)
{
echo "<script type=\"text/javascript\"> alert('$ch'); </script>";
return TRUE;
}
//Utilisation
if(alerte("Bonjour à tous")) echo "La fonction alerte() fonctionne bien";
alerte('RAAAAAAAAAAAAAAAAAA');
?>