Jump to content
n0sleeves

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

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? 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

The most common typo is any white-space character before the closing label or after the semicolon.

// This fails, note the space before EOS;
echo <<<EOS
Hello world.
 EOS;

Share this post


Link to post
Share on other sites

Good to know, I never really used it, but I can't remember seeing something different than EOT. 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mithlesh
      Changing it to null
    • By Mithlesh
      Hi, I have one URL - writerrelocations.com/contact-now/

      I have one issue where my header Image is appearing again after the contact form, for your reference:



      I have checked my processwire template section where have not added any Image field besides Header Image, attaching the same for the reference:




      PLEASE help me to get rid of the image below contact form

      Waiting for the swift response!

      Thanks
       
    • By anderson
      Hi,
      For the purpose of learning, as shown in this photo, I created a repeater field "we", then a template, then a page.
      But in /templates/testrepeater.php, I has some problem, the "foreach" part does not work as expected.
      <html> <body> <?php echo "<h1>$page->title</h1><br>"; ?> <? foreach($page->we as $member); ?> <img src="<?php echo $member->images->url; ?>" alt=""> <?php echo $member->wemember; ?><br> <? endforeach;?> </body> </html> Could anybody please help point out my error?
      Thanks in advance.

    • By gregory
      Hi guys, I need help.
      how do I translate Next Page?
      <?php if($page->next->id) {echo "<div class='float-right'><a class='button' href='{$page->next->url}'> Next Page </a></div>";} ?> I usually use this:
      <?php $lang = $user->language->name; if($lang == 'default') {echo "Next Page";} else {echo "Pagina successiva";} ?> or
      <?php echo __("Next Page"); ?> Thanks
×
×
  • Create New...