| Viewed 127 times

how to create function in php


how to create function in php

PHP
1 Answer
Mohammad Qandeel
Answered
2

First of all, there are two types of function

  • built-in Functions are shown by the language
  • User-Defined Functions 

built-in Functions

  • like print_r(), var_dump(), var_export()

User-Defined Functions

  • which are programmed by the developer as needed

When writing your function follow these rules

  1. Start with a function word 
  2. Follow with function_name 
    1. can start with string or underscore 
    2. not case-sensitive
    3. can`t start with number
    4. can`t use PHP Reserved words

Syntax

function function_name(){

// any code 

}

 

Example 

<?php
    function say_hello(){
        echo 'hello';
    }
?>

The function is called by its name with brackets 

say_hello();

 

Example

<?php
    function say_hello(){
        echo 'hello';
    }
    say_hello();
?>

Output

hello