Web Analytics Made Easy -
StatCounter References / Pointers in PHP - CodingForum

Announcement

Collapse
No announcement yet.

References / Pointers in PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • References / Pointers in PHP

    I've got some code which reads in large amounts of data from a database into an array and passes the data between a few functions to display the information I need.

    I know in PERL I can use references to save on memory usage when passing arrays, and since PHP has roots in PERL I'm guessing there's a way to do it. A quick search hasn't given me any results, (unless I'm using the wrong terms).

    Does anybody use references / pointers in PHP? If so can you point (no pun intended) me to a reference (ok, pun intended )?

    Thanks
    Create accessible online surveys -::- Koobten.com - compare netbook prices and reviews -::- Affordable, reliable hosting for less than $20 per year
    Zend Certified Engineer


  • #2
    There is quite an extensive manual section on the usage of references, at least how the PHP developers thought they should be applied:


    I think you can use the ampersand in front of an array variable to create a reference to it.

    PHP Code:
    $foo = array("bla""barf""bork");
    $foo2 =& $foo
    But try it out first, references in PHP are tricky.
    De gustibus non est disputandum.

    Comment


    • #3
      Ok, I don't get the PHP references.

      In PERL, if I reference a variable like this...

      Code:
      #!/usr/bin/perl
      $var1 = "Hello";
      $var2 = \$var1;
      print $var2;
      It prints out a memory location. In PHP if I use...

      Code:
      <?php
      $a = "HELLO WORLD";
      $b = & $a;
      print "A:$a -- B:$b";
      ?>
      All it does is print out the value of the variable.

      Is there a way to pass the memory location and work with that in PHP???
      Create accessible online surveys -::- Koobten.com - compare netbook prices and reviews -::- Affordable, reliable hosting for less than $20 per year
      Zend Certified Engineer

      Comment


      • #4
        nope. at leas, not that I'm aware of. You're seriously talking about using pointers here...don't think there's a way to get an actual memory address...

        Comment


        • #5
          yeah just to reenforce what's already been said, I've come to PHP from a c++ background, and I haven't been able to use pointers.

          However, part of the advantage that they provide in C++ is still provided in PHP. You can still do inexpensive assignments by assigning by reference, as in the example above. So if its inexpensive assignments you're after, you have it. I believe you can even return-by-reference.

          However, if you're seeking superior control over memory, I'm not sure you can't get it... But that's the don't-worry-about-the-details-we'll-do-it-for-you nature of PHP.

          Comment


          • #6
            do you/why do you , want the physical address ? , if you want to play with shared memory for application scope stuff then perhaps take a peek at the shmop functions ( also mmcache provides some shared memory operations) , else be happy that PHP works the way it does
            resistance is...

            MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

            Comment

            Working...
            X