如何获取 php 中 switch 语句的用户输入?
How to get user input for switch statement in php?
如上所问,我正在尝试在我的 PHP 脚本中获取用户输入以执行进一步的任务。 PHP 中是否有任何方法可以像 C
中使用的 'scanf()' 那样获取用户输入
<?php
echo "Swapping Numbers </br> Please select the method: </br>";
echo "1. Using 3rd variable </br> 2. Using add/sub </br> 3. Using mul/div";
//read user input $choice
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>
STDIN
常量是预定义的,您可以立即使用。
获取用户输入
<?php
$line = trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN, "%d\n", $number); // reads number from STDIN
例如
<?php
echo "Enter a number: ";
fscanf(STDIN, "%d\n", $number);
switch ($number) {
case 1: echo "one\n"; break;
case 2: echo "two\n"; break;
case 3: echo "three\n"; break;
default: echo "I can't count that high\n";
}
查看 I/O docs 了解更多详细信息
您可以处理来自表单元素或 url 的用户输入。
在你的情况下你可以这样使用。
$choice = $_GET['choise'];
用户将这样输入
http://localhost/your_file_name.php?choise=option
比你应该使用你的开关
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>
你应该使用 readline() :
var_dump(readline('Enter number: '));
如上所问,我正在尝试在我的 PHP 脚本中获取用户输入以执行进一步的任务。 PHP 中是否有任何方法可以像 C
中使用的 'scanf()' 那样获取用户输入<?php
echo "Swapping Numbers </br> Please select the method: </br>";
echo "1. Using 3rd variable </br> 2. Using add/sub </br> 3. Using mul/div";
//read user input $choice
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>
STDIN
常量是预定义的,您可以立即使用。
<?php
$line = trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN, "%d\n", $number); // reads number from STDIN
例如
<?php
echo "Enter a number: ";
fscanf(STDIN, "%d\n", $number);
switch ($number) {
case 1: echo "one\n"; break;
case 2: echo "two\n"; break;
case 3: echo "three\n"; break;
default: echo "I can't count that high\n";
}
查看 I/O docs 了解更多详细信息
您可以处理来自表单元素或 url 的用户输入。 在你的情况下你可以这样使用。
$choice = $_GET['choise'];
用户将这样输入
http://localhost/your_file_name.php?choise=option
比你应该使用你的开关
switch($choice)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
echo "</br>You entered wrong choice";
}
?>
你应该使用 readline() :
var_dump(readline('Enter number: '));