Tag Info

About

Definition

A function is a sequence of program instructions that perform a specific task. It is packaged as a unit that can be used wherever the according task should be performed.


PHP Example

The following PHP function, for instance located in the file functions.php, prints the blog name with a certain formatting (in this case: bold and red) anywhere we call it:

function styled_blog_name() {
    echo '<b style="color:red;">' . bloginfo('name') . '</b>';
}

Usage:

<p>Welcome to <?php styled_blog_name(); ?>!</p>


JavaScript Example

The following JavaScript function, for instance located in an included JS file, performs a redirect to WordPress Answers:

function WPSE() {
    window.location = "http://wordpress.stackexchange.com";
}

Usage:

<a href="#" onclick="WPSE();">Visit WordPress Answers</a>
history | show excerpt | excerpt history