Almost every programming language starts you off with the simple functionality of printing “Hello World” to the screen, such as:

<?php echo 'Hello World'; ?>

… and there we have it. If you copied and pasted that into the appropriate place you would see “Hello World” on your screen.

Now, let’s see how complicated we can make this simple statement … and still have it work.

We can create a function that returns ‘Hello World’ and then echo that function.

<?php
function hello_world() {
	return 'Hello World';
}

echo hello_world();
?>

We could add some parameters to the function:

<?php
function hello_world( $hello = 'Hello', $world = 'World' ) {
	return $hello . ' ' . $world;
}

echo hello_world();
?>

We can introduce some defaults rather than explicitly defining the parameters in the function definition:

<?php
function hello_world( $hello_world_args = '' ) {
	$defaults         = array(
		'hello' => 'Hello',
		'world' => 'World'
	);
	$hello_world_args = wp_parse_args( (array) $hello_world_args, $defaults );
	
	$hello = $hello_world_args['hello'];
	$world = $hello_world_args['world'];

	return $hello . ' ' . $world;
}

echo hello_world();
?>

Before we get too far ahead of ourselves we should ensure this is i18n compatible:

<?php
function hello_world( $hello_world_args = '' ) {
	$defaults         = array(
		'hello' => __( 'Hello', 'wpfa_complicated_hello_world' ),
		'world' => __( 'World', 'wpfa_complicated_hello_world' ),
	);
	$hello_world_args = wp_parse_args( (array) $hello_world_args, $defaults );

	$hello = $hello_world_args['hello'];
	$world = $hello_world_args['world'];

	return sprintf( __( '%1$s %2$s', 'wpfa_complicated_hello_world' ), $hello, $world );
}

echo hello_world();
?>

We should also add some hooks … just in case someone wants to go fishing?!

<?php
function hello_world( $hello_world_args = '' ) {
	$defaults         = array(
		'hello' => __( 'Hello', 'wpfa_complicated_hello_world' ),
		'world' => __( 'World', 'wpfa_complicated_hello_world' ),
	);
	$hello_world_args = wp_parse_args( (array) $hello_world_args, $defaults );

	$hello = apply_filters(
		'wpfa_complicated_hello_world_hello_string',
		$hello_world_args['hello']
	);
	$world = apply_filters(
		'wpfa_complicated_hello_world_world_string',
		$hello_world_args['world']
	);

	return apply_filters(
		'wpfa_complicated_hello_world_return_string',
		sprintf(
			__( '%1$s %2$s', 'wpfa_complicated_hello_world' ),
			$hello,
			$world
		)
	);
}

echo hello_world();
?>

To finish this off we really should be including appropriate comments:

<?php
/**
 * Hello World
 * Returns the default string "Hello World"
 * The string can be filtered by individual word or by the entire phrase.
 *
 * @package    WPFA_Complicated_Hello_World
 * @since      1.0
 * @date       January 2014
 *
 * @link       http://wpfirstaid.com/2014/02/a-complicated-hello-world/
 *
 * @param    string $hello_world_args
 *
 * @uses       __
 * @uses       apply_filters
 * @uses       wp_parse_args
 *
 * @return    mixed|void
 *
 * @todo       Can we make this even more complicated?
 */
function hello_world( $hello_world_args = '' ) {
	/** @var array $defaults - set the default words */
	$defaults = array(
		'hello' => __( 'Hello', 'wpfa_complicated_hello_world' ),
		'world' => __( 'World', 'wpfa_complicated_hello_world' ),
	);
	/** @var array $hello_world_args - uses the defaults if not defined */
	$hello_world_args = wp_parse_args( (array) $hello_world_args, $defaults );

	/** @var string $hello - default "Hello" */
	$hello = apply_filters(
		'wpfa_complicated_hello_world_hello_string',
		$hello_world_args['hello']
	);

	/** @var string $world - default "World" */
	$world = apply_filters(
		'wpfa_complicated_hello_world_world_string',
		$hello_world_args['world']
	);

	/** Return the hello and world variables */

	return apply_filters(
		'wpfa_complicated_hello_world_return_string',
		sprintf(
			__( '%1$s %2$s', 'wpfa_complicated_hello_world' ),
			$hello,
			$world
		)
	);
}

/** End function - hello world */

echo hello_world();
?>

Obviously this was over-kill to output the simple phrase “Hello World” but the code is solid. This was just an exercise in what you can do when you want to output a simple string with WordPress and allow for a wide range of input and potential for modifications by providing various different access points into the data being sent to the screen.

Have some fun, feel free to create your own Rube Goldberg styled code snippet and share it in the comments.