I've programmed for many years now in c++/c/python but never did a project in PHP, which I will be doing soon.
1- I find the usage of echo
to be somewhat annoying, especially for printing long stuff. For example, if I have a function which wants to echo
a redundant part of a website, such as the header or a nav menu, what approach would be better.
<?php
function PrintHeader() {
[some php stuff, mysql queries, etc]
?>
some HTML code
more HTML code
<?php
[some closing php stuff]
}
?>
Is it better than
<?php
function PrintHeader() {
[some php stuff, mysql queries, etc]
echo 'some HTML code'
echo 'more HTML code'
[some closing php stuff]
}
?>
I find somewhat the first approach to be, to me, simpler, because you can modify the HTML code more easily without the echo
s. I tested it, it works, but is it a good approach? Is it valid?
2- Is it considered a good programming practice to use a function to echo a redundant part of a website, rather than copy/pasting the redundant code accross all PHP files?
These questions might sound obvious to some, but I have not much experience about code management in PHP. I always liked to reduce code redundancy as much as possible; it's easier to maintain.