PHP Functions

PHP Functions

PHP function is a block of code that can be used many times when we need. PHP has a thousands of internal or built-in functions that we can use within our PHP scripts to perform a specific task.
A function will not execute when a page loads. It executed by a calling a function. We can create custom functions in PHP.

PHP User Defined Function

We can declare a function in php with the word “function”. Function names are NOT case-sensitive in php. function name can be start with a letter or underscore but it can be start with a number.

Syntext

 
function functionName() {
    code to be executed;
}

Example

<?php
function welcomemsg() {
    echo "Welcome to Webcodeschool.com";
}
welcomemsg(); // call the function
?>

PHP Function With Parameters

An Parameters is just like a variable. Information can be passed to functions through Parameters. Parameters are specified after the function name, inside the parentheses. You can add multiple Parameters as you want, just separate them with a comma.

Syntex

function myFunc($param1, $param2){
    // Code to be executed
}

Example

<?php
// Declaring function
function getadd($val1, $val2){
  $sum = $val1 + $val2;
  echo "Sum of the both numbers  is : $sum";
}
 // Calling function
getadd (100, 10);
?>