Is it a bad idea to declare an array as array(-1) ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jan verschueren
    New Member
    • Jul 2010
    • 4

    Is it a bad idea to declare an array as array(-1) ?

    I got the question from someone at the office. He was converting some code to php code.

    He saw "$var = array(-1);" somewhere.
    On the internets people say it's better to use (0) then (-1), but no-one says why exactly.

    Can harm come from the array(-1) ?
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    $var = array(-1); is not the same as $var = array(0);. They are both arrays but they are arrays with different elements.

    I do not understand your question.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      I don't understand why initializing an array with a single element that has the value -1 could be harmful? You're creating an issue where there isn't one.

      Comment

      • jan verschueren
        New Member
        • Jul 2010
        • 4

        #4
        ok, thanks. just wanted to be sure

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          it might be different, if you consider languages like JavaScript, where passing a single value to the array constructor will set the array length (or any other object specific setting).

          but that’s not the case for PHP.

          Comment

          Working...