Bifalgorithm post · Computer science one · Converting Units

How can PHP read and clean user input from standard input?

How can PHP read and clean user input from standard input? (Computer science one · Converting Units) — "The function fgets (short for file get string) using th…

Ideas in this flowchart

  • Lecture de l’entrée utilisateur avec fgets et STDIN
  • fgets(STDIN) lit tout ce que l’utilisateur saisit jusqu’à la touche Entrée et retourne cette saisie sous forme de chaîne.
  • trim supprime les espaces blancs au début et à la fin d’une chaîne, notamment le caractère de fin de ligne.
  • Un exemple complet affiche une demande, lit la saisie dans $a, puis applique trim.
  • Les instructions de lecture peuvent être combinées en une seule expression : $a = trim(fgets(STDIN));
  • L’appel à fgets est bloquant : le programme attend que l’utilisateur saisisse une valeur.
  • Après l’appui sur Entrée, le programme reprend, lit le tampon d’entrée standard et affecte la chaîne à $a.
  • L’entrée standard est non structurée, donc l’utilisateur peut saisir n’importe quelle valeur.
  • Une saisie non numérique alors qu’un nombre est attendu peut produire des résultats incorrects.
  • Des fonctions de conversion peuvent tenter de transformer la saisie en entier ou en nombre flottant.
  • La conversion garantit seulement le type de la variable résultante, et non la validité de la valeur saisie.
  • L’entrée standard n’est pas un mécanisme idéal pour lire des données, mais elle constitue un bon point de départ pour de petits programmes.
Feed/

How can PHP read and clean user input from standard input?

No cover

Channel

Computer science one

1 page

"The function fgets (short for file get string) using the keyword STDIN (Standard Input) returns, as a string, everything the user enters up to and including the enter key, interpreted as the endline character. To remove the endline character, you can use trim, which removes leading and trailing whitespace from a string. A full example is: printf(\"Please enter a number: \"); $a = fgets(STDIN); $a = trim($a); Alternatively, lines 3–4 could be combined into one: $a = trim(fgets(STDIN)); The call to fgets waits, referred to as “blocking,” for the user to enter input. The user is free to start typing, and when they are done, they hit the enter key, at which point the program resumes, reads the input from the standard input buffer, and returns it as a string value assigned to the variable $a. Standard input is unstructured, so the user is free to type whatever they want. If we prompt the user for a number but they enter non-numerical input, we may get incorrect results. We can use conversion functions to attempt to properly convert the values, although this only guarantees that the resulting variable is of the type we want, such as an integer or floating-point value. Standard input is not a good mechanism for reading input, but it provides a good starting point for developing a few simple programs."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.