Jump to content

Interlacing PHP and HTML without echo Statements


iwato

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 comment
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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...