By D. Winning


2018-11-08 23:10:07 8 Comments

I am using <<<heredoc heredoc; inside a function (in a class) and it messes up the syntax highlighting of all the code below it.

It's fine using it outside the function - or on a single line within a function:

php

But if I use it in a function (not on a single line), it messes up the highlighting below it, and my editor (same in Atom or Sublime Text) seems to think it closes with the one outside the function and class.. what's happening?

php

<?php

class SimpleCMS {
    var $host = 'localhost';
    var $username = 'root';
    var $password = '';
    var $table = '';

    public function display_public() {

    }

    public function display_admin() {
        return <<<ADMIN_FORM 
        ADMIN_FORM;
    }

    public function write() {

    }

    public function connect() {
        mysql_connect($this->host, $this->username, $this->password) or die('Could not connect to the database. ' . mysql_error());
        mysql_select_db($this->table) or die('Could not select database. ' . mysql_error())

        // build the database
        return $this->buildDB();
    }

    private function buildDB() {
        $sql = <<<MySQL_QUERY CREATE TABLE IF NOT EXISTS testDB (title VARCHAR(150), bodyText TEXT, created VARCHAR(100)) MySQL_QUERY;

        return mysql_query($sql);
    }
}

<<<ADMIN_FORM

ADMIN_FORM;
?>

1 comments

@Dai 2018-11-08 23:13:29

Your heredoc terminator needs to be in the leftmost column, i.e. without indentation. This is documented on PHP's website: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

The closing identifier must begin in the first column of the line.

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.

Change your current code:

    public function display_admin() {
        return <<<ADMIN_FORM
        ADMIN_FORM;
    }

To this:

    public function display_admin() {
        return <<<ADMIN_FORM
ADMIN_FORM;
    }

@paulsm4 2018-11-08 23:15:12

Cool - I assumed this was some limitation in the editor. Thank you for the info!

@Paul Crovella 2018-11-08 23:15:51

@ArtisticPhoenix 2018-11-08 23:17:01

@PaulCrovella - about time, I say!!

@D. Winning 2018-11-08 23:23:13

Oh man, that's so ugly :'( thanks for the quick and helpful answer though!

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] PHP MySQL database problem

  • 2010-04-27 18:10:01
  • Jorge
  • 988 View
  • 1 Score
  • 3 Answer
  • Tags:   php mysql

0 Answered Questions

PHP mySQLi connection to AWS RDS in AWS EC2 Instance

3 Answered Questions

[SOLVED] AJAX HTML PHP question

  • 2010-04-27 16:17:27
  • Jorge
  • 783 View
  • -1 Score
  • 3 Answer
  • Tags:   php html ajax

4 Answered Questions

[SOLVED] One MySQL Database & Multiple Table

  • 2013-12-13 16:04:39
  • Athirah Hazira
  • 2323 View
  • 1 Score
  • 4 Answer
  • Tags:   php mysql database

11 Answered Questions

[SOLVED] PHP alternating colors

  • 2010-05-31 01:51:29
  • Jorge
  • 1740 View
  • 1 Score
  • 11 Answer
  • Tags:   php

1 Answered Questions

Call an external function inside wordpress code

5 Answered Questions

[SOLVED] mysql_insert_id() not returning a value -

2 Answered Questions

[SOLVED] 2 issues: Call to undefined function and no writes to database

  • 2012-07-12 20:24:51
  • Ramin
  • 78 View
  • 0 Score
  • 2 Answer
  • Tags:   php mysql

4 Answered Questions

[SOLVED] converting text to int from a form

  • 2010-09-08 07:41:00
  • saadlulu
  • 3110 View
  • 0 Score
  • 4 Answer
  • Tags:   php forms

2 Answered Questions

Sponsored Content