Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrian Patterson
19,588 PointsPrompt for user input
Just a general question. How do you prompt the user for input in php? Like asking for their name. Then saying welcome so and so. Any help would be appreciated.
4 Answers
Greg Kaleka
39,021 PointsHi Brian,
I assume you're talking about console interaction, like the gets
method in Ruby.
You want the readline()
function in PHP. Works essentially the same way.
Example:
- On a Mac, run
php -a
in the terminal to open an interactive shell. - Type
echo "Hello, " . readline("What's your name? ");
- You will be asked "What is your name?", and then php will echo "Hello, [your name]"
Hope this answers your question!
Cheers
-Greg
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsQuoting stackoverflow:
You can't take input in the middle of php execution since it finishes before the page is actually shown to the user. However, you can get input using HTML and receive that using php
https://stackoverflow.com/questions/22277633/how-to-take-user-input-like-prompt-but-in-php
Brian Patterson
19,588 PointsSo is there no such thing as a gets method in PHP like there is in Ruby.
Greg Kaleka
39,021 PointsThere is an almost identical method in PHP! It's readline
in PHP. See my answer above. Henrik is right for web page display, but I don't believe Ruby's gets
works in the middle of web page display either. I assume you're talking about console interaction.
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsI don't think so - the only way to get a users input (like prompt()) is to make a form and then get the entered value (like the example in the link I posted.
Sharif Ahmed
11,629 Points<?php
$firstName = "Rasmus"; $lastName = "Lerdorf"; $fullName = "$firstName $lastName";
echo "$fullName was the original creator of PHP\n";
?>
The error message i get is 'Bummer: I do not see a period.' Any idea what's wrong guys???
Sharif Ahmed
11,629 PointsI missed a full stop at the end of the statement XD