CodesJava

Easy learning with example program codes

PHP Default Arguments


If we call a PHP function without arguments, PHP functions takes the default value as an argument, just like in C++.
Example 1: Code for Default Argument for a function with a single argument.

<!DOCTYPE html>
<html>
<body>
 
<?php
function weight($defaultweight = 60) {
	echo "The weight is : $defaultweight <br>";
}
weight(45);
weight();
weight(70);
weight(80);
?>
 
</body>
</html>

Output

The weight is : 45
The weight is : 60
The weight is : 70
The weight is : 80

Example 2: Code for Default Argument for a function with more than one argument.

<!DOCTYPE html>
<html>
<body>
 
<?php
function hw($weight = 60, $height = 5) {
	echo "The height and weight is : $height, $weight<br>"; 
}
hw(45,4);
hw();
hw(70,6);
hw(80,5);
?>
 
</body>
</html>

Output

The height and weight is : 4, 45
The height and weight is : 5, 60
The height and weight is : 6, 70
The height and weight is : 5, 80
Posted in PHP   


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap