Jump to content

Recommended Posts

Can someone tell me what's wrong with the following:

 

$myNewArray = array();
$myNewArray[variable1] = "Hello world";
$myNewArray[variable2] = "Hello Universe";
$myNewArray[weirdName] = "Hello Everyone!";

 

If I have

error_reporting(E_ALL);

on the first line, this throws many errors.

 

The errors say something like:

Notice: Use of undefined constant variable1 - assumed 'variable1' in /home/user/public_html/dirs/dir2/dir3/myTest.php on line 5

 

I'm a bit lost, I didn't think there was anything wrong with my array declreation.

If I leave error_reporting(E_ALL); out, then all is OK!  But because it's throwing errors, I guess I must be doing something semantically wrong?

 

Thanks.

 

 

OM

Try doing:

 

$myNewArray = array();
$myNewArray['variable1'] = "Hello world";
$myNewArray['variable2'] = "Hello Universe";
$myNewArray['weirdName'] = "Hello Everyone!";

 

The error indicates that PHP is looking for a constant defined earlier titled variable1 because variable1 is not defined in quotes inside your array arguement that is declaring it.

when you do $blah[something] php first checks to see if something is a constant. Since you did not make it a constant, it's throwing you that error.  However, if php fails to find a constant, it will treat it will then attempt to use it as a string like $blah['something'].  So...your code will work as intended, but you get that message.  To fix it, use the quotes around it as njoj pointed out.

aah... ok.  i will do this now.

just to confirm though, if i have:

$myNewArray = array();
$myNewArray['variable1'] = "Hello world";
$myNewArray['variable2'] = "Hello Universe";
$myNewArray['weirdName'] = "Hello Everyone!";

 

when i actually want to reference the items, should i use $myNewArray[weirdName] or $myNewArray['weirdName']?

 

thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.