How to Get Input Value From User in PHP?
In this tutorial, we Get Input Value From the User without using any HTML form and the GET and POST method.
Here are the steps which we follow to achieve the task,
- Creating a PHP script with
readline()
function. - Save it to your related path.
- Run using CMD in windows OS and Terminal in Linux OS.
readline() function: Returns the string from the user and ask for input from the user.
Live Demo to Get Input Value From User in PHP
Creating a PHP script with readline function
<?php
// Input section
// $a = 20
$a = (int)readline(‘Enter an integer: ‘);
// $b = 20.12
$b = (float)readline(‘Enter a floating’
. ‘ point number: ‘);
// Entered integer is 20 and
// entered float is 20.12
echo “Entered integer is “ . $a
. “ and entered float is “ . $b;
?>
Output:
Enter an integer: 20
Enter a floating point number: 20.5
Entered integer is 20 and entered float is 20.5
To achieve the task we have to use readline() function which helps to get the input from the user.
How to Run The PHP Code
- Save it to your server path (if local server C:\xampp\htdocs\test.php)
- Open your command line and go to the same path.
- Run using PHP command (
php filename.php
)
Conclusion
In this article, we check how we get user input in PHP without using HTML form. We use CMD or command line to run the PHP code with the prompt for the user to input the data.
We use readline() PHP inbuilt function to do the complete task.
You can know more about readline function here https://www.php.net/manual/en/function.readline.php
Happy Coding..!
Was this article helpful?
Originally published at https://phpcoder.tech on May 16, 2021.