PHP-function

<Beginner PHP><Array><Loop><Function><Pre-Def variable><Session><Read Write File><CRUD>


A function name start with letter or underscore but not number or special symbols, it is not case sensitive.
To call the function we write its name.

function sayHello(){
echo "Hello";
}
sayHello();

User defined functions

It is a block of prog. that can be used repeatedly in a program
It executes not automatically but a call.
function functionName(){
//code
}

Function parameters

Single argument

Info can be passed to functions through arguments, which are like variables.
Arguments are specified after the function name and within the parenthesis

function multby2 ($number){
$answ=$number*2;
echo $answ;
}

multby2(3); // result is 6

Multiple arguments

function multy ($n1, $n2) { // $n1, $n2 are called parameters (variable)
echo $n1/$n2;
}

multy (3,6); // 3 and 6 are called arguments (value)

Default arguments

function stct($num=10){
echo "counter".$num;
}

stct(42) // 42
stct () // 10

Function  - return

A function returns a value using the return statement.
Return stops the function execution and send the value back to calling code, next statement after return is not executed
Function is not called, echo is not there in the function.
A function can not return multiple values.


function mult($num1,$num2){
$res=$num1*$num2;
return $res;
}

echo mult(8,3);

Comments

Popular posts from this blog

Programming- AI, machine learning

Programming -- JS, C, C++, Python

links - Robotic