functions php code example

Example 1: function php

function functionName() {
    //code to be executed;
}

Example 2: how to define function in php

<?php
function writeMsg() {
    echo "Hello world!";
}

writeMsg(); //call the function
?>

Example 3: wordpress functions.php

/**
 * MyFirstTheme's functions and definitions
 *
 * @package MyFirstTheme
 * @since MyFirstTheme 1.0
 */
 
/**
 * First, let's set the maximum content width based on the theme's design and stylesheet.
 * This will limit the width of all uploaded images and embeds.
 */
if ( ! isset( $content_width ) )
    $content_width = 800; /* pixels */
 
if ( ! function_exists( 'myfirsttheme_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which runs
 * before the init hook. The init hook is too late for some features, such as indicating
 * support post thumbnails.
 */
function myfirsttheme_setup() {
 
    /**
     * Make theme available for translation.
     * Translations can be placed in the /languages/ directory.
     */
    load_theme_textdomain( 'myfirsttheme', get_template_directory() . '/languages' );
 
    /**
     * Add default posts and comments RSS feed links to <head>.
     */
    add_theme_support( 'automatic-feed-links' );
 
    /**
     * Enable support for post thumbnails and featured images.
     */
    add_theme_support( 'post-thumbnails' );
 
    /**
     * Add support for two custom navigation menus.
     */
    register_nav_menus( array(
        'primary'   => __( 'Primary Menu', 'myfirsttheme' ),
        'secondary' => __('Secondary Menu', 'myfirsttheme' )
    ) );
 
    /**
     * Enable support for the following post formats:
     * aside, gallery, quote, image, and video
     */
    add_theme_support( 'post-formats', array ( 'aside', 'gallery', 'quote', 'image', 'video' ) );
}
endif; // myfirsttheme_setup
add_action( 'after_setup_theme', 'myfirsttheme_setup' );

Example 4: php function

<html>
   
   <head>
      <title>Writing PHP Function with Parameters</title>
   </head>
   
   <body>
   
      <?php
         function addFunction($num1, $num2) {
            $sum = $num1 + $num2;
            echo "Sum of the two numbers is : $sum";
         }
         
         addFunction(10, 20);
      ?>
      
   </body>
</html>

Example 5: php functions

#functions

<?php
    #function - a block of code that can be repeatedly called

    /*
    How to format functions
    1. Camel Case myFunction()
    2.Lower case with underscore my_function()
    3. Pascal Cae - MyFunction() usally used with classes
    */
    function simpleFunction(){
        echo 'Hello John';

    }
    //Run the function like so
    simpleFunction();

    //function with param
    function sayHello($name = " you out there!"){
        echo "<br>and<br> Hello $name<br>";
    }
    sayHello('John');
    sayHello();

    //Reurn Value
    function addNumbers($num1, $num2){
        return $num1 + $num2;
     }
     echo addNumbers(2,3);

     // By Reference

     $myNum = 10;

     function addFive($num){
         $num += 5;
     }

     function addTen(&$num) {
         $num += 10;
     }

     addFive($myNum);
     echo "<br>Value: $myNum<br>";

     addTen($myNum);
     echo "Value: $myNum<br>";


?>