0

Please help me with this error. I try to modify the php.ini file to add ;short_open_tag=off but nothing works?

<?php
function pdo_connect_mysql(){
    //Connection DB
    $DATABASE_HOST = 'localhost';
    $DATABASE_USER = 'root';
    $DATABASE_PASS = '';
    $DATABASE_NAME = 'products';
    //Try - Catch for exception with PDO
    try {
        return new PDO('mysql:host=' . $DATABASE_HOST . ';dbname=' . $DATABASE_NAME .
            ';charset=utf8',$DATABASE_USER,$DATABASE_PASS);
    }catch (PDOException $e){
        exit('Failed to connect to database!');
    }
}
//Template
function template_header($title){
    //Cantity of articles on cart
    $num_items_in_cart = isset($_SESSION['cart'])?count($_SESSION['cart']):0;
    echo <<<EOT
        <!DOCTYPE html>
        <html>
        <head>
        <meta charset="utf-8">
        <title>$title</title>
        <link href="style.css" rel="stylesheet" type="text/css">
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
        </head>
        <body>
        <header>
            <div class="content-wrapper">
                <h1>Cos cumparaturi exemplu</h1>
                    <nav>
                    <a href="index.php">Home</a>
                    <a href="index.php?page=products">Produse</a>
                    </nav>
                <div class="link-icons">
                    <a href="index.php?page=cart">
                    <i class="fas fa-shopping-cart"></i>
                    <span>$num_items_in_cart</span>
                    </a>
                </div>
            </div>
        </header>
        <main>
        EOT;
}
// Template footer
function template_footer() {
    $year = date('Y');
    echo <<<EOT
        </main>
        <footer>
            <div class="content-wrapper">
            <p>&copy; $year, cos cumparaturi exemplu didactic</p>
            </div>
        </footer>
        </body>
        </html>
    EOT;
}
?>

What I have tried:

error is:

Parse error: syntax error, unexpected end of file on line 62

i changed many things and tried,but this error always exist. please,say,what does this error mean? and what actually error is ?

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50
  • 1
    Did you search for other questions here related to the same topic? Maybe you can check this thread to find a solution for yourself https://stackoverflow.com/questions/11482527/parse-error-syntax-error-unexpected-end-of-file-in-my-php-code – senior_freshman Dec 09 '20 at 08:02
  • What is your PHP version? – Koala Yeung Dec 09 '20 at 08:03
  • If you read [the manual](https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc) about heredoc, it has a note saying: _"Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon."_. You have indented your closing EOT's. – M. Eriksson Dec 09 '20 at 08:06
  • 1
    Does this answer your question? [Parse error: Syntax error, unexpected end of file in my PHP code](https://stackoverflow.com/questions/11482527/parse-error-syntax-error-unexpected-end-of-file-in-my-php-code) – SwissCodeMen Dec 09 '20 at 09:52

1 Answers1

0

Both your template header and footer functions have string in a Heredoc syntax:

function template_footer() {
    $year = date('Y');
    echo <<<EOT
        </main>
        <footer>
            <div class="content-wrapper">
            <p>&copy; $year, cos cumparaturi exemplu didactic</p>
            </div>
        </footer>
        </body>
        </html>
    EOT;
}

According to the documentation, PHP prior to 7.3 requires the closing of Heredoc to be at the beginning of a line:

function template_footer() {
    $year = date('Y');
    echo <<<EOT
        </main>
        <footer>
            <div class="content-wrapper">
            <p>&copy; $year, cos cumparaturi exemplu didactic</p>
            </div>
        </footer>
        </body>
        </html>
EOT;
}

Thus if you're using PHP before 7.3, parser will not have treat the indented EOT; as the end of the string. And since the string quote was never closed before the end of file, the PHP interpreter would have trouble understanding your script. Therefore it gives you the "unexpected end of file" error.

In the original documentation:

Warning: It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS. The closing delimiter must also be followed by a newline.

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

Example #1 Invalid example

<?php
class foo {
    public $bar = <<<EOT
bar
    EOT;
}
// Identifier must not be indented
?>

Example #2 Valid example

<?php
class foo {
    public $bar = <<<EOT
bar
EOT;
}
?>

Heredocs containing variables can not be used for initializing class properties.

Koala Yeung
  • 7,475
  • 3
  • 30
  • 50