iwato Posted September 25, 2017 Share Posted September 25, 2017 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 More sharing options...
dsonesuk Posted September 25, 2017 Share Posted September 25, 2017 Echo or escaping and entering by opening/closing php, are both valid, but i think separating php from html is much cleaner as you have it. 1 Link to comment Share on other sites More sharing options...
Gabrielphp Posted September 25, 2017 Share Posted September 25, 2017 (edited) 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 September 25, 2017 by Gabrielphp 1 Link to comment Share on other sites More sharing options...
Ingolme Posted September 26, 2017 Share Posted September 26, 2017 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 } ?> 1 Link to comment Share on other sites More sharing options...
iwato Posted September 27, 2017 Author Share Posted September 27, 2017 (edited) 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 September 27, 2017 by iwato Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now