PHP 參數(shù)化函數(shù)
PHP參數(shù)化函數(shù)是帶有參數(shù)的函數(shù)。愛掏網(wǎng) - it200.com您可以在函數(shù)中傳遞任意數(shù)量的參數(shù)。愛掏網(wǎng) - it200.com這些傳遞的參數(shù)在函數(shù)內(nèi)部作為變量使用。愛掏網(wǎng) - it200.com
它們在函數(shù)名后面的括號中指定。愛掏網(wǎng) - it200.com
輸出取決于作為參數(shù)傳遞給函數(shù)的動態(tài)值。愛掏網(wǎng) - it200.com
加法和減法
在此示例中,我們傳遞了兩個參數(shù) $x
和 $y
分別在兩個函數(shù) add() 和 sub() 中。愛掏網(wǎng) - it200.com
<!DOCTYPE html>
<html>
<head>
<title>Parameter Addition and Subtraction Example</title>
</head>
<body>
<?php
//Adding two numbers
function add(x,y) {
sum =x + y;
echo "Sum of two numbers is =sum <br><br>";
}
add(467, 943);
//Subtracting two numbers
function sub(x,y) {
diff =x - y;
echo "Difference between two numbers is =diff";
}
sub(943, 467);
?>
</body>
</html>
輸出:
PHP參數(shù)化 示例2
使用動態(tài)數(shù)字進行加法和減法
在這個示例中,我們在兩個函數(shù) add() 和 sub() 中傳遞了兩個參數(shù) $x
和 $y
。愛掏網(wǎng) - it200.com
<?php
//add() function with two parameter
function add(x,y)
{
sum=x+y;
echo "Sum =sum <br><br>";
}
//sub() function with two parameter
function sub(x,y)
{
sub=x-y;
echo "Diff =sub <br><br>";
}
//call function, get two argument through input box and click on add or sub button
if(isset(_POST['add']))
{
//call add() function
add(_POST['first'],_POST['second']);
}
if(isset(_POST['sub']))
{
//call add() function
sub(_POST['first'],_POST['second']);
}
?>
<form method="post">
Enter first number: <input type="number" name="first"/><br><br>
Enter second number: <input type="number" name="second"/><br><br>
<input type="submit" name="add" value="ADDITION"/>
<input type="submit" name="sub" value="SUBTRACTION"/>
</form>
輸出:
我們通過了以下數(shù)字,
現(xiàn)在點擊”ADDITION”按鈕,我們得到如下輸出。愛掏網(wǎng) - it200.com
現(xiàn)在點擊SUBTRACTION按鈕,我們得到以下輸出。愛掏網(wǎng) - it200.com
聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。