簡體   English   中英

如果條件為真,則轉到程序的開始

[英]if condition is true goto the start of the program

我有這段代碼,基本上,如果函數“ minmax”中此代碼中的“ IF”語句為true,我希望程序從頭開始。 我不確定如何實現此邏輯,請有人幫忙

<?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);
?>

您可以使用goto

location:
//...
goto location;

在您的情況下,您需要檢查函數是否存在,以免嘗試使用function_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);
?>

請注意,此處無法使用標頭重載,因為已經回顯了某些元素。 您可以更改content的值以延遲頁面重新加載的時間(該值以秒為單位)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM