-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20. Function.php
118 lines (94 loc) · 2.92 KB
/
20. Function.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
// Function is a piece of code that takes another input in the form of a parameter, processes it, and then returns a value.
// Call the function just write it name followed by ()
function name()
{
echo "dalveer singh <br>";
}
name();
// Parameters multi time used
// Declared within the parentheses of the function definition.
// Serve as placeholders for the values that will be passed to the function
function name1($fnam, $lnam)
{
echo "Hello $fnam $lnam <br>";
}
name1("dalveer", "singh");
name1("jaipur", "Royal green");
// Functions with return value and percentage
function sum($c, $java, $php, $react)
{
$s = $c + $java + $php + $react;
return $s;
}
function percentage($st)
{
$per = $st / 3 . "% <br>";
echo $per;
}
$total = sum(62, 51, 83, 96);
echo $total;
percentage($total);
// Functions with Arguments
// PHP supports passing arguments by value (default), passing by reference, and even default argument values.
// Passing Arguments by Value:
// A copy of the argument's value is passed to the function, so changes inside the function do not affect the original variable.
function test($string) {}
$str = "This is a Passing Arguments by Value <br>";
test($str);
echo $str;
// Passing Arguments By reference:
// The original variable is passed to the function, so changes inside the function directly modify the original variable.
// This is indicated by using the & symbol before the parameter name in the function definition.
function testing(&$string)
{
$string = "this is not a Passing Arguments By reference <br>";
}
$str = "This is a Passing Arguments by Value <br>";
testing($str);
echo $str;
// Variable Functions call
// Call used in variable
function wow($na)
{
echo "Hello $na";
}
$func = "wow"; // Store function name in variable
$func("dharmveer singh <br>"); // Call function using variable
// Anonymous Functions
// Anonymous function, also called a closure, is a function without a specified name
$sayhello = function ($name) {
echo "Hello $name";
};
$sayhello("dalveer singh <br>");
// Recursive Function
// A function that calls itself in its own body.
function display($number)
{
if ($number <= 5) {
echo "$number <br>";
display($number + 1);
}
}
display(1);
// Factorial Number Concept
// A factorial, denoted by is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120.
// It's a mathematical concept, not a programming concept, though it can be calculated using code.
// First program Factorial used in if else statement
function factorial($n)
{
if ($n <= 1) {
return 1;
} else {
return ($n * factorial($n - 1));
}
}
$number = 5;
echo "Factorial of $number is: " . factorial($number);
// Second Program Factorial used for Loop
$number = 5;
$factorial = 1;
for ($i = 1; $i <= $number; $i++) {
$factorial *= $i;
}
echo " <br> Factorial of $number is: $factorial ";