<?php include 'factorial.php'; //incluimos el script php al inicio de la página ?> <!DOCTYPE html> <html lang="es"> <head> <title>Factorial con HTML y PHP</title> <meta charset="utf-8" /> </head> <body> <form action="Index.php" method="POST" name="miform"> <center> <input type="text" name="numero" placeholder="Ingrese un número"value="<?php if ($a!='' && is_numeric($a))echo $a;?>"/> <BR><BR> <input type="text" name="resultado" placeholder="Resultado" readonly="readonly" value="<?php iniciar($a); ?>"/> <BR><BR> <input type="submit" value="Factorial" /> </center> </form> </body> </html>
Factorial con HTML Y PHP
1. Crear un Script PHP "Index.php" e ingresar el siguiente código y guardar.
2. Crear un segundo Script PHP "factorial.php", es el que va a tener la función factorial.
<?php
$a=isset($_POST['numero'])? $_POST['numero']: ''; //capturamos la variable ingresada
function iniciar($dat){
if ($dat != ''){
if (is_numeric($dat)){ //si sólo hemos ingreado numeros ejecutamos
factorial($dat);//llamamos a factorial
}
else{ echo 'Ingrese sólo numeros.' ; }
}
}
function factorial($numero){
$f=1;
for ($i=1;$i<=$numero;$i++){
$f=$f*$i;
}
echo $f;
}
?>
En el siguiente video pueden ver cómo funciona.
LINK:
Suscribirse a:
Entradas (Atom)


No hay comentarios:
Publicar un comentario