What does the "<<<" symbol mean?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tarek01
    New Member
    • Feb 2010
    • 14

    What does the "<<<" symbol mean?

    I understand the following script is for building a database table in MySQL, but does anyone know what are the functions of the <<< in line 3 and * in lines 4 & 10?

    Code:
    <?php
    
    $sql_file=<<<SQL_FILE
    DROP TABLE IF EXISTS `table_name`;*
    CREATE TABLE IF NOT EXISTS `table_name` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(250) default NULL,
      `address` varchar(250) default NULL,
      PRIMARY KEY  (`id`)
    );*
    SQL_FILE;
    
    ?>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Have a look at the heredoc/nowdoc syntax. Not sure about those asterisks though.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      The asterisks kind of look like either
      1. The remainder of a comment block, which somebody tried - unsuccessfully - to remove from the code, or
      2. part of a custom delimiter.

      Does the SQL actually work?

      Comment

      • tarek01
        New Member
        • Feb 2010
        • 14

        #4
        @Markus: Very cool! Never heard or seen the heredoc syntax before. Gotta love your friendly neighborhood forum. Thanks!

        @Atli: Yes, this script was taken out of a working model and it's very likely the asterisk was placed intentionally as it was made by smarties over at PHP Jabbers.

        Comment

        • itsloop
          New Member
          • May 2009
          • 18

          #5
          Originally posted by tarek01
          I understand the following script is for building a database table in MySQL, but does anyone know what are the functions of the <<< in line 3 and * in lines 4 & 10?

          Code:
          <?php
          
          $sql_file=<<<SQL_FILE
          DROP TABLE IF EXISTS `table_name`;*
          CREATE TABLE IF NOT EXISTS `table_name` (
            `id` int(11) NOT NULL auto_increment,
            `name` varchar(250) default NULL,
            `address` varchar(250) default NULL,
            PRIMARY KEY  (`id`)
          );*
          SQL_FILE;
          
          ?>
          this operator is very useful when you rendering HTML with php script beacuse it doesn't requires qutoes for string like traditionally we do.

          "SQL_FILE" you can use any text here in simple language its the way of starting and ending the block. when you write fist time it starts and when you put second it assumes that the block is ended.

          Code:
          $sql_file=<<<SQL_FILE
          <p style="color:#0000ff" onmouseover="alert('hello');">Its an example.</p>
          SQL_FILE;
          
          echo $sql_file;
          You can see i have used both quotes (" and ' ) in my quotes. it really the fun.

          sorry if the code having any minor mistake i just typed this code here i don't having any IDE and Compiler but it will work do check this up guys.....

          Comment

          • tarek01
            New Member
            • Feb 2010
            • 14

            #6
            I finally figured out what the purpose of the * symbol was for when I stumbled upon this in the script:
            Code:
            $sql_statements = split('\;\*', $sql_file);
            for ($i = 0; $i < count($sql_statements) - 1; $i++) {
            	if (!mysql_query($sql_statements[$i], $connection)) {
            		$msg="Failed to submit the query: ".$sql_statements[$i];
            		return false;
            	}
            }
            The coder used the split function to break up $sql_file into an array of individual SQL statements wherever a ;* appears. A for loop is then used to execute each statement individually and, in the event of an error, returns which key of the array caused it.

            Sorry if I confused anyone from my original post!

            PLEASE NOTE: The split function is deprecated!

            Comment

            • itsloop
              New Member
              • May 2009
              • 18

              #7
              but you can use it and also explode function can be used.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                You *can* use it, but you shouldn't. That is why it's deprecated.

                Comment

                Working...