Jump to content

Interlacing PHP and HTML without echo Statements


Recommended Posts

Please consider the following two sets of code.  Are they not identical in their functionality?  

BACKGROUND:  I am troubled by the following set of code, for it intertwines PHP and HTML in a way that, for me, is neither intuitive, nor reasonable based on my limited knowledge of how PHP and HTML work.  This said, it appears to work. 

<?php
	if(isset($_GET['edit']))
	{
?>
<button type="submit" name="update">update</button>
<?php
	} else {
?>
<button type="submit" name="save">save</button>
<?php
	}
?>

ANALYSIS:  Had I written the code I would have felt compelled to enter the <button> elements as part of PHP echo statements. 

QUESTION:  Is this proper coding technique?

Roddy

 

Link to post
Share on other sites

If you are doing such coding use the : form and not { }, it will be much easier to understand the code when you mix them.

<?php

if(condition): ?>

Html here

<?php else: ?>

Html here

<?php endif; ?>

 

Edited by Gabrielphp
  • Thanks 1
Link to post
Share on other sites

The problem with using echo statements is that code editors will not highlight the HTML syntax. If you're building a template file, you usually have large amounts of HTML with small amounts of PHP.

I personally do not like the BASIC style syntax even for templates. When you use curly braces, most code editors will show you where the current brace ends and allow you to show or hide the block of code.

With proper formatting and indentation the code becomes easier to read.

<?php if(isset($_GET['edit'])) { ?>
	<button type="submit" name="update">update</button>
<?php } else { ?>
	<button type="submit" name="save">save</button>
<?php } ?>

 

  • Like 1
Link to post
Share on other sites
Quote

When you use curly braces, most code editors will show you where the current brace ends and allow you to show or hide the block of code.

Very useful information, Ingolme.  Thanks!

Except for the fact that it does not appear to be true for BBEdit -- my code editor.  BBEdit phrases the open and close PHP tags instead.

Edited by iwato
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...