Wordpress

Why PHP is bad and why it isn’t

Nowadays programmers consider PHP as a very bad programming language. One of the example is in this comic strip, saving the princess with programming language. But why is PHP considered bad and why does it is very popular out there?

The good

In general, PHP is a good language to start learning programming with.

It’s easy to setup and start

PHP is very easy to setup, especially for beginner. Just use XAMPP (for windows) and LAMP (for linux), and drop the code in htdocs and everything will go well. Just search in google for “hello world php xampp” or “hello world php lamp” and you’re good to go.

Furthermore it’s one of the easiest language to setup shared hosting, making it very easy to make your own website.

It’s very forgivable

PHP is dynamic typing, meaning you don’t need to specify whether an variable is string, int, specific class, etc. And it’s string concatenation is different with numeric additional, making it less ambiguous than javascript’s dynamic and don’t need type conversion. It’s very easy for beginner to start with.

And PHP variables works very well with HTML. Almost all native variables can be printed to screen by using echo, while array and object need special treatment.

Furthermore, using undefined variable only resulting in notice, and can be easily suppressed. But beware, both are considered “bad habit” in programming, so take it as learning features. There are also more exceptions that usually result in error in other language, that can easily suppressed in PHP.

It’s both procedural and OOP

PHP can serve procedural code, and OOP one. It’s very common to start learning programming with procedural, and learning OOP next, and it’s easier in same language.

Furthermore, PHP is a C-like syntax programming language, and there are many good languages in C-like syntax, like Java, C# and javascript. It’s C-like syntax is better than python (which is also a good starting language) “if” you aim to move later to those language.

Frameworks and tutorials are abundant

With many framework and tutorials out there, someone can search any problem or topics that they currently worked at, and finding many pages of google results. It’s very easy to find answers to PHP problems nowadays.

Furthermore, many PHP framework are using MVC (Model View Controller) pattern, which is one of the most common pattern in web programming. Learning them can help transition to other good languages using MVC pattern, such as Java MVC spring, C# Asp.Net MVC, NodeJs MVC frameworks and many more.

Furthermore nowadays PHP has composer, which is good to handle library as packages, which is almost all new languages use. And PHP has many CMS which make creating webpage like wordpress CMS easy.

The bad

So why is PHP considered bad? Well you need to at least good in programming to know it’s limitation and bad side.

It is not strong, static typed

PHP starts as dynamic, weakly typing language, helping to customize HTML pages ages ago. Up to this day, it still support dynamic typing, while supporting some type hinting at arguments and property level. While dynamic typing is good to start learning programming, it’s not good at complex business process.

However, being interpret language means the type hinting can only trigger when executed. So we won’t get any type error up until the portion of code is executed, as opposed to Java/C# where it can be caught compile time.

Moreover, PHP7, even after getting scalar type hinting for string and int, still not having generics for array. Without any means to type checking array, it’s harder to do type checking and enforce reliability, especially in business process (accounting).

It doesn’t have multithreading options by native

Without using additional components “PThreads”, PHP doesn’t have any options to emulate multithreading. It isn’t that PHP cannot do multithreading, however the problem lies in how “PThreads” works. It copy the current “process” state (loaded classes, etc) into another process and execute them concurrently.

In my experience with PThreads for PHP 5.6, (maybe I just lack configuration, correct me if so) PThreads use bigger memory than other programming languages, notably C#, Java and NodeJs. Moreover it’s harder to catch exception and to debug process spawned by threads.

So it doesn’t support multi-core process

In case of heavy background process or batch processing, most of the time multi-core support is a requirement.

It doesn’t have memory-persistence cache

PHP is run-and-forget scripting language, which load all it’s needed reference class on beginning of request (and during execution for lazy loading one), and to flush them later. The process takes time, and while PHP7 doing JIT to cache some of it’s code, it’s still not efficient because they need to be loaded for every request.

In contrast with PHP’s scripting, NodeJs and C# Asp.Net MVC (haven’t use java, but should be similar) run a server, and keeping the loaded classes (scripts) in memory, making them more efficient.

It’s dynamic typing takes too much memory

Looks like it’s mitigated in PHP 7, however in PHP 5.6 below, the dynamic variable in PHP takes too much memory. It’ll soon be a hassle when working with big variables, big file or many records of data.

And even if PHP7 is more efficient, it still can’t beat C/C++ level of memory usage per variable. And arguably, so do as in comparison with static typed language, such as Java and C# and the currently rising golang.

It’s data access doesn’t support multiple-result sets

Apply for MySql at least (looks like it supported in PostgreSQL). PHP cannot return multiple tables in one query. Let’s say that you have one procedure that returns 3 select queries, PHP MySql driver can only return one.

Many of it’s library support is configured at installation level

Some of the native library for PHP is configured during installation (gcc make and phpize). Some of the examples are zip (–enable-zip), thread safety (–enable-zts) for pthreads. It makes binding configuration to app repository level harder and reduce portability.

In conclusion

PHP is a good language to start programming with, easy to setup and have many libraries / framework / CMS. However in case of advanced use by expert programmers, PHP doesn’t really meet up the requirement.

Advertisements

Further categorizing your wordpress post!

Tags grouping / categorizing tags

During publishing articles, you may think that using “tags” seems too general. Many times you need more categorized tags, or grouping tags. That way, you can make user easier to find similar tags that consists in the category.

For example: You are publishing an “omelette” recipe post. You need to tag them with ingredients (egg, vegetable oil), utensils (stove, frying pan, spatula) and cooking technique (frying). That way, rather than having bunch of unsorted tags like: egg, vegetable oil, stove, frying pan, spatula, frying; you can sort them better. The example will be shown in the following figure:

