whats wrong with this picture?

$appleVar = 'apple';
$veggie = 'carrots';

$var = array('fruit' => $appleVar, 'veggie' => ''.$carrotVar.' no carrots please');

print_r($var);

when I print the array only "no carrots please" is displayed. why?

I'm so sorry I meant

$carrotVar = 'carrots'; not $veggie = 'carrots';
share|improve this question
1  
where does $carrotVar come from? or should that be $veggie – bumperbox Apr 13 '11 at 11:08

5 Answers

up vote 1 down vote accepted

When declaring the array, you are using $carrotVar :

$var = array(
    'fruit' => $appleVar, 
    'veggie' => ''.$carrotVar.' no carrots please'
);

But that $carrotVar variable is not defined.


You should probably use the $veggie variable :

$var = array(
    'fruit' => $appleVar, 
    'veggie' => ''.$veggie.' no carrots please'
);


Or rename it so it matches its content :

$carrotVar = 'carrots';
share|improve this answer

change

$veggie = 'carrots';

to

$carrotVar = 'carrots';
share|improve this answer

Have you checked carefully.

In my case it is printing :-

Notice: Undefined variable: carrotVar in /home/jatin/webroot/vcms/trunk/application/modules/ibroadcast/controllers/VideoController.php on line 10 Array (
    [fruit] => apple
    [veggie] =>  no carrots please )
share|improve this answer
Since there is no carrotVar defined it is throwing notice also – Jatin Dhoot Apr 13 '11 at 11:09

You did not define $carrotVar.

share|improve this answer

$appleVar = 'apple';
$veggie = 'carrots';
$carrotVar = $veggie . ' no carrots please';
$var = array('fruit' => $appleVar, 'veggie' => $carrotVar);

Or whatever your output needs to be?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.