在php中将用户输入分配给变量
在构建我的FirsPHP程序时,我面临的问题是将用户输入分配给一个变量 这是我的密码在php中将用户输入分配给变量,php,command-line-interface,Php,Command Line Interface,在构建我的FirsPHP程序时,我面临的问题是将用户输入分配给一个变量 这是我的密码 <?php printf("Enter student test mark"); $test = read_stdin(); function read_stdin() { $fr=fopen("php://stdin","r"); // open our file pointer to read from stdin $input = fgets($fr,1
<?php
printf("Enter student test mark");
$test = read_stdin();
function read_stdin()
{
$fr=fopen("php://stdin","r"); // open our file pointer to read from stdin
$input = fgets($fr,128); // read a maximum of 128 characters
$input = rtrim($input); // trim any trailing spaces.
fclose ($fr); // close the file handle
return $input; // return the text entered
}
printf("Enter student assignment mark");
?>
如何让用户输入数字并将其分配给测试变量
<?php
if(isset($_POST['submit']))
{
$test = $_POST['marks'];//assigning your input value
if(isset($test))
{
$fr=fopen("php://stdin","r"); // open our file pointer to read from stdin
$input = fgets($fr,128); // read a maximum of 128 characters
$input = rtrim($input); // trim any trailing spaces.
fclose ($fr); // close the file handle
$result = $input; // return the text entered
}
}
?>
<form action="#" method="post">
<input type="text" name="marks" placeholder="Enter student test mark">
<input type="submit" name="submit" value="submit">
</form>
<?php
if(empty($result))
{
}
else
{
echo ' Enter student assignment mark';
}
这是我为测试您的代码而制作的一个示例,您的代码似乎正在运行
将此命名为sample.php
[FILE]内/Users/xxxxxxxxx/Desktop
[DIR]
在Terminal.app下
cd [DIR] // cd /Users/xxxxxxxxx/Desktop
php [FILE] // php sample.php
sample.php
您必须使用PHP作为服务器端语言,它用于处理information@Bankzilla但我想构建简单的控制台程序,难道没有其他更简单的方法吗?就像在java中使用扫描器一样?php不是这样使用的,也许您正在寻找更多关于@Bankzilla的东西,php可以与CLI一起使用@Bankzillam一般来说对脚本是新手,我没有太多知识,我只想用纯php构建程序,不涉及其他语言。需要检查$test
的分配,它会在页面loadTry$test=上抛出警告!空($_POST['marks'])$_POST['marks']:空如果设置了,$test
则不进行测试。如果(!is_null($test))
则使用。原因是,用户仍然不能提交任何内容
<?php
function read_stdin()
{
$fr=fopen("php://stdin","r"); // open our file pointer to read from stdin
$input = fgets($fr,128); // read a maximum of 128 characters
$input = rtrim($input); // trim any trailing spaces.
fclose ($fr); // close the file handle
return $input; // return the text entered
}
printf("Enter 1st number:");
$num1 = read_stdin();
printf("Enter 2nd number:");
$num2 = read_stdin();
echo "Equation: ".$num1." + ".$num2." = ".($num1 + $num2)."\n";
?>
Enter 1st number :1
Enter 2nd number :2
Equation : 1 + 2 = 3