Php 创建一个函数,根据参数打印内容
我想创建一个函数,该函数的参数取值为“开”和“关”Php 创建一个函数,根据参数打印内容,php,Php,我想创建一个函数,该函数的参数取值为“开”和“关” public function test(on || off) { if isset[test(on);] -> {echo "test";} if isset[test(off);] -> {echo "test"; echo "<br>"; echo "great";} } 公共功能测试(开| |关){ 如果设置为[test(on);]->{echo“test”;} 如果设置为[test(off);
public function test(on || off) {
if isset[test(on);] -> {echo "test";}
if isset[test(off);] -> {echo "test"; echo "<br>"; echo "great";}
}
公共功能测试(开| |关){
如果设置为[test(on);]->{echo“test”;}
如果设置为[test(off);]->{echo“test”echo“
“echo”great”}
}
我希望最终的结果是:
public function test(on || off) {
if isset[test(on);] -> {function1();}
if isset[test(off);] -> {function1(); echo "<br>"; function2();}
}
公共功能测试(开| |关){
如果设置为[test(on);]->{function1();}
如果设置为[test(off);]->{function1();echo“
”;function2();}
}
请注意,isset()
用于标识是否定义了变量或数组元素。还要注意的是,on
和off
在PHP中不是预定义的
我能翻译您的伪代码所显示内容的最佳方法是:
<?php
function test($testing) {
if ($testing) {
function1();
}
else {
function1();
echo "<br>";
function2();
}
}
// called as test(true) or test(false)
您的函数可以如下所示:
public function test($mode) {
if($mode=='on'){
function1();
}elseif($mode=='off'){
function1();
echo "<br>";
function2();
}
else{
die('Error: Only "on" and "off" permitted for test');
}
}
公共功能测试($mode){
如果($mode=='on'){
功能1();
}elseif($mode=='off'){
功能1();
回声“
”;
函数2();
}
否则{
模具('错误:测试只允许“开”和“关”);
}
}
定义(“开”,1);
定义(“关”,0);
//在这里,您可以在程序中使用ON或OFF
函数测试($param=OFF){//如果未提供,则默认状态为OFF
如果($param==ON){
功能1();
}否则{
功能1();
回声“
”;
函数2();
}
}
测试(关于)//将调用函数1
测试(关闭);//将调用函数1回显br调用函数2
define("ON",1);
define("OFF",0);
//from here you can use ON or OFF in your program
function test($param = OFF) { //default state if not supplied will be OFF
if($param == ON){
function1();
}else{
function1();
echo "<br>";
function2();
}
}
test(ON); //will callfunction 1
test(OFF); // will call function1 echo br call function 2