Parameters and Arguments; Incorrect syntax help!


#1
    <?php
    $faf = array("Lonnie", "Will", "David", "Knapp", "Chris");
        array_push($faf, "Austen");
        sort($faf);
        return($faf);
        
    function nameGenerator(){
            $whosthere = rand(0, count($faf));
                if ($whosthere != false) {
                    $name = $faf[$whosthere];
                    }
            return($name);
        }
        
    function greetings($name){
        nameGenerator();
            if ($name == true){
        	    echo "Greetings, " . $name . "!";
                }
            else {
        	    echo "Hmm, nobody there..";
                }
        }
    
    greetings($name);
?>

I'm being told that nothing is getting printed, therefore I didn't call my function. I clearly use echo in my greetings function and called to it at the bottom of the code. Please help.


#2
function greetings($name){
    nameGenerator();
        if ($name == true){
    	    echo "Greetings, " . $name . "!";
            }
        else {
    	    echo "Hmm, nobody there..";
            }
    }

greetings($name);

First off, $name is not defined so this should throw an error. From the way I read this function, it shouldn't need an argument (no parameter necessary). The $name variable is defined in the nameGenerator() function, and is returned to the caller so the greetings() function should read,

    function greetings() {
        $name = nameGenerator();
        if ($name) {
            echo "Greetings, " . $name . "!";
        } else {
            echo "Hmm, nobody there&hellip;";
        }
    }
    
    greetings();

Only one small question remains... When will $name not be a name? Never. However, introducing a single empty string as one of the elements in the array will create this condition, at times.

Now looking at the code at the top, return($faf); is an illegal return. There is no function to return from. That line may be removed. Also, since the name generator is choosing a random index, do we need to sort the list? Not really. That step may be removed, as well.

This brings us to the nameGenerator() function, itself.

    function nameGenerator(){
        $whosthere = rand(0, count($faf));
            if ($whosthere != false) {
                $name = $faf[$whosthere];
                }
        return($name);
    }

The conditional is moot. $whosthere will always be a number in the range (0, count($faf)) so will never be false. We can write a simpler function to do the same thing:

    function nameGenerator(){
        $whosthere = rand(0, count($faf));
        return $faf[$whosthere];
    }

Try these fixes and see what results.


#3

The condition for when $name will not be a name was actually in the original code but poorly done with line:

$atDoor = rand(0, count($faf));

Because rand is choosing between 7 options and my array originally only had 6 names. I knew this wasn't the best way but at the time I had no other idea how to create the desired condition.

So this is what I ended up with working after heeding your advice and looking around at some other stuff. The reason function greetings() has a parameter is because the lesson requires it; I ended up writing a code more complex than necessary making the parameter moot for real life purpose?

    <?php
        function greetings($name)
            {
            $faf = array("", "Austen", "Lonnie", "Will", "David", "Knapp", "Chris");
            $atDoor = rand(0, count($faf)-1);
                
            if ($atDoor == true)
                {
                $name = $faf[$atDoor];
                echo "Greetings, " . $name . "!";
                }
            else {
                echo "Hmm, nobody there..";
                }  
            }
        //
        greetings("");
    ?>

P.S. How did you get that first bit of code you posted to be colored?


#4

For normal one-color sample code, highlight the block of code and click the </> tool. For color highlighting, type three back-ticks, (usually left of the 1 key) and a newline, paste in your code, and one newline with three more back-ticks.


#5

You're trying to do way too much for what it is asking. Way too much.
All it is asking for here is ONE function:. function greetings($name);
simple as

$name="whateveryournameis";
function greetings($name){
echo "Greetings, " .$name. "!";
}

greetings("$name");


#6

Am I trying to do more than what the lession asks? Yes. Is that necessarily bad? I don't think so; I was just trying to incorporate a few things I had learned from previous lessons. As you'll see above, I did minimize my code to a single function and still achieved the desired effect.

TLDR: Don't crush my creativity, brah.