开发者

PHP - cooler function parameter

开发者 https://www.devze.com 2023-01-05 19:02 出处:网络
I have this: function boo($show_one = false){ if(!show_one) return 1; else return 2; } how can I call boo 开发者_开发知识库like this:

I have this:

function boo($show_one = false){
  if(!show_one) return 1; else return 2;
}

how can I call boo 开发者_开发知识库like this:

boo(SHOW_ALL);

instead of boo(false). I see some native php function have parameters like that which make the code more easy to read


define("SHOW_ALL", false);
define("SHOW_ONE", true);

would correspond to your code there. But I'd reccomend using numbers instead of booleans. What if next weeks you decide to have a SHOW_PAGINATED option?


define('SHOW_ALL', true);

function boo($show_one = false){
  if(!$show_one) return 1; else return 2;
}

boo(SHOW_ALL);
0

精彩评论

暂无评论...
验证码 换一张
取 消