Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:44:46+00:00 2026-06-14T19:44:46+00:00

Here is my function public function isValidPostCode(&$toCheck) { // Permitted letters depend upon their

  • 0

Here is my function

    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;
    }

Notice the &$toCheck in the function argument. Now at the end of the function code at this line (see code below) function returns the updated postcode and true value in case of all conditions being met.

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

My question is how can I get the value of the returned $toCheck variable outside the function. In simple words I need to fetch the value of $toCheck being return by the function isValidPostCode(). How can I get that. Please let me know as I am newbie with OOPs concepts.

So far i am using this function as

if (!isValidPostCode($postcode))
{
    header("location:mechanics_edit.php?id=$id&case=edit&type=warning&msg=" .urlencode("Invalid Post Code"));
    exit();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:44:47+00:00Added an answer on June 14, 2026 at 7:44 pm

    Your function receives the variable $toCheck by reference (&). Therefore, the value of the variable you passed to it has been modified after it is called. You are passing in the variable $postcode, so just accessing $postcode after the function will contain the modified value.

    // $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...
    }
    

    Note that this variable $postcode is not the same $postcode that you constructed inside the function. That one was scoped locally to the function, and although you assigned the function $postcode‘s value to the reference variable $toCheck (which was the outer scope $postcode above), they remain 2 distinct variables in different scopes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my RegisterController: public function saveforminformationAction(){ $request = $this->getRequest(); if($request->isPost()){ //I NEED HELP WITH
Here is the class: functions.php class buildPage { public function Set($var,$val){ $this->set->$var = $val;
Here's my Zend code: <?php require_once ('Zend\Form.php'); class Sergio_Form_registrationform extends Zend_Form { public function
There is probably something dumb I am doing wrong here. public function get_scores($id) {
I have this little function here: public Activity getRootActivity() { Activity a = this;
Here is my function ( updated ): Public Shared Function shortenUrl(ByVal URL As String)
Here is my function: Public Function ListPublishedArticles(ByVal startingDate As DateTime) As List(Of Article) Dim
Hi I have this code here: public function length($args) { if (isset($this->length)) { foreach
I have two functions here: public function bindOwnerToSites(){ error_reporting (E_ALL^ E_NOTICE); foreach( $this->balance as
this might be a bit tricky. First here is my function: public function transfer_smf_userinfo($username)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.