Close Menu
    Facebook X (Twitter) Instagram
    Oixiesoft
    • Home
    • Services
      • WordPress Malware Removal
      • Fix WordPress Errors
      • WordPress Website Development
    • Articles
    • Contact
    Oixiesoft
    Home»Tutorial»PHP»PHP Functions
    PHP

    PHP Functions

    Editorial StaffBy Editorial StaffUpdated:May 2, 2022No Comments2 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    PHP functions are equivalent to other programming languages. A function is a part of code that takes one more input in the form of a parameter and does some processing and returns a value.

    There are lots of functions like print_r(), fopen() and fread() etc. All these functions are built-in functions. In PHP, you can also create your own functions as well.

    There are two factors that should be clear to you:

    • Creating a PHP Function
    • Calling a PHP Function

    PHP User-Defined Functions

    A function will be executed by a call to the function.

    Syntax

    function functionName()
    {
    code to be executed;
    }
    

    PHP function approaches:

    • Take the function name that recalls what the function does
    • The function name can begin with a letter or underscore (not a number)

    Example

    function testMsg() {
      echo "Welcome to Oixiesoft!";
    }
    
    testMsg(); // call the function
    

    Output

    Welcome to Oixiesoft!

    PHP Functions with Parameters

    To add extra functionality to a function, we can use parameters. A parameter is just like a variable. The function name is defined before the parameters. And the parameters are specified inside the parentheses ().
    You can add multiple arguments and each argument is separated by a comma.

    Example

    function fruitName($fname) {
      echo " This is a $fname.<br>";
    }
    
    fruitName("apple");
    fruitName("kiwi");
    fruitName("banana");
    fruitName("mango");
    

    Output

    This is a apple.
    This is a kiwi.
    This is a banana.
    This is a mango

    Example2: Multiple Parameters

    function addFunction($num1, $num2)
    {
      $sum = $num1 + $num2;
      echo "Sum of the two numbers is : $sum";
    }
    addFunction(15, 20);
    
    

    Passing Arguments by Reference:

    It is likely to pass arguments to functions by reference. It signifies that a reference to the variable is controlled by the function instead of a copy of the variable’s value.
    Any modifications made to an argument in these circumstances will vary the value of the actual variable. You can pass an argument by reference by using an ampersand (&) to the variable name in either the function call or the function definition.

    Example

    function multiply_six(&$value) {
      $value *= 6;
    }
    
    $num = 3;
    multiply_six($num);
    echo $num;
    
    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Tumblr Email
    Editorial Staff

    Related Posts

    CodeIgniter vs Laravel: A Detailed Side-by-Side Comparison

    Python Arrays

    Python Lambda

    PHP
    • PHP Comments
    • PHP if else Statements
    • The PHP switch Statement
    • PHP Functions
    • PHP for Loop
    • PHP Arrays
    • PHP Include Files
    • PHP Date() and Time() Function
    • PHP Forms Tutorial
    Services
    • Web Development
    • Mobile Application Development
    • WordPress Malware Removal Service
    • Website Design
    • WordPress Development
    • Magento Development
    • Shopify Development
    • SEO Services
    Blog
    • How to Fix the Error Establishing a Database Connection
    • Ping List WordPress
    • How To Fix Japanese Keyword Hack
    • How to remove Malware from WordPress
    Hire Developers
    • Hire WordPress Developer
    • Hire Shopify Developer
    Contact Info
    • Oixiesoft Technologies
      A-40, Block A, I thum Tower, Sector 62, Noida
    • [email protected]
    • Privacy Policy
    • About Us
    • Contact Us
    © 2025 OixieSoft Technologies

    Type above and press Enter to search. Press Esc to cancel.