All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Take Input From User In PHP Without Form

Last Updated : Mar 11, 2024

How To Take Input From User In PHP Without Form

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;
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we defined two variables $inp1, $inp2 and using list method we make them together.
  4. 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.
  5. 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:’.
  6. Finally we displayed all collected inputs on console. For seeing result we need to use command prompt.
  7. 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’.
  8. 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.

Conclusion :-

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.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