当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP readline()用法及代码示例


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



相关用法


注:本文由纯净天空筛选整理自neeraj3304大神的英文原创作品 PHP readline() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。