2

My question is: Is it ok, or even preferable, to use html instead of php when possible?

Why I ask: Wordpress seems to use php even for simple tasks, where old good html would do the job. Php code is not famous for its simplicity or readability. Besides this, I guess that all the unnecessary database requests make the serving of the web pages just a bit slower. So I wonder if there is a good reason, I can not think of, that WP prefers to use php instead of html. Example:

<div class="site-info">
<?php do_action( 'twentyfourteen_credits' ); ?>
  <a href="<?php echo esc_url( __( 'http://wordpress.org/', 'twentyfourteen' ) ); ?>"><?php printf( __( 'Proudly powered by %s', 'twentyfourteen' ), 'WordPress' ); ?></a>.
 Copyright ©<?php echo date( 'Y' ); ?>
</div>

instead of

<div class="site-info">
 <a href="http://wordpress.org">Proudly powered by WordPress</a>.
 Copyright ©2015
</div>

(I understand that in the example above, I have to change the year number once per year, and the text can't be translated, but this is IMO an acceptable trade off for nicer, faster code)

  • 1
    Why would you want to translate an Url like http://wordpress.org? – kaiser Sep 8 '15 at 0:00
  • This is taken by the original twentyfourteen theme footer and I think it translates only the "Powered by". But I'm not sure, since php is hard to read (its un-readability is the main reason I'm asking this question). – IXN Sep 8 '15 at 4:42
  • @kaiser that is a good question you should ask one of the core developers as the code above comes from the twentyfourteen theme :-) – Pieter Goosen Sep 8 '15 at 6:10
  • First of all if you use only HTML it will make your website static means take a example of yours copyright ©2015 will same every year and he has added that some action of particular theme. Generally php is preferred when you want to make web page dynamic – WisdmLabs Sep 8 '15 at 8:08
  • You would want to translate URL like wordpress.org to be able to point it to localized version of the site like uk.wordpress.org. – Rarst Sep 8 '15 at 9:17
2

Quite strange that the core developers would decide to make an URL translatable, it is actually a case here of over-doing something. But anyways, lets get to the main question

PHP vs HTML

Although there are many reasons I can think of why one would prefer PHP above HTML, and that goes for the core developers as well, the main reason lies in one big difference between these two different languages

  • PHP is dynamic language and HTML is static language

This means that code written in PHP will update itself according to the condition/s set for that specific operation, it does not need human intervention, while anything written in HTML will remain the same until someone manually updates the code to reflect changes, so it requires human intervention. Not having to manually update a single line of code or a thousand lines of code when changes are required is one of the main reasons PHP is used and prefered above a language like HTML

I do think your main issue is that you are still very new to PHP and do not really understand the language as such, which is actually a great opportunity then to dig in PHP and to learn the basics to get you going

As to your exact question, there is nothing wrong swopping PHP with HTML, but you will loose the dynamic aspect of PHP

  • Indeed I'm new to PHP. And yes I am trying to learn to use it. However I believe is it fair to say it is not an elegant, readable language (like python for example), so I was a bit baffled to see WP using php as much as possible, when blogger for example, makes good use of html when possible. – IXN Sep 8 '15 at 8:11
  • Thank you for the extra info. Have never used python or blogger before so it is always nice to know how other languages and platform differ from the ones we know :-). Enjoy – Pieter Goosen Sep 8 '15 at 8:19
  • Making URLs translatable is a normal practice. It is done so that users in different language can be provided language–specific version of the URL as needed. – Rarst Sep 8 '15 at 9:09
  • @Rarst thanks for that info. I did not really know about that and that made it seem odd. ;-) – Pieter Goosen Sep 8 '15 at 9:23
1

The key distinction is that you are looking at the public theme. It's meant to be used by many sites out there and be is accommodating as possible.

It might be your personal choice to edit year in every footer of every site you own, but for someone else that would seem like waste of (costly) human resources on something PHP can do effortlessly and essentially free. If you profile performance of single date call its impact would be incredibly insignificant on resources.

Same story with just "text can't be translated" downside. This would be incredibly unfitting for people who do need it translated and costly to make it translatable. If I remember right the amount of non–English WP installs worldwide is now a majority of them.

  • Good point. So I guess you agree that if my priorities are simple, short and readable code, it is perfectly ok to replace php with html when possible. – IXN Sep 8 '15 at 9:19
  • Yes, for your own private code. :) – Rarst Sep 8 '15 at 9:20
1

Yes, you can use HTML tags instead of PHP code to print the static content. So it is possible. For example, you may have a website with static content excepting the articles, the comments and an archive page; in that case, you would have the header, the menus, the sidebars, the widgets, the footer and even shortcodes used into an article as static content.

The files will be .php anyway because all the WordPress themes have a PHP structure, so this files need to be interpreted by the server to work. The header content is interpreted from the file header.php, the footer content is interpreted from footer.php, and so on. You can't change the files extension.

Then, if you want to use HTML tags instead of PHP code to have a code «faster», the answer is yes: by reducing the number of database calls you will be able to increase your WordPress speed. Of course.

By the way, the code won't be «nicer». PHP is hard to read if you don't know PHP. (Moreover, HTML is not a programming language, but a markup language. Each one has its functionallity).

PD: There are more efficient and preferable ways to increase the WordPress speed. This is the last method I would use.

0

Well.. it is faster to use strict html, becouse server takes some time to make php functions/actions/code.. but it's less flexable to echo html text than php..

for ex. date( 'Y' ) takes only 2.5033950805664E-5 secconds to echo current date, othere single, not DB functions should take appr. same time to compleat them.

Consider better to use some chaching plugin instead of stripping out php code.

NOTE: you cannot use html instead of php - it's 2 different languages and they surve for different tasks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.