By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,407 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,407 IT Pros & Developers. It's quick & easy.

Question about variable declaration

P: n/a
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..

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 21 May 2005 07:42:39 -0700, "macduder83" <ad**************@gmail.com> wrote:
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.


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 / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2

P: n/a
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($myOtherArray) + 1];

works.

Thanks for the reply...

Jul 17 '05 #3

P: n/a
macduder83 wrote:
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($myOtherArray) + 1];

works.


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

Jul 17 '05 #4

P: n/a

macduder83 wrote:
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($myOtherArray) + 1];


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

Jul 17 '05 #5

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

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.