我想讓用戶輸入三個不同的引數而不改變輸出的順序
function check_status($a, $b, $c) {
Some stuff
}
// Needed Output
echo check_status("User", 38, true); // "Hello User, Your Age Is 38, You Are Available For Hire"
echo check_status(38, "User", true); // "Hello User, Your Age Is 38, You Are Available For Hire"
echo check_status(true, 38, "Osama"); // "Hello User, Your Age Is 38, You Are Available For Hire"
echo check_status(false, "User", 38); // "Hello User, Your Age Is 38, You Are Not Available For Hire"
我試過如果陳述句不順利
uj5u.com熱心網友回復:
對于這些情況,您可以使用關聯陣列作為函式引數。
function check_status($params) {
$availability = $params['availability'] ?? false;
$name = $params['name'] ?? '';
$age = $params['age'] ?? 0;'enter code here'
$availableString = $availability ? "available" : "not available";
echo "Hello $name, your age is $age, you are $availableString for hire";
}
uj5u.com熱心網友回復:
您可以檢查變數的型別并連接字串以獲得最后一個。我會做類似的事情:
<?php
function check_status($a, $b, $c) {
$name = null;
$age = null;
$availability = null;
if (is_string($a)) $name = $a;
if (is_string($b)) $name = $b;
if (is_string($c)) $name = $c;
if (is_int($a)) $age = $a;
if (is_int($b)) $age = $b;
if (is_int($c)) $age = $c;
if (is_bool($a)) $availability = $a;
if (is_bool($b)) $availability = $b;
if (is_bool($c)) $availability = $c;
$availableString = $availability ? "available" : "not available";
echo "Hello $name, your age is $age, you are $availableString for hire \n";
}
check_status("John", 26, true);
check_status(26, "John", true);
check_status(true, 26, "John");
check_status(true, "John", 26);
check_status(false, "John", 26);
?>
輸出是:
Hello John, your age is 26, you are available for hire
Hello John, your age is 26, you are available for hire
Hello John, your age is 26, you are available for hire
Hello John, your age is 26, you are available for hire
Hello John, your age is 26, you are not available for hire
這不是最短的方法,它只是一個例子。這都是關于在檢查型別后連接變數。連接可以用sprintf代替
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/537377.html
標籤:PHP拉维
