Return value of function is getting added

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    Return value of function is getting added

    Hi...

    I have a php script which has a function call and function returns a value.

    But when i open it in browser and refresh the page the return value will get added to the old value.

    Code is:
    Code:
    <? php
    
    include("function.php");
    
    $res=0;
    
    $res=function::fn_call()
    
    echo $res;
    
    ?>
    When i run it in Terminal i will always get the desired value, but when i run it on browser. first time it displays currect value and on page refresh it adds the return value to old returned value.

    i.e if return value echoed for 1st time is 4000 after page refresh i displays 8000, after next refresh 12000 and so on.

    Script runs perfectly in terminal.
    I am hosting pages on nginx and server log do not say anything as error.

    Any idea why it behaves strangly?

    Regards
    Dheeraj
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    What is the contents of function.php?

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      It is a wrapper for set of C functions.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        As a guess (because you failed to answer my previously question sufficiently), I'd say the problem is this: the SAPI that a web server uses is continuous, as opposed to the SAPI that CLI uses, which is request-based. Therefore, it's how you're implementing those C wrappers that is the problem, but I cannot be any more specific because I am in no way shape or form an expert on Zend extensions (I only know a little).

        I recommend you read the book 'Extending and Embedding PHP' - google will show you where to get it (although it is slightly outdated).

        Mark.

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          I am using SWIG to Wrap my C functions.

          And one more thing what i dont understand is why it gives desired output in the terminal not in browser?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by dheerajjoshim
            I am using SWIG to Wrap my C functions.

            And one more thing what i dont understand is why it gives desired output in the terminal not in browser?
            Like I said, from what I can remember (I will have to read it again), a web-server uses a continuous SAPI, that is, once the server is started, any memory it uses is not reclaimed until the server is stopped/restarted, unless the memory is explicitly freed by the server/extension. So, if a C extension uses some memory when the server cycle starts and doesn't free it at the end of a request (I will explain about this more when I have re-read the book), the memory is retained.

            The CLI (terminal/command-line) SAPI is started and stopped with each request - therefore, Zend frees up it's memory when the cycle ends - when the script you're executing ends.

            But don't quote me on any of that.

            Mark.

            P.S. It's hard to explain something when you don't fully remember / understand what you read - so, hopefully, I'll make more sense when I have read it again.

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              Ok.

              Thanks for some info Mark...

              Does somebody out there knows a solution for this problem?

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by dheerajjoshim
                Ok.

                Thanks for some info Mark...

                Does somebody out there knows a solution for this problem?
                ... if we saw the source-code, then we might be able to help; we're not psychics.

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  Definitely not psychics.....

                  All i wanted to know is did somebody got a problem like this in past?? if yes, how they tackled it..

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by dheerajjoshim
                    All i wanted to know is did somebody got a problem like this in past??
                    at least not me......

                    Comment

                    • Dheeraj Joshi
                      Recognized Expert Top Contributor
                      • Jul 2009
                      • 1129

                      #11
                      Do you have any insight about problem?

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        nope, haven’t done anything as such yet.

                        Comment

                        • Dheeraj Joshi
                          Recognized Expert Top Contributor
                          • Jul 2009
                          • 1129

                          #13
                          Ok....

                          Hope Somebody knows the solution.

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #14
                            Originally posted by dheerajjoshim
                            Ok....

                            Hope Somebody knows the solution.
                            Look, what I'm telling you is: if we do not see the source-code, the chances of us being able to help you are incredibly slim.

                            Comment

                            • Dheeraj Joshi
                              Recognized Expert Top Contributor
                              • Jul 2009
                              • 1129

                              #15
                              Which source code?

                              C or PHP?

                              Comment

                              Working...