Question about variable declaration

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

    Question about variable declaration

    All,

    I have modified my PHP.ini file so that it reports E_ALL, even the
    notices. I wanted to see everything. Question is how do I declare a
    variable... bare with me, Im new to PHP.

    thx..

  • Andy Hassall

    #2
    Re: Question about variable declaration

    On 21 May 2005 07:42:39 -0700, "macduder83 " <adrian.paveles cu@gmail.com> wrote:
    [color=blue]
    >I have modified my PHP.ini file so that it reports E_ALL, even the
    >notices. I wanted to see everything. Question is how do I declare a
    >variable... bare with me, Im new to PHP.[/color]

    Variables aren't declared in PHP, at least not separately from initialisation.

    Assign a value to the variable before you attempt to read any values from it.

    <?php
    print $x; // produces a warning
    ?>

    <?php
    $x = 1;
    print $x; // no warning
    ?>

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • macduder83

      #3
      Re: Question about variable declaration

      Ok... I have the answer. The issue was that the variable was an array.
      If I tried to do something like this ::

      $myArray[5]; // php screams that it is undeclared, if set E_ALL

      So to fix this the first thing you must do is this::

      $myArray = NULL; // and then start assigning sizes and whatnot.

      So::

      $myArray = NULL;
      $myArray[sizeof($myOther Array) + 1];

      works.

      Thanks for the reply...

      Comment

      • Oli Filth

        #4
        Re: Question about variable declaration

        macduder83 wrote:[color=blue]
        > Ok... I have the answer. The issue was that the variable was an[/color]
        array.[color=blue]
        > If I tried to do something like this ::
        >
        > $myArray[5]; // php screams that it is undeclared, if set E_ALL
        >
        > So to fix this the first thing you must do is this::
        >
        > $myArray = NULL; // and then start assigning sizes and whatnot.
        >
        > So::
        >
        > $myArray = NULL;
        > $myArray[sizeof($myOther Array) + 1];
        >
        > works.[/color]

        You don't need to declare the size of an array in PHP. It wouldn't make
        sense anyway, because an array in PHP is really a hashmap, i.e. the
        keys (indices) are arbitrary, and don't have to be sequential, or even
        numeric.

        e.g.

        $a[0] = "blah";
        $a[1] = "bleh";
        $a[1000] = "blih";
        $a["dog"] = "bloh";

        $a now has only 4 members, even though one of the keys is 1000.

        --
        Oli

        Comment

        • Ken Robinson

          #5
          Re: Question about variable declaration


          macduder83 wrote:[color=blue]
          > Ok... I have the answer. The issue was that the variable was an[/color]
          array.[color=blue]
          > If I tried to do something like this ::
          >
          > $myArray[5]; // php screams that it is undeclared, if set E_ALL
          >
          > So to fix this the first thing you must do is this::
          >
          > $myArray = NULL; // and then start assigning sizes and whatnot.
          >
          > So::
          >
          > $myArray = NULL;
          > $myArray[sizeof($myOther Array) + 1];[/color]

          Actually, you probably want to create a null array:

          $myArray = array();

          This is helpful if you're using a temporary array in a loop and you
          always want to start with an empty array.

          Ken

          Comment

          • macduder83

            #6
            Re: Question about variable declaration

            So thats how you do that. I tried new array(); or something like that
            but it complained.

            Comment

            Working...