Created
July 19, 2019 02:37
-
-
Save parzibyte/07261ca1bb65aa2c37c1949fcd87b188 to your computer and use it in GitHub Desktop.
PHP getopt example https://parzibyte.me/blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Recuerda: | |
// Si tiene : entonces debe tener un valor | |
// Si tiene :: entonces su valor es opcional | |
// Si no tiene : ni :: entonces quiere decir que la opción es una bandera, no un valor | |
// Las opciones son: | |
// -d destinatario | |
// -a Asunto del correo | |
// -m El mensaje del correo | |
// -g Si se especifica la opción, el correo será guardado | |
$argumentos = getopt("d:a:m:g"); | |
// Las opciones deben estar establecidas | |
// Si no, salimos e indicamos el modo de uso | |
if ( | |
!isset($argumentos["d"]) | |
|| | |
!isset($argumentos["a"]) | |
|| | |
!isset($argumentos["m"]) | |
) { | |
exit("Modo de uso: | |
-d destinatario | |
-a Asunto del correo | |
-m El mensaje del correo | |
-g Si se especifica la opción, el correo será guardado"); | |
} | |
// Hasta aquí todas las opciones están bien establecidas | |
$destinatario = $argumentos["d"]; | |
$asunto = $argumentos["a"]; | |
$mensaje = $argumentos["m"]; | |
$guardar = isset($argumentos["g"]); | |
echo "Enviar correo" . PHP_EOL; | |
echo "Destinatario: $destinatario" . PHP_EOL; | |
echo "Asunto: $asunto" . PHP_EOL; | |
echo "Mensaje: $mensaje" . PHP_EOL; | |
echo "¿Guardar?: " . ($guardar ? "Sí" : "No") . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment