Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

MELVIN GEORGE
MELVIN GEORGE
3,490 Points

Not executing code in php....(PLZ HELP)

this code is not executing. whats the problem i cannot find anything...

<?php
 /* FUNCTION GREETING AND MESSAGE */ 
 function greet_nd_message($name_arr , $message_arr){
    if(is_array($name_arr , $message_arr)){
       foreach($name_arr as $name , $message_arr as $message) { 
          echo "Hello , $name : MESSAGE :: $message <br>";
       }  
    }
 }

 /* NAME ARRAY */
 $name=array(
 'Melvin',
 'Nibin',
 'Basil'
 );

 /* MESSAGE ARRAY */
 $message=array(
 'hai ....!',
 'hai ....!',
 'hai....!'
 );

 greet_nd_message($name , $message);
?>

SORRY FOR BAD ENGLISH !

2 Answers

Grace Kelly
Grace Kelly
33,990 Points

Hi Melvin there are a couple of issues with your code which i will try to iron out :

Firstly you need to create two seperate instances of the condition within the if statement for it to work, like so:

<?php

if(is_array($name_arr) && is_array($message_arr)){ //if $_name_arr is an array AND if $_message_arr is an array

}

?>

Secondly foreach($name_arr as $name , $message_arr as $message) is NOT valid code, we can only use ONE array in a foreach loop, I know you want to access two arrays within this foreach loop so i would suggest the following:

<?php

   $index = 0; //create an $index variable
 foreach($name_arr as $name ){ 
 echo "Hello , $name : MESSAGE :: $message_arr[$index] <br>"; //access the index value within the message_arr [0,1,2]
   $index++; //add one to the $index
  }

Lastly I see you have a syntax error within your $message array, you don't need a comma on the last value within an array, you need to change it to the following:

<?php
$message=array(
 'hai ....!',
 'hai ....!',
 'hai....!' //remove last comma
 );

?>

So now putting all this together we have the following:

<?php

 /* FUNCTION GREETING AND MESSAGE */ 

 function greet_nd_message($name_arr , $message_arr){

 if(is_array($name_arr) && is_array($message_arr)){
   $index = 0;
 foreach($name_arr as $name ){ 
 echo "Hello , $name : MESSAGE :: $message_arr[$index] <br>";
   $index++;
  }

 }

 }

 /* NAME ARRAY */
 $name=array(
 'Melvin',
 'Nibin',
'Basil'
 );

 /* MESSAGE ARRAY */
 $message=array(
 'hai ....!',
 'hai ....!',
 'hai....!'
 );

 greet_nd_message($name , $message);

 ?>

I checked this on workspaces and it now seems to run fine, If you have any questions don't hesitate to ask! :)

MELVIN GEORGE
MELVIN GEORGE
3,490 Points

What if there is four messages in the "message_arr"..what would happen..?

Grace Kelly
Grace Kelly
33,990 Points

In this case with the way the code is set up, if there are 3 values in the $name array and 4 values in the $message array, the 4th value would be ignored as the foreach loop uses the $name values so the loop is ran three times and so it only goes through the $message array 3 times, hence you get the following output

Hello , Melvin : MESSAGE :: hai ....! 
Hello , Nibin : MESSAGE :: hai ....! 
Hello , Basil : MESSAGE :: hai....! 
//fourth message value is ignored

With this in mind if you added another value to the $name array along with another value in the $message array, the foreach loop would run 4 times and you would see the values, for example:

<?php

 /* FUNCTION GREETING AND MESSAGE */ 

 function greet_nd_message($name_arr , $message_arr){

 if(is_array($name_arr) && is_array($message_arr)){
   $index = 0;
 foreach($name_arr as $name ){ 
 echo "Hello , $name : MESSAGE :: $message_arr[$index] <br>";
   $index++;
  }

 }

 }

 /* NAME ARRAY */
 $name=array(
 'Melvin',
 'Nibin',
'Basil',
'Mike' //added 4th value
 );

 /* MESSAGE ARRAY */
 $message=array(
 'hai ....!',
 'hai ....!',
 'hai....!',
'How are you?' //added 4th value
 );

 greet_nd_message($name , $message);

 ?>

This would give you the following output:

Hello , Melvin : MESSAGE :: hai ....! 
Hello , Nibin : MESSAGE :: hai ....! 
Hello , Basil : MESSAGE :: hai....! 
Hello , Mike: MESSAGE :: How are you?
MELVIN GEORGE
MELVIN GEORGE
3,490 Points

How can i access more than 1 array in a loop. Other than accessing via "foreach" loop mentioned above?

MELVIN GEORGE
MELVIN GEORGE
3,490 Points

Thank You...So much..Kelly..

I reformatted your code to look more readable.

The error in your code is this line:

<?php 

foreach($name_arr as $name , $message_arr as $message) {

?>

This is not valid.