Accessing arrays returned from functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rick@fourfront.ltd.uk

    Accessing arrays returned from functions

    I was led to believe that it was possible to access an array returned
    from a function in php5 without first assigning it to a variable as
    follows ;-

    ....
    function setArr()
    {
    return array( 1, 2, 3, 4, 5 ) ;
    }

    echo setArr()[ 3 ] ;


    However this does not work - it just produces a parse error.
    Have I been mis-informed or am I doing it wrong ?
    TIA

  • ZeldorBlat

    #2
    Re: Accessing arrays returned from functions

    On Mar 28, 8:18 am, r...@fourfront. ltd.uk wrote:
    I was led to believe that it was possible to access an array returned
    from a function in php5 without first assigning it to a variable as
    follows ;-
    >
    ...
    function setArr()
    {
    return array( 1, 2, 3, 4, 5 ) ;
    >
    }
    >
    echo setArr()[ 3 ] ;
    >
    However this does not work - it just produces a parse error.
    Have I been mis-informed or am I doing it wrong ?
    TIA
    What led you to believe that? Where in the documentation does it say
    you can do it that way?

    Comment

    • Erwin Moller

      #3
      Re: Accessing arrays returned from functions

      rick@fourfront. ltd.uk wrote:
      I was led to believe that it was possible to access an array returned
      from a function in php5 without first assigning it to a variable as
      follows ;-
      >
      ...
      function setArr()
      {
      return array( 1, 2, 3, 4, 5 ) ;
      }
      >
      echo setArr()[ 3 ] ;
      >
      >
      However this does not work - it just produces a parse error.
      Have I been mis-informed or am I doing it wrong ?
      Hi,

      I asked the same question some time ago and was told it is not possible tat
      way.
      But assigning it to an array will not force a copy of the array, so the
      overhead involved is minimal.
      So as far as I know you were informed wrong.

      Regards,
      Erwin Moller
      TIA

      Comment

      • rick@fourfront.ltd.uk

        #4
        Re: Accessing arrays returned from functions

        What led you to believe that? Where in the documentation does it say
        you can do it that way?
        If only I could remember where ...
        As far as I can remember it was a write up about the functionality
        that was going to be available in php5 prior to the actual release.

        IMHO it should be valid - it would save a lot of time coding
        especially in the following instance ;-

        if ( setArr()[ 3 ] === 2 )
        {
        ....
        }

        instead of

        $X = setArr() ;
        if ( $X[ 3 ] === 2 )
        {
        ....
        }

        etc.

        The current syntax is a waste of an assignment - even if it is only a
        reference.

        Comment

        • Shelly

          #5
          Re: Accessing arrays returned from functions


          <rick@fourfront .ltd.ukwrote in message
          news:1175087834 .747284.228690@ b75g2000hsg.goo glegroups.com.. .
          >What led you to believe that? Where in the documentation does it say
          >you can do it that way?
          >
          If only I could remember where ...
          As far as I can remember it was a write up about the functionality
          that was going to be available in php5 prior to the actual release.
          >
          IMHO it should be valid - it would save a lot of time coding
          especially in the following instance ;-
          >
          if ( setArr()[ 3 ] === 2 )
          {
          ...
          }
          >
          instead of
          >
          $X = setArr() ;
          if ( $X[ 3 ] === 2 )
          {
          ...
          }
          >
          Surely you are joking -- "it would save a lot of time coding"??? It is ONE
          line and it makes the code clearer!

          What you are saying here reminds me of (way back when) when I first learned
          C coding. I found that some smart aleck programmers suffered from what I
          called the "Name that Tune Syndrome". They played the game like
          -- "I can write that code in 5 lines".
          -- "I can write that code in 3 lines".
          -- "I can write that code in one line".
          -- "Write that code".

          All it did was make debugging and maintenance a nightmare. Adding one extra
          line for increased clarity? B.F.D.!

          Shelly


          Comment

          Working...