Return Array from PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Beginner1
    New Member
    • Feb 2007
    • 16

    Return Array from PHP

    Hi all,

    Could anyone help me about returning the array from a function in PHP? What is the right way to do it in PHP?
    I've tried by reference and also returned as a local variable. Both works but is they good for big arrays??? Is they safe??


    However, my current knowledge prompts me (from C++) that returning a pointer or a reference might be dangerous (this variable can go out of scope).

    I appreciate for your information.

    P.S. Can you also suggest any documentation about the good style of programming in PHP. I often have such questions:(.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I have often agonised over this myself being from a C background.
    But by return from a function are you refering to the return value or a referenced function argument? The following is my understanding from reading various articles (sorry no bibliography).
    There are no real pointers in PHP, only references that have to be looked up each time you refer to one. From a performance point, it takes as long to look up a reference as it does to make a copy. However I find that hard to believe when passing arrays or recordsets. There must be a mid point where the size of the array dictates it is better to use a reference. My instinct tells me to use references for any array but not for variables, as in C.
    I would normally create an array or a string outside the function even if empty then pass it as a referenced function argument. I try to limit function returns to an integer or a boolean. But most PHP system functions seem to return array or string results and none to my knowledge accept a pointer, so is my practice the most efficient?

    Comment

    • devsusen
      New Member
      • Feb 2007
      • 136

      #3
      Hi,

      I prefer to go through good documentation or tutorial of php. search it in google, I hope u will good ones.

      I am writing a sample code to return array

      [PHP]function sendArray()
      {
      $arr = array('test1', 'test2', 'test3', 'test4');
      return $arr;
      }[/PHP]

      its really simple, thats why I like php :)
      Last edited by devsusen; Apr 18 '07, 01:47 PM. Reason: spelling mistake :(

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Essentially there are not pointers in PHP. In my opinion the only time you will want to pass an array by reference to a function is when you want the function to modify the value of the array.
        When considering the speed efficiency, you should first think about where the bottleneck in the operations are going to be. Most likely, under a scrutinizing eye, the bottleneck for any PHP application will be in the actual interpretation of the script, not the memory access and allocation (which happens in RAM, and is usually done in the MMU)
        Additionally, you will not need to worry about memory management issues, as PHP is a dynamic-typed language, and will not perform any 'dangerous' operations. In fact, unless you specifically unset() a variable, it remains in memory until the end of the scripts lifetime, even if you leave the scope in which the variable was declared in.
        PHP.net is the best reference by far for the inner workings of the language. I would strongly suggest you take the time to do a little reading.

        Originally posted by Beginner1
        Hi all,

        Could anyone help me about returning the array from a function in PHP? What is the right way to do it in PHP?
        I've tried by reference and also returned as a local variable. Both works but is they good for big arrays??? Is they safe??


        However, my current knowledge prompts me (from C++) that returning a pointer or a reference might be dangerous (this variable can go out of scope).

        I appreciate for your information.

        P.S. Can you also suggest any documentation about the good style of programming in PHP. I often have such questions:(.

        Comment

        • Beginner1
          New Member
          • Feb 2007
          • 16

          #5
          Thank you very much for your replies!!! They really helped me!

          Comment

          Working...