473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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..

Jul 17 '05 #1
5 4941
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
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
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

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
So thats how you do that. I tried new array(); or something like that
but it complained.

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Asif | last post by:
Hi there, I have been trying to understand the behaviour of char (*pfn)(null) for a couple of days. can some body help me understand the behaviour of char (*pfn)(null) in Visual C++ environment?...
15
by: Alex | last post by:
could somebody tell me the difference between those two styles: function abc(var1, var2){ /* logic in here */ } and abc = function(var1, var2){ /* logic in here */ }; When / why would I...
18
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
23
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
9
by: Peskov Dmitry | last post by:
It is a very basic question.Surely i got something wrong in my basic understanding. //Contents of file1.cpp using namespace std; #include <iostream> template <typename T> class my_stack;
13
by: MartinRinehart | last post by:
Do these two give a different result? function positive() { return true; } var positive = function() { return true; }
6
by: vaclavpich | last post by:
Hi, I have a question on constant variables. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //...
7
by: krishna | last post by:
What is the need of this syntax for initializing values, isn't this ambiguous to function call? e.g., int func_name(20); this looks like function call (of course not totally as there is no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.