Here's my code for index.php


    // configuration

    // render portfolio
    $rows = CS50::query("SELECT symbol, shares FROM portfolio WHERE id = ?", $_SESSION["id"]);

    $positions = [];
    foreach($rows as $row)
        $stock = lookup($row["symbol"]);
        if ($stock !== false)
            $positions[] = [
                "name" => $stock["name"],
                "price" => $stock["price"],
                "shares" => $row["shares"],
                "symbol" => $row["symbol"],
                "total" => $stock["price"] * $row["shares"]

    $cash = CS50::query("SELECT cash FROM users WHERE id =?", $_SESSION["id"]);

    render("portfolio.php", ["positions" => $positions, "cash" => $cash, "title" => "Portfolio"]);


And here's my code for portfolio.php




        foreach ($positions as $position)
            print("<td>" . $position["symbol"] . "</td>");
            print("<td>" . $position["name"] . "</td>");
            print("<td>" . $position["shares"] . "</td>");
            print("<td>" . $position["price"] . "</td>");
            print("<td>" . $position["total"] . "</td>");


<pr>You have <?php print($cash); ?> balance left.</pr>

However when I try to load https://ide50-username.cs50.io/, I only get the table's header row and nothing else, even the line that tells me how much cash I have left prints a weird array instead of telling me how much cash I have left. Here's a screenshot of the website

Can anyone tell me where I went wrong? Also, how do I debug a website like this? It seems that gdb doesn't work so well here.

1 Answer 1


the interpreter is complaining about array to string conversion. I assume it's talking about $cash since $cash is an array, yet you're passing it as a string as that's what print takes. see http://php.net/manual/en/function.print.php!

  • 1
    Even so, I get an error if I change $cash to $cash["cash"]. Also is there any alternative to gdb/printf for php which will allow me to see the contents of my variables?
    – Wet Feet
    Mar 23, 2016 at 11:49
  • 1
    @WetFeet recall query returns an array of rows! and I think dumping/printing the values of your variables is probably the easiest approach with PHP at this level.
    – kzidane
    Mar 23, 2016 at 12:12
  • 1
    What function can I use to print to the terminal instead of the Web page in php? Looking for something like printf in C for PHP but googling doesn't seem to bring up any results.
    – Wet Feet
    Mar 23, 2016 at 13:15
  • 2
    @WetFeet the problem is that you don't run the PHP interpreter yourself (e.g., from a terminal) to be able to see what's being printed on the standard output. the server is the one which runs the PHP interpreter. a simple hack would be to dump your values to a file, maybe using file_put_contents.
    – kzidane
    Mar 23, 2016 at 13:56

You must log in to answer this question.

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