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 PHP Functions Introducing Functions PHP Function Arguments

Frank D
Frank D
14,042 Points

Is if(is_array($arg)) compulsory??

Is that "if(is_array($arg))" condition compulsory and why?

I have tried many times without that condition and I got an error on the } else { line.

Parse error: syntax error, unexpected T_ELSE on line 17

Why if $names is obviously an array it does not work without that conditional? Any good explanation? The code below:

 <?php
                 $names = array('Sandra', 'Jim', 'Ekleny', 'Okla');

           function hello($arg) {
                        if(is_array($arg)){
                foreach($arg as $name){
                    echo "Hello $name, how are you today?" . '<br>';
                }
                } else {
                    echo "Hello folks";
                }
            }

            echo hello($names);
?>
Frank D
Frank D
14,042 Points

That's the code without the if condition:

 <?php
                 $names = array('Sandra', 'Jim', 'Ekleny', 'Okla');

           function hello($arg) {
                foreach($arg as $name){
                    echo "Hello $name, how are you today?" . '<br>';
                } else {
                    echo "Hello folks";
                }
            }

            echo hello($names);
?>

2 Answers

Hi Frank,

An else statement can only extend an if statement. In other words, an else statement can not exist without a corresponding if.

This is why you get a parse error. The php interpreter comes to the else statement and it wasn't expecting one to be there because it did not come across an if statement before that.

So if you want to remove the if you have to also remove the else and all you would be left with is the foreach loop.

But now your function is prone to errors. A foreach loop can only loop over arrays and objects. It will error if you pass in something else, either by accident or intentionally.

So the reason the code is written that way is for error checking and to make the function more flexible.

If an array was passed in then go ahead and run this loop and output these messages, otherwise don't run the loop and just output this generic message "Hello folks"

As you mentioned, in this particular case you know that it's an array, But you have to think of the general case. This function might be called many times throughout the code and there could be an instance where an array wasn't passed in. This function will be able to handle that rather than producing an error.

Frank D
Frank D
14,042 Points

Thank you!! Now it all make sense. if....else... of course :O'

There is nothing wrong with the code, furthermore the block you have sent as no line 17. You want to keep the if is_array as you want to check that you are passing in an array of data otherwise PHP would error and it would prevent your page being displayed.

This worked for me;

<?php
$names = array('Sandra', 'Jim', 'Ekleny', 'Okla');

function hello($arg){
  if(is_array($arg)){
    foreach($arg as $name){
      echo "Hello $name, how are you?\n";
    }
  } else {
      echo "Hello folks\n";
  }
}

echo hello($names);
?>