Jump to content

Can someone explain PHP's <<<EOT syntax without referring me to the PHP manual?


n0sleeves
 Share

Recommended Posts

I'm following the FAQ post here / trying to create password protected page. The code they supplied was this: 

 

<?php

if ($_POST['awesome_password'] != $page->page_password) {

echo <<<EOT
<h1>Login</h1>
    <form name='form' method='post' action='$page->url'>
    <input type='password' title='Enter your password' name='awesome_password' />
    <br />
    <input type='submit' name='Submit' value='Login' />
	</form>
EOT;
} else {
echo "<p>This is the protected page. Content goes here.</p>";
}

?> 

That works beautifully. However, I want to spit out more than just a simple ECHO in the else statement. In fact, I want some pretty simple Html mixed with php.  Now I tried modifying the original code supplied by adding another <<< thinking that it would work but of course it didn't :P

else {
echo <<<EOD

	
	<!--Begin Table For Catalog Data -->
	<table id="catalog" class="display" cellspacing="0" width="100%">
		<thead>
	    	<tr>
		        <th>Artist</th>
		        <th>Track Name</th>
		        <th>Album</th>
		        <th>Length</th>
		        <th>Songwriter(s)</th>
		        <th>Genre</th>
		        <th><span data-tooltip class="has-tip" title="Displays the following mood / emotion / characteristic">Traits</span></th>
		        <th><span data-tooltip class="has-tip" title="All songs are originals unless designated cover (letter C)">Type</span></th>
		        <th></th>
		    </tr>
		</thead>

		<tbody>

		<?php //Table Loops
		$tracks = $pages->find("has_parent=/artists/, template=track");

		if($tracks) {
		  foreach($tracks as $track) {
		  	echo "<tr>";
		  	echo "<td id='artistName'>" . $track->parent->parent->title . "</td>"; // artist name
		    echo "<td id='trackTitle'>" . $track->title . "</td>";
		    echo "<td id='albumTitle'>" . $track->parent->title . "</td>"; // this is the album name
		    echo "<td id='trackLength'>" . $track->track_length . "</td>"; // track length
		    echo "<td>" . $track->songwriter . "</td>"; // track songwriter

		    echo "<td id='genres'>";
		    foreach ($track->genres as $genre) {
		    	echo $genre->title . "/";
		    };
		    echo "</td>";

		    echo "<td id='traits'>";
		    foreach ($track->moods as $mood) {
		    	echo $mood->title . "/ ";
		    }; 
		    echo "</td>";

		    echo "<td>";
		    // determin cover song or not
		    if($track->song_type != null) {
		    	echo '<img id="logo" src="' . $config->urls->templates . 'img/letterC.png">';
		    } else {
		    	echo "";
		    }; //end determin cover
		    echo "</td>";

		    echo "<td>" . "<a href=' " . $track->song_file->url . " ' class='sm2_button'>Play</a>" ."</td>";
		    echo "</tr>";
		    		
		  }
		}
		?> 

    </tbody>
	</table>
	<!-- End Catalog Table -->

	<!-- Initialize DataTable -->
	<script> 
	$(document).ready(function () {
    	$('#catalog').DataTable();
	} );
	</script>

EOD;
}
?> 

I have never worked with <<< Heredoc syntax before, Apparently you can't put whatever you want between it? No? 

Link to comment
Share on other sites

EOT is only to echo large texts within PHP vars i think....so try this untested...

else {
//ending PHP part start HTML
?> 

	
	<!--Begin Table For Catalog Data -->
	<table id="catalog" class="display" cellspacing="0" width="100%">
		<thead>
	    	<tr>
		        <th>Artist</th>
		        <th>Track Name</th>
		        <th>Album</th>
		        <th>Length</th>
		        <th>Songwriter(s)</th>
		        <th>Genre</th>
		        <th><span data-tooltip class="has-tip" title="Displays the following mood / emotion / characteristic">Traits</span></th>
		        <th><span data-tooltip class="has-tip" title="All songs are originals unless designated cover (letter C)">Type</span></th>
		        <th></th>
		    </tr>
		</thead>

		<tbody>

		<?php //Table Loops   --starts PHP is correct for the loop for your tracks
		$tracks = $pages->find("has_parent=/artists/, template=track");

		if($tracks) {
		  foreach($tracks as $track) {
		  	echo "<tr>";
		  	echo "<td id='artistName'>" . $track->parent->parent->title . "</td>"; // artist name
		    echo "<td id='trackTitle'>" . $track->title . "</td>";
		    echo "<td id='albumTitle'>" . $track->parent->title . "</td>"; // this is the album name
		    echo "<td id='trackLength'>" . $track->track_length . "</td>"; // track length
		    echo "<td>" . $track->songwriter . "</td>"; // track songwriter

		    echo "<td id='genres'>";
		    foreach ($track->genres as $genre) {
		    	echo $genre->title . "/";
		    };
		    echo "</td>";

		    echo "<td id='traits'>";
		    foreach ($track->moods as $mood) {
		    	echo $mood->title . "/ ";
		    }; 
		    echo "</td>";

		    echo "<td>";
		    // determin cover song or not
		    if($track->song_type != null) {
		    	echo '<img id="logo" src="' . $config->urls->templates . 'img/letterC.png">';
		    } else {
		    	echo "";
		    }; //end determin cover
		    echo "</td>";

		    echo "<td>" . "<a href=' " . $track->song_file->url . " ' class='sm2_button'>Play</a>" ."</td>";
		    echo "</tr>";
		    		
		  }
		}
                // ends PHP part again is correct from your code
		?>  

    </tbody>
	</table>
	<!-- End Catalog Table -->

	<!-- Initialize DataTable -->
	<script> 
	$(document).ready(function () {
    	$('#catalog').DataTable();
	} );
	</script>

<?php //start PHP for last closing brace for the if/else contruct
// no need for EOD;
}
?> 

But i'am no real PHP Coder....so be aware this is only a hint and may someone else could do this more fancy ;)

Best Regards

Link to comment
Share on other sites

Mr-fan, I am now a fan because you are the man! It seemed too easy and too good to be true, but that totally worked!!! Thank you!! 
How come I have never seen this method mentioned in the books I am reading is beyond me  ???  Maybe reading the wrong books? lol.

Is there a name for this?

  • Like 1
Link to comment
Share on other sites

Look up "Php heredoc" on Google and you will find everything you need on the subject :)

The first result in the list at Php.net explains it pretty well.

  • Like 1
Link to comment
Share on other sites

Not sure about EOT (so much so that I removed it from the example above) but I'm using the example to echo out a mix of HTML and PHP with loops, etc. I'll post what I've got in the original thread when I get chance if that's of any use?

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

I think your problem was a simple typo, it has to be EOT, not EOD

echo <<<EOT
[...]
EOT;

Not really. Identifier in can be any valid label. This, for an example, is entirely valid:

<?php
echo <<<TEPPO
Hello world.
TEPPO;
  • Like 1
Link to comment
Share on other sites

but that totally worked!!! Thank you!!

i thought it's solved....

...EOT or what ever is named is only for large texts within PHP vars without loops or something else...but may i'm wrong since i'm no pro.

kind regards mr-fan

Link to comment
Share on other sites

Oh wow, someone obviously took my post the wrong way and changed the title of this topic.

Plenty of good examples over on the PHP site with explanations of the functionality so please anyone don't be surprised or offended if anyone here refers you to docs or other sites sometimes or we would repeat ourselves and duplicate content found elsewhere.

  • Like 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...