Use of functions in PHP

Author: Al-mamun Sarkar Date: 2020-03-23 08:45:17

আজকে আমরা দেখবো কিভাবে PHP Function ব্যবহার করতে হয়। এবং Function এ Required and Optional Parameter ব্যবহার করতে হয়। 

 

Source Code:

 

function:

function hello() {
	echo "Welcome to Dhaka";
	echo "<br/>";
	echo "Go Home";
}


function sum() {
	$a = 12;
	$b = 25;
	$c = $a + $b;
	echo "Sum is : $c <br/>";
}
sum();


function sumTwoNumber($a, $b) {
	$c = $a + $b;
	echo "Sum is : $c <br/>";
}

sumTwoNumber(12, 25);
sumTwoNumber(10, 20);

 

function with default parameter:

function message($city = 'Dhaka') {
	$a = "Welcome to $city <br/>";
	echo $a;
}

$c = 'Gazipur';
message($c);
message();

 

Optional Parameter with required parameter:

function sum($a, $b=5) {
	$sum = $a + $b;
	echo "Sum is $sum <br/>";
}

sum(14);
sum(14, 12);

 

Function Return:

function sum($a, $b=5) {
	$sum = $a + $b;
	return $sum;
}

$ans = sum(14);
echo "Answer is : $ans <br/>";

echo '2nd Answer is : ' . sum(14, 12) . '<br/>';

echo '2nd Answer is : ' . (sum(14) + sum(14, 12));