Function with reference-passed variabel

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The87Boy

    Function with reference-passed variabel

    Are there somebody, who can tell me, what I'm doing wrong with this
    function:
    function hej($q, &$h) {
    if ($q == 0) {
    return true;
    $h = 'Hello';
    } else {
    return false;
    $h = 'Goodbye';
    }
    }

    $rt = hej(0, &$a);
    if ($rt==true) { echo 'Hej'; }
    else { echo 'Farvel'; }
    print_r($a);
    echo "<br />\n";

  • Oli Filth

    #2
    Re: Function with reference-passed variabel

    The87Boy said the following on 26/09/2006 12:04:
    Are there somebody, who can tell me, what I'm doing wrong with this
    function:
    function hej($q, &$h) {
    if ($q == 0) {
    return true;
    $h = 'Hello';
    } else {
    return false;
    $h = 'Goodbye';
    }
    }
    >
    $rt = hej(0, &$a);
    if ($rt==true) { echo 'Hej'; }
    else { echo 'Farvel'; }
    print_r($a);
    echo "<br />\n";
    >
    You are returning from the function before $h is set.

    --
    Oli

    Comment

    • The87Boy

      #3
      Re: Function with reference-passed variabel

      Yes, but does that mean, you can't write a function like system()
      youself?

      Oli Filth wrote:
      You are returning from the function before $h is set.

      Comment

      • Oli Filth

        #4
        Re: Function with reference-passed variabel

        The87Boy said the following on 26/09/2006 12:50:
        Oli Filth wrote:
        >You are returning from the function before $h is set.
        >
        Yes, but does that mean, you can't write a function like system()
        youself?
        Just make sure you set $h *before* the return statement.

        --
        Oli

        Comment

        • Johnny

          #5
          Re: Function with reference-passed variabel


          "Oli Filth" <catch@olifilth .co.ukwrote in message
          news:PV7Sg.4714 5$7D6.28026@new sfe2-win.ntli.net...
          The87Boy said the following on 26/09/2006 12:04:
          Are there somebody, who can tell me, what I'm doing wrong with this
          function:
          function hej($q, &$h) {
          if ($q == 0) {
          return true;
          $h = 'Hello';
          } else {
          return false;
          $h = 'Goodbye';
          }
          }

          $rt = hej(0, &$a);
          if ($rt==true) { echo 'Hej'; }
          else { echo 'Farvel'; }
          print_r($a);
          echo "<br />\n";
          >
          You are returning from the function before $h is set.
          >
          --
          Oli
          also you don't need to use the & when you call the function, only when you
          define it:



          Comment

          Working...