define function in 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: define in php

//define() is used to create constants
define(name,value);
//here name has to be a string
//here value can be string, integer, float, boolean or NULL, 
//and can be an array to if you are using PHP 7.0+

//define() before php 7.3
define(name,value,case_insensitive);
//case_insensitive is optional and can be TRUE or FALSE by default its false

Example 4: execute function php

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

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>";


?>

Example 6: define php

define("name", {value});