1

这是我的功能

    public function isValidPostCode(&$toCheck)
    {
      // Permitted letters depend upon their position in the postcode.
      $alpha1 = "[abcdefghijklmnoprstuwyz]";                          // Character 1
      $alpha2 = "[abcdefghklmnopqrstuvwxy]";                          // Character 2
      $alpha3 = "[abcdefghjkpmnrstuvwxy]";                            // Character 3
      $alpha4 = "[abehmnprvwxy]";                                     // Character 4
      $alpha5 = "[abdefghjlnpqrstuwxyz]";                             // Character 5
      $BFPOa5 = "[abdefghjlnpqrst]{1}";                               // BFPO character 5
      $BFPOa6 = "[abdefghjlnpqrstuwzyz]{1}";                          // BFPO character 6

      // Expression for BF1 type postcodes
      $pcexp[0] =  '/^(bf1)([[:space:]]{0,})([0-9]{1}' . $BFPOa5 . $BFPOa6 .')$/';

      // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA with a space
      $pcexp[1] = '/^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([[:space:]]{0,})([0-9]{1}'.$alpha5.'{2})$/';

      // Expression for postcodes: ANA NAA
      $pcexp[2] =  '/^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([[:space:]]{0,})([0-9]{1}'.$alpha5.'{2})$/';

      // Expression for postcodes: AANA NAA
      $pcexp[3] =  '/^('.$alpha1.'{1}'.$alpha2.'{1}[0-9]{1}'.$alpha4.')([[:space:]]{0,})([0-9]{1}'.$alpha5.'{2})$/';

      // Exception for the special postcode GIR 0AA
      $pcexp[4] =  '/^(gir)([[:space:]]{0,})(0aa)$/';

      // Standard BFPO numbers
      $pcexp[5] = '/^(bfpo)([[:space:]]{0,})([0-9]{1,4})$/';

      // c/o BFPO numbers
      $pcexp[6] = '/^(bfpo)([[:space:]]{0,})(c\/o([[:space:]]{0,})[0-9]{1,3})$/';

      // Overseas Territories
      $pcexp[7] = '/^([a-z]{4})([[:space:]]{0,})(1zz)$/';

      // Anquilla
      $pcexp[8] = '/^ai-2640$/';

      // Load up the string to check, converting into lowercase
      $postcode = strtolower($toCheck);

      // Assume we are not going to find a valid postcode
      $valid = false;

      // Check the string against the six types of postcodes
      foreach ($pcexp as $regexp) {

        if (preg_match($regexp,$postcode, $matches)) {

          // Load new postcode back into the form element
              $postcode = strtoupper ($matches[1] . ' ' . $matches [3]);

          // Take account of the special BFPO c/o format
          $postcode = preg_replace ('/C\/O([[:space:]]{0,})/', 'c/o ', $postcode);

          // Take acount of special Anquilla postcode format (a pain, but that's the way it is)
          if (preg_match($pcexp[7],strtolower($toCheck), $matches)) $postcode = 'AI-2640';

          // Remember that we have found that the code is valid and break from loop
          $valid = true;
          break;
        }
      }

      // Return with the reformatted valid postcode in uppercase if the postcode was
      // valid
      if ($valid)
      {
          $toCheck = $postcode;
            return true;
      }
        else return false;
    }

注意函数参数中的 &$toCheck。现在在这一行的函数代码的末尾(见下面的代码)函数返回更新的邮政编码和真实值,以防所有条件都得到满足。

if ($valid)
      {
          $toCheck = $postcode;
            return true;
      }

我的问题是如何在函数之外获取返回的 $toCheck 变量的值。简单来说,我需要获取函数 isValidPostCode() 返回的 $toCheck 的值。我怎么能得到那个。请让我知道,因为我是 OOP 概念的新手。

到目前为止,我将此功能用作

if (!isValidPostCode($postcode))
{
    header("location:mechanics_edit.php?id=$id&case=edit&type=warning&msg=" .urlencode("Invalid Post Code"));
    exit();
}
4

1 回答 1

2

您的函数$toCheck 通过引用( &) 接收变量。因此,您传递给它的变量的值在它被调用后已经被修改。您正在传递变量$postcode,因此$postcode在函数之后访问将包含修改后的值。

// $postcode is some value, passed by reference to isValidPostCode()
if (isValidPostCode($postcode))
{
    // It was valid on input, and now it contains a new value
    // since it was passed by reference and modified in the function
    // just before the function returned TRUE
    echo $postcode
    // outputs some new value...
}

请注意,此变量$postcode与您在函数内部构造的变量不同$postcode那个是局部作用域的函数,尽管您将函数$postcode的值分配给引用变量$toCheck(即上面的外部作用域$postcode),但它们仍然是不同作用域中的 2 个不同变量。

于 2012-11-23T19:21:42.970 回答