1

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';
1
  • 1
    where does $carrotVar come from? or should that be $veggie Commented Apr 13, 2011 at 11:08

6 Answers 6

2

change

$veggie = 'carrots';

to

$carrotVar = 'carrots';
1

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';
1

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 )
1
  • Since there is no carrotVar defined it is throwing notice also Commented Apr 13, 2011 at 11:09
1

Although php doesn't need variable declaration, you can simply use it just by defining it when needed by the variable you are using, i.e. $carrotVar has no value in it so the output is being displayed not as you wished just switch $veggie = 'carrots'; to $carrotVar = 'carrots'; or change the array variable.

0

You did not define $carrotVar.

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

(or whatever your output needs to be.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.