简短回答:是的,可以在用户定义的函数中使用标志。
简而言之,A 标志的用例有以下三种:
- 内置函数标志
- 一个 PHP 扩展函数标志
- 用户定义的函数标志
一个内置的php函数,可以看到:
echo PREG_OFFSET_CAPTURE."<br>";// output 256
一个 php 扩展,安装后,你可以使用它的常量,就好像它们是核心 php 函数标志一样,
如果您想为自己的函数定义自己的标志,您可以:
define ("LETTER", "letters");
define ("NUMBER", "numbers");
function newFootnote($text,$flag){
if ($flag==NUMBER){
// code to display foot notes as numbers
echo "The numbers are :$text <br />";
}elseif($flag==LETTER){
// code to display foot notes as letters
echo "The letters are :$text <br />";
}else{
// choose between a default behavior, or rising an error
echo "error <br />";
}
}
newFootnote (" some random text to test", NUMBER); // output The numbers are : some random text to test
newFootnote (" some random text to test", LETTER); // output The letters are : some random text to test
newFootnote (" some random text to test",NULL); // error
但是,我更喜欢使用以下格式:newFootnote(LETTER) 或 newFootnote(NUMBER)
实际上你不能这样调用它,因为你必须提供第一个参数是 $text,你会这样调用它:
newFootnote($text,LETTER);//or
newFootnote($text,NUMBER);
如果你想隐藏常量定义和/或让它们在你的程序中可用,那么你可以将它们放在一个文件中(最好是函数定义)并在需要时包含它,让那个文件成为 notesFunctions.php,把在里面:
define ("LETTER", "letters");
define ("NUMBER", "numbers");
function newFootnote($text,$flag){
if ($flag==NUMBER){
// code to display foot notes as numbers
echo "The numbers are :$text <br />";
}elseif($flag==LETTER){
// code to display foot notes as letters
echo "The letters are :$text <br />";
}else{
// choose between a default behavior, or rising an error
echo "error <br />";
}
}
然后在你的程序中,根据文件的位置(这里假设它们在同一个文件夹中),你把:
include ("notesFunctions.php");
/* then you call your function as needed and the output still be the same as
before */
newFootnote (" some random text to test", NUMBER); // output The numbers are : some random text to test
newFootnote (" some random text to test", LETTER); // output The letters are : some random text to test
newFootnote (" some random text to test",NULL); // error