function call as an array

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

    function call as an array

    Hi all,

    I'm after the PHP equivlent to this Perl construct:

    print @{[someFunctionCal l()]}[0];

    In the above example someFunctionCal l() would return an array, and we're
    printing the first item from the returned array..

    How do I do this in PHP ? (without first assigning the output of the
    function call to a variable)

    Chris


  • Jerry Stuckle

    #2
    Re: function call as an array

    Skeleton Man wrote:
    Hi all,
    >
    I'm after the PHP equivlent to this Perl construct:
    >
    print @{[someFunctionCal l()]}[0];
    >
    In the above example someFunctionCal l() would return an array, and we're
    printing the first item from the returned array..
    >
    How do I do this in PHP ? (without first assigning the output of the
    function call to a variable)
    >
    Chris
    >
    >
    Chris,

    I don't think you can.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Steve

      #3
      Re: function call as an array


      "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
      news:wsidndQz3O mUNbPbnZ2dnUVZ_ tzinZ2d@comcast .com...
      | Skeleton Man wrote:
      | Hi all,
      | >
      | I'm after the PHP equivlent to this Perl construct:
      | >
      | print @{[someFunctionCal l()]}[0];
      | >
      | In the above example someFunctionCal l() would return an array, and we're
      | printing the first item from the returned array..
      | >
      | How do I do this in PHP ? (without first assigning the output of the
      | function call to a variable)
      | >
      | Chris
      | >
      | >
      |
      | Chris,
      |
      | I don't think you can.

      currently, you can't in php. you have to return the full array to a variable
      and then access the element(s).


      Comment

      • Rami Elomaa

        #4
        Re: function call as an array

        news:v7idndPlgc RHArPbnZ2dnUVZ_ j-dnZ2d@wightman. ca...
        Hi all,
        >
        I'm after the PHP equivlent to this Perl construct:
        >
        print @{[someFunctionCal l()]}[0];
        >
        In the above example someFunctionCal l() would return an array, and we're
        printing the first item from the returned array..
        >
        How do I do this in PHP ? (without first assigning the output of the
        function call to a variable)
        If it indeed is the first element, then you can use reset (and for the last
        element end) but there is no such functionality for returning the n'th
        element.

        echo reset(functionc all());

        Set the internal pointer of an array to its first element

        reset() rewinds array's internal pointer to the first element and returns
        the value of the first array element, or FALSE if the array is empty.


        --
        Rami.Elomaa@gma il.com

        "Good tea. Nice house." -- Worf


        Comment

        • Toby A Inkster

          #5
          Re: function call as an array

          Rami Elomaa wrote:
          If it indeed is the first element, then you can use reset (and for the last
          element end) but there is no such functionality for returning the n'th
          element.
          >
          echo reset(functionc all());
          If you need the nth item, it's fairly easy to write your own function to
          do this:

          function array_n($array, $n) { return $array[$n]; }

          echo array_n(functio ncall(), 2);

          --
          Toby A Inkster BSc (Hons) ARCS
          Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

          Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

          * = I'm getting there!

          Comment

          • Toby A Inkster

            #6
            Re: function call as an array

            Skeleton Man wrote:
            print @{[someFunctionCal l()]}[0];
            PS -- this sort of thing is generally unsafe, as you've not checked that
            the returned array even *has* an element with index 0. someFunctionCal l
            might be defined as:

            function someFunctionCal l ()
            {
            if (some error condition)
            return FALSE;
            else
            return array(etc, etc, etc);
            }

            You should really check that someFunctionCal l has returned an array, and
            that the array has an element 0 before you try to print element 0 out!

            --
            Toby A Inkster BSc (Hons) ARCS
            Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

            Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

            * = I'm getting there!

            Comment

            • Skeleton Man

              #7
              Re: function call as an array

              >You should really check that someFunctionCal l has returned an array, and
              >that the array has an element 0 before you try to print element 0 out!
              I know it will always return an array with at one least element.. but
              that's besides the point really..

              Chris


              Comment

              Working...