For example, if I wanted to generate some HTML based on some input. Which is the preferred way out of:
<?php
function generate_html($type, $input){
switch($type){
case 'paragraph':
//Generate and return the HTML code for a paragraph
break;
case 'ul':
//Generate and return the HTML code for an unordered list
break;
default:
error('Invalid type specified');
break;
}
}
generate_html('paragraph', 'This is a paragraph');
generate_html('ul', [
'This is the first item in a list',
'This is the second item in a list'
]);
or:
<?php
function generate_paragraph($input){
//Generate and return HTML for a paragraph
}
function generate_ul($input){
//Generate and return HTML for a list
}
generate_paragraph('This is a paragraph');
generate_ul([
'This is the first item in a list',
'This is the second item in a list'
]);
I know about the idea of a function doing only one "thing", but I'm not sure how specific that "thing" should be. The first type could only be doing one thing (generating HTML), or it could be doing two things (generating lists, paragraphs). It depends how you look at it. What would be the preferred way of doing something like this?