What does the "<<<" symbol mean? – PHP

  php

Q(Question):

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?

<?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;
?>

A(Answer):

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

A(Answer):

The asterisks kind of look like eitherThe remainder of a comment block, which somebody tried – unsuccessfully – to remove from the code, or part of a custom delimiter. Does the SQL actually work?

A(Answer):

@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.

A(Answer):

@tarek01

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.


$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…..

A(Answer):

I finally figured out what the purpose of the * symbol was for when I stumbled upon this in the script:


$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!

A(Answer):

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

A(Answer):

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

LEAVE A COMMENT