How To Take Input From User In PHP Without Form
Last Updated : Jan 1, 2023
IN - PHP
In this tutorial we will show you the solution of how to take input from user in PHP without form, here we using readline() method for ask input from user without help of form. The readline() is a built-in function in php that enables us to read input from the console.
The function accepts an optional parameter that contains text to display.
Then we using explode() method for splitting inputs with delimiter and result printed on console.
Step By Step Guide On How To Take Input From User In PHP Without Form :-
In php when we need to ask input from user without form help then we need to use command prompt to run and get input from user.
Here we defined list() method with two variables for storing user given input values and explode() method will identify user input by space delimiter.
Then we assigning inputs types, variable $inp1 type is integer and variable $inp2 type is float.
We can also ask input with instruction also so we asking input with message ‘Enter your name’ in readline() method finally we displaying users all inputs on console using echo() method.
<?php list($inp1,$inp2)=explode(' ',readline()); $inp1=(int)$inp1; $inp2=(float)$inp2; $name=(string)readline('Enter Your Name: '); echo "Hello ".$name." Your integer input is: ".$inp1. " and floating point is ".$inp2; ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
- Here we defined two variables $inp1, $inp2 and using list method we make them together.
- As we seen readline() method used to collect input from user, here we collecting both inputs at a time so we using explode method space delimiter identifies two inputs.
- We setting type for inputs $inp1 as int, $inp2 as float and variable $name type sets as string then using readline() method we asking input from user with message ‘Enter Your Name:’.
- Finally we displayed all collected inputs on console. For seeing result we need to use command prompt.
- We need to confirm few things before executes program those are, first we need to open our command prompt as admin and change directory path as our file destination then run command ‘php filename.php’.
- If you’re getting error when executing above command means we need to add environmental path to our file directory after that we need to restart command prompt then execute we won’t get any error.
In conclusion we are able to know how to take input from user using php.
First we need to start our xampp server then we need to run command prompt as admin and execute command ‘php filename.php’ then we need to give two inputs for variables $inp1, $inp2 and it is ask us ‘Enter Your Name:’ message there user need to provide string input after that we can get result of ‘Hello user_name Your integer input is: value1 and floating point is value2’ string on console.
I hope this tutorial on how to take input from user in PHP without form helps you and the steps and method mentioned above are easy to follow and implement.