Php 如果条件为真,则转到程序开始
我有这段代码,基本上如果函数“minmax”中的“if”语句为真,我希望程序从头开始。 我不知道如何实现这个逻辑,有人能帮忙吗Php 如果条件为真,则转到程序开始,php,Php,我有这段代码,基本上如果函数“minmax”中的“if”语句为真,我希望程序从头开始。 我不知道如何实现这个逻辑,有人能帮忙吗 <?php // user to enter the number of heads echo "Enter the number of heads: " . PHP_EOL; // the script will wait here until the user has entered something and hit ENTER $inhead = rea
<?php
// user to enter the number of heads
echo "Enter the number of heads: " . PHP_EOL;
// the script will wait here until the user has entered something and hit ENTER
$inhead = read_stdin();
// user to enter the number of legs
echo "Enter the number of legs: " . PHP_EOL;
// the script will wait here until the user has entered something and hit ENTER
$inleg = 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
}
//function to evalute if th euser has provided the right number of legs
function minmax($number)
{
global $inhead,$inleg;
$min = 2*$inhead;
$max = 4*$inhead;
if ($number<$min || $number>$max)
{
echo "please enter a number between $min and $max to get the correct result";
echo "\n";
echo "\n";
}
}
//function to calculate the number of heads and legs
function get_the_result($noofheads,$nooflegs)
{
$x=($nooflegs-(2*$noofheads))/2;
$y=$noofheads - $x;
echo "The num of dogs are $x";
//echo "/n";
echo "The no of humans are $y";
}
minmax($inleg);
get_the_result($inhead,$inleg);
?>
您可以使用:
在您的情况下,您需要检查函数是否存在,以避免使用function\u exists
尝试声明它们两次
虽然这是可能的,但我建议您重写代码,因为这种方法显然不可行。您可以使用:
在您的情况下,您需要检查函数是否存在,以避免使用function\u exists
尝试声明它们两次
虽然这是可能的,但我建议您重写代码,因为这种方法显然不可行。如果验证不满足要求,为什么不重新加载页面呢。 尝试使用会话显示错误消息
<?php
session_start();
if(isset($_SESSION['error'])){
echo $_SESSION['error'];
unset($_SESSION['error']);
}
// user to enter the number of heads
echo "Enter the number of heads: " . PHP_EOL;
// the script will wait here until the user has entered something and hit ENTER
$inhead = read_stdin();
// user to enter the number of legs
echo "Enter the number of legs: " . PHP_EOL;
// the script will wait here until the user has entered something and hit ENTER
$inleg = 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
}
//function to evalute if th euser has provided the right number of legs
function minmax($number)
{
global $inhead,$inleg;
$min = 2*$inhead;
$max = 4*$inhead;
if ($number<$min || $number>$max)
{
$_SESSION['error'] = "please enter a number between $min and $max to get the correct result";
echo("<meta http-equiv='refresh' content='1'>");
}
}
//function to calculate the number of heads and legs
function get_the_result($noofheads,$nooflegs)
{
$x=($nooflegs-(2*$noofheads))/2;
$y=$noofheads - $x;
echo "The num of dogs are $x";
//echo "/n";
echo "The no of humans are $y";
}
minmax($inleg);
get_the_result($inhead,$inleg);
?>
如果验证不满足要求,为什么不重新加载页面呢。
尝试使用会话显示错误消息
<?php
session_start();
if(isset($_SESSION['error'])){
echo $_SESSION['error'];
unset($_SESSION['error']);
}
// user to enter the number of heads
echo "Enter the number of heads: " . PHP_EOL;
// the script will wait here until the user has entered something and hit ENTER
$inhead = read_stdin();
// user to enter the number of legs
echo "Enter the number of legs: " . PHP_EOL;
// the script will wait here until the user has entered something and hit ENTER
$inleg = 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
}
//function to evalute if th euser has provided the right number of legs
function minmax($number)
{
global $inhead,$inleg;
$min = 2*$inhead;
$max = 4*$inhead;
if ($number<$min || $number>$max)
{
$_SESSION['error'] = "please enter a number between $min and $max to get the correct result";
echo("<meta http-equiv='refresh' content='1'>");
}
}
//function to calculate the number of heads and legs
function get_the_result($noofheads,$nooflegs)
{
$x=($nooflegs-(2*$noofheads))/2;
$y=$noofheads - $x;
echo "The num of dogs are $x";
//echo "/n";
echo "The no of humans are $y";
}
minmax($inleg);
get_the_result($inhead,$inleg);
?>
一种方法是将用户输入逻辑封装到while
循环中。只需继续循环开始部分,直到用户输入中断循环的内容并执行程序的其余部分。只需重新加载包含错误消息的页面即可session@jason,请您点亮while循环中的代码部分好吗?用户,这里是您的代码,所以你是程序员,决定哪些部分应该在while循环中,Jason可以给你指导,但你需要能够接受指导,并将其应用到你自己的编程逻辑中。不是由外部程序员来决定代码的哪些部分应该在哪些循环中。谢谢martin,我解决了这个问题。@jason,谢谢hintOne,这样做的方法是将用户输入逻辑封装到while
循环中。只需继续循环开始部分,直到用户输入中断循环的内容并执行程序的其余部分。只需重新加载包含错误消息的页面即可session@jason,请您点亮while循环中的代码部分好吗?用户,这里是您的代码,所以你是程序员,决定哪些部分应该在while循环中,Jason可以给你指导,但你需要能够接受指导,并将其应用到你自己的编程逻辑中。不是由外部程序员来决定代码的哪些部分应该在哪些循环内。谢谢martin,我已经解决了。@jason,谢谢hintaccording to php文档,如果我在函数内使用goto,我只能在该函数内找到一块代码,而不是在函数外,我的代码块在函数外,所以我无法使用goto@user3132672,您可以使用包装器或try-catch处理程序来完成,但我再次建议不要使用goto。根据php文档,如果我在函数内使用goto,我只能访问该函数内的代码块,而不能访问该函数外的代码块,我的代码块在函数外,所以我无法使用goto@user3132672,您可以使用包装器或try-catch处理程序来执行此操作,但我再次建议您不要使用goto。请您编辑整个代码,而不是只键入您的代码部分。此外,我正在尝试在控制台而不是浏览器中运行我的代码语句“请输入介于当“如果”条件为真时,是否不显示?有什么想法吗why@arjun,如果你能告诉我是怎么回事,我可以给你的答案投票。我认为会话在控制台中不起作用,所以这就是问题所在。你能编辑整个代码,而不是只键入代码的一部分吗,我试图在控制台而不是浏览器中运行我的代码当“IF”条件为真时,语句“please enter a number between…”不会显示,知道吗why@arjun,如果你能告诉我出了什么问题,我可以投票支持你的回答。我认为会话在控制台中不起作用,所以这就是问题所在。