When creating code, PHP gives us a huge freedom. Since PHP is compiled at runtime, it can be mixed with any text inside the file. Then, when the code is compiled, the content of the file gets to the output (usually your browser).
If you create a small project, using this technique is totally ok. The problem starts when the project starts to grow.
This article is about a small sized project with very few PHP files. In any case, readability is a very important thing. You know the case when you get back to your code after a long time to edit something, and you just are lost. To make the code readable and logically sound, there is a structure that can be used to achieve it.
If the PHP file contains HTML in it, usually it is good to separate the two. The file starts with PHP, containing all the logic needed. All the data that needs to be printed on the screen is added into variables. These variables are then used in the HTML. This clearly separates the logic from the actual data output. So the structure looks something like:
1 2 3 4 5 6 |
<?php // PHP code here $variable = 'Hello, world'; $variable = 'Hello, world!'; ?> <!-- HTML part here --> <div><?php echo $variable; ?></div> |
In the PHP section, where the logic is, you can also separate the logic that handles POST requests for example. It is good practice to create variables that will control such events and that hold information about these events. These variables than can control the HTML part accordingly. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php $error = ''; $post_var = ''; $was_post = false; // Some data collecting logic $variable = 'Hello, world!'; if ($_POST) { $error = 'Sample error.'; $post_var = 'Goodbye'; $was_post = true; } ?> <?php if ($error != ''): ?> <div style="color:red;"><?php echo $error; ?></div> <?php endif! ?> <div><?php echo $variable; ?></div> <?php if ( $was_post ): ?> <div><?php echo $post_var; ?></div> <?php endif; ?> <form action="" method="post"> <input type="submit" value="POST"> </form> |
This makes your code clear and if needed, later you can separate the logic from the actual html. Using such a structure is beneficial for you. Since PHP is a very loose language, you are the one that needs to pay attention to such things, but you will not regret it.
Hi there to all, as I am actually eager of reading this web site’s post to be updated regularly.
It contains good stuff.
Heya i am for the first time here. I found this board and I
find It truly helpful & it helped me out a lot. I hope to provide one
thing again and aid others such as you aided me.
If you desire to grow your know-how just keep visiting this website and be updated with the newest news update posted here.
Good post! We are linking to this particularly great article
on our site. Keep up the good writing.
Very nice article. I absolutely love this website. Keep writing!
Aw, this was an incredibly good post. Spending some time and actual effort to make a really good article… but what can I say…
I procrastinate a whole lot and don’t seem to get anything done.