Wordpress categorized tag

Fig. 1. WordPress categorized tag

Note: thanks to http://www.epicurious.com/recipes/food/views/classic-omelette-15068 for the recipe.

Moreover, if you do some tweaks in your themes template or via plugin, you can display the categorized tags in tidy manner. It’ll be useful when user wants to browse the category itself. The following picture show an example of categorized tags based on omelette recipe:

Wordpress categorized tag lists

Fig. 2. Categorized tag list

Other useful case is when you are writing a post about notebook, where you can further grouping tags to specify OS, RAM size, HDD size, VGA type, VGA size, etc. So further categorizing tags will be very useful generally.

Tag categorizing is referred by wordpress as “Taxonomy“. However even though fully supported by wordpress, it’s not easy to administrate taxonomy. I suspect that the first intent the taxonomy is developed, it is to be used as specialized themes rather than user customizable. However, with helps from plugins you can easily administrate them.

Plugins required

Types plugin

Types plugin – https://wordpress.org/plugins/types/, is a nice plugin that give you good administrator tools over post types, custom fields and custom taxonomy. Both custom fields and post types are also useful to writing posts, but right now for me the custom taxonomy is the one I need. Here is the example of custom taxonomy page provided by Types plugin:

Wordpress Types plugin custom taxonomies

Fig. 3. Types plugin custom taxonomies page

The administration is rather straightforward here. You only need to create the custom category (ingredient, utensils, cooking technique) then thanks to support of wordpress, all of them will be available in post by itself. The following figures will show how custom taxonomies will show during writing post:

Wordpress custom taxonomy on writing post

Fig. 4. Custom taxonomy on post

As you can see from the Fig. 4 above, in Post menu there are new menu items, each for custom taxonomies defined by user. And during writing post, new box below category will appear where you can fill the custom taxonomies information for the page.

Add actions and filters

Add actions and filters plugin – https://wordpress.org/plugins/add-actions-and-filters/– is a plugin which allow you to add “action hooks” and functions into your wordpress site. Types plugin already help you to maintain custom taxonomies. However, it won’t help you to display the custom taxonomies associated with the post, as shown at Fig. 1. In this case, I use this plugin to show the categorized tag / taxonomies associated with post. I use the following code snippet:

function render_taxonomy($content) {
    $excluded_taxonomies = array(
        'category',
        'post_tag',
        'nav_menu',
        'link_category',
        'post_format'
    );
    $taxonomies = get_taxonomies();
    $post_id = get_the_ID();
    $result = '';

    $result .= '<div class="post_taxonomy">';
    
    foreach($taxonomies as $taxonomy){
        if(in_array($taxonomy, $excluded_taxonomies) == false)
        {
            $obj_taxonomy = get_taxonomy($taxonomy);
            $result .= get_the_term_list($post_id , $taxonomy, '<strong>' . $obj_taxonomy->label . ': </strong>', ', ');
            $result .= '<br/>';
        }
    }
    $result .= '</div>';
    return $content . $result;
}

add_action('the_content','render_taxonomy');

That’s it. With those 2 plugins, your wordpress now has categorized tags.

Showing list of taxonomies

Now that you may want to display the list of taxonomies in one page, as shown under Fig. 2. In this case, I use the following code snippet inside “add actions and filters plugin” to add shortcode. The defined shortcode will render the list of taxonomies defined in your wordpress site. Then, the shortcode can be used in any page / post to instantly change the page / post into taxonomies list.

function render_taxonomies() {
    $excluded_taxonomies = array(
        'nav_menu',
        'link_category',
        'post_format'
    );
    $taxonomies = get_taxonomies();
    
    $result = '';
    $result .= '<div class="taxonomies">';
    
    foreach($taxonomies as $taxonomy){
        if(in_array($taxonomy, $excluded_taxonomies) == false)
        {
            $obj_taxonomy = get_taxonomy($taxonomy);
            $taxonomy_name = $obj_taxonomy->name == 'post_tag' ? 'tag' : $obj_taxonomy->name;
            $result .= '<h2><a name="' . $taxonomy_name . '">' . $obj_taxonomy->label . '</a></h2>';
            
            $query_taxonomies = array( $taxonomy );

            $args = array(
                'orderby'           => 'name', 
                'order'             => 'ASC',
                'hide_empty'        => true, 
                'exclude'           => array(), 
                'exclude_tree'      => array(), 
                'include'           => array(),
                'number'            => '', 
                'fields'            => 'all', 
                'slug'              => '',
                'parent'            => '',
                'hierarchical'      => true, 
                'child_of'          => 0,
                'childless'         => false,
                'get'               => '', 
                'name__like'        => '',
                'description__like' => '',
                'pad_counts'        => false, 
                'offset'            => '', 
                'search'            => '', 
                'cache_domain'      => 'core'
            ); 

            $terms = get_terms($query_taxonomies, $args);
            
            $result .= '<div class="taxonomy"><ul>';
            foreach($terms as $term){
                $result .= '<li>';
                $result .= '<a href="' . get_site_url() . '/' . $taxonomy_name . '/' . $term->name . '">' . $term->name . '</a>';
                $result .= '</li>';
            }
            $result .= '</ul></div>';
        }
    }
    $result .= '</div>';
    return $result;
}

add_shortcode('render_taxonomies', 'render_taxonomies');

Then to use it, just make a page with [render_taxonomies] as the content.

Conclusion

Further categorizing your wordpress site will enhance navigation experience for user.