Parse error: syntax error, unexpected token "echo", expecting "function" or "const" in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/understanding-advanced-object-oriented-functionality-in-PHP-pag-185/math.php on line 6

class Math
{
  const pi = 3.14159;

  echo "Math::pi = ".Math::pi;
}

    A class doesn't/cannot contain runnable code apart from a function (method).
    <?php
    class Math
    {
    const pi = 3.14159;

    function test() {
    echo "Math::pi = ". Math::pi;
    }
    }

    // Do this, then, outside your class:
    $m = new Math;
    $m->test();

      The book you're using includes this in it's blurb on amazon:

      updated to incorporate the new features of PHP 5.1 and MySQL 5.1

      PHP 7 is at end of life and 5.6 active support ended 6 years ago. I'd venture to say most of this book is obsolete by now and you probably need a more recent reference. I've not read the book, but this one is recent and has decent reviews.

        I meant to respond to the each() deprecation thread, but honestly the point's the same...

          Depending on what you are actually trying to do, you could have just done:

          class Math {
            const pi = 3.14159;
          }
          // Outside of the class:
          echo "Math::pi = " . Math::pi . "\n";
          
            Write a Reply...