readline()function 是 PHP 中的内置函数,用于从命令行窗口读取用户输入。它提供了一种交互式提示用户输入并将其响应捕获为字符串的方法。
用法:
readline($prompt): string|false
参数:该函数接受一个参数,如下所述。
- $prompt: 这是一个可选参数。这是用户进入命令行接口时显示的字符串参数。
返回值: readline()函数返回提供的字符串。如果没有发现更多数据可读取,该函数将返回 false。
程序1:下面的程序演示了readline()函数。
PHP
<?php
// Simple example prompting the user for their name
$name = readline("Enter your name: ");
// Check if the user provided a name
if ($name !== "") {
echo "Hello, $name! Nice to meet you.";
} else {
echo "You didn't enter your name.";
}
?>
输出
Enter your name: Hello, ! Nice to meet you.
程序2:下面的程序演示了readline()函数。
PHP
<?php
// Function to ask a question and get user input
function askQuestion($question) {
return readline($question . " ");
}
// Main program
echo "Welcome to the Interactive Survey!\n";
echo "Please answer the following questions:\n";
// Ask the user some questions
$name = askQuestion("1. What is your name?");
$age = askQuestion("2. How old are you?");
$location = askQuestion("3. Where are you from?");
$hobby = askQuestion("4. What is your favorite hobby?");
$feedback = askQuestion("5. How do you like this survey?");
// Display the survey results
echo "\nSurvey Results:\n";
echo "Name: {$name}\n";
echo "Age: {$age}\n";
echo "Location: {$location}\n";
echo "Favorite Hobby: {$hobby}\n";
echo "Feedback: {$feedback}\n";
echo "\nThank you for participating in the survey!\n";
?>
输出:
Welcome to the Interactive Survey!
Please answer the following questions:
1. What is your name? GFG
2. How old are you? 20
3. Where are you from? India
4. What is your favorite hobby? ID
5. How do you like this survey? bored
Survey Results:
Name: GFG
Age: 20
Location: India
Favorite Hobby: ID
Feedback: bored
Thank you for participating in the survey!
参考:https://www.php.net/manual/en/function.readline.php
相关用法
- PHP readline_add_history()用法及代码示例
- PHP readfile()用法及代码示例
- PHP readdir()用法及代码示例
- PHP read_exif_data()用法及代码示例
- PHP realpath_cache_get()用法及代码示例
- PHP realpath( )用法及代码示例
- PHP realpath_cache_size()用法及代码示例
- PHP rename()用法及代码示例
- PHP rewind()用法及代码示例
- PHP restore_error_handler()用法及代码示例
- PHP restore_exception_handler()用法及代码示例
- PHP reset()用法及代码示例
- PHP rewinddir()用法及代码示例
- PHP require()和include()的区别用法及代码示例
- PHP require()和require_once()的区别用法及代码示例
- PHP round()用法及代码示例
- PHP rmdir()用法及代码示例
- PHP rad2deg()用法及代码示例
- PHP rand()用法及代码示例
- PHP random_int()用法及代码示例
- PHP range()用法及代码示例
- PHP rawurldecode()用法及代码示例
- PHP rawurlencode()用法及代码示例
- PHP rsort()用法及代码示例
- PHP rtrim()用法及代码示例
注:本文由纯净天空筛选整理自neeraj3304大神的英文原创作品 PHP readline() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。