Please, take a look at how I understand the render() function from helplers.php, called by the function apologize().

     * Apologizes to user with message.
    function apologize($message)
        render("apology.php", ["message" => $message]);

/** * Renders view, passing in values. */ function render($view, $values = []) { // if view exists, render it if (file_exists("../views/{$view}")) { // extract variables into local scope extract($values);

            // render view (between header and footer)

        // else err
            trigger_error("Invalid view: {$view}", E_USER_ERROR);

The function apologize takes one argument as its parameter - $message. Apologize() calls the render function that have two parameters: the variable $view, which is supposed to be the name of a file; and an array $values.

By passing $values = [] as a parameter I in fact say that this array can have any kind and any number of keys in it, correct?

The apologize () function calls the render() one with parameters "apology.php" and ["message" => $message]. Then checks if apology.php exists in the given folder. If yes, then it calls extract(["message" => $message]) function and thus creates a new variable $message, which now has the value $message. (question on this below). Open header.php, followed by apology.php, which uses that new $message variable, and finish by opening footer.php; therefore display the whole message starting from Sorry! (printed as it is because it is not a php, but html) followed by $message. If apology.php doesn't exist in the given folder, return error.

I am confused by this $message story. If I pass to the apologize() function the variable $message as its parameter, where do I get this message from? Or would it correct to assume that if I have a small program, in which I call apologize() function, I should create a variable $message (or any other name) and use something like readline() function to get that message, or hardcode the value in that variable?

Thank you!


You might be overthinking this. Look at login.php in the distro code. It calls apologize with a string literal ie apologize("Invalid username and/or password."); $message has the value that was passed to the apologize function. So, you do not have to create any variable named $message, it is the function argument.

  • Thank you very much for your reply and help. Yes, I see how apologize() is called in the login.php, and I see now that, indeed, no separate variable is needed. I took to enjoy C so much (although have been not acquainted with anything related to programming before CS50), so I guess I need to get used to "easy" php features ) – Vitale Jul 4 '16 at 15:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .