0

I have a footnote function that I've created. I want to give users the option to have their footnotes appear as numbers or letters. Here is how I could to call the function:

newFootnote($text,"letters")
newFootnote($text,"numbers")
newFootnote($text,true)
newFootnote($text,false)

If using bool, true could be to use numbers and false could be to use letters. However, I would prefer to use the format: newFootnote(LETTER) or newFootnote(NUMBER)

I know built-in functions have flags that you case use. For example, preg_match_all has PREG_OFFSET_CAPTURE, PREG_PATTERN_ORDER, etc. Is it possible to use a flag in a user-defined function?

Achraf JEDAY
  • 1,936
  • 5
  • 24
  • 33
theglossy1
  • 543
  • 3
  • 13
  • This is not a duplicate question as indicated... it is quite different. In that question, they are actually defining the constant. I'm asking how to pass a flag for a constant that isn't defined, as flags are passed to built-in functions like preg_match_all, etc... – theglossy1 May 20 '17 at 03:57

3 Answers3

0

yes, use define() to define a named constant aka flag

define('NAMED', 'letters');
newFootnote($text, NAMED);
jbe
  • 1,722
  • 1
  • 12
  • 20
  • I know that you can define it beforehand as you indicated, but my point was that things like PREG_OFFSET_CAPTURE are not defined before they are used. They must all be defined by PHP's core I guess... right? – theglossy1 May 20 '17 at 03:52
  • correct, flags are constants defined by php which map to a number – jbe May 20 '17 at 09:07
0

For global scope

define('NUMBERS',true);
define('LETTERS',false);

In classes you can use class constants also.

Sal
  • 1,307
  • 1
  • 8
  • 16
  • I know that you can define it beforehand as you indicated, but my point was that things like PREG_OFFSET_CAPTURE are not defined before they are used. They must all be defined by PHP's core I guess... right? – theglossy1 May 20 '17 at 03:54
  • 1
    Right. There are **predefined constants** defined at different levels: core, extension, framework, etc.. – Sal May 20 '17 at 15:01
  • That comment answers my original question. Your answer does not. However, since you're the guy that answered the question (albeit in a comment), I will mark the answer correct. Thanks much. – theglossy1 May 22 '17 at 01:48
0

Short answer : Yes, it is possible to use a flag in a user-defined function. In a nutshell, A flag uses cases are three :

  • a built in function flag
  • a PHP extension function flag
  • a user defined function flag

A built in php function, you can see that:

echo PREG_OFFSET_CAPTURE."<br>";// output 256

A php exntesion, when installed, you could use its constants, as if they were core php functions flags,

If you want to define your own flags for your own function, you can :

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 

However, I would prefer to use the format: newFootnote(LETTER) or newFootnote(NUMBER)

actually you can't call it that way, because you have got to provide the first parameter that is the $text, you'll call it that way:

newFootnote($text,LETTER);//or
newFootnote($text,NUMBER);

If you want to hide the constants definition and/or make them available in your programs then you can put them in a file (ideally with the function definition) and include it when needed, let that file be notesFunctions.php, put in it:

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 />";
     }
}

then in your program, depending on the location of the files (here assuming they are in the same folder), you put :

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
user10089632
  • 5,216
  • 1
  • 26
  • 34
  • To your second point, yes, certainly that's true that you would need to pass two variables into the function as I defined it. I went back and added the $text part to clarify the beginning but didn't fix my example later down. – theglossy1 May 20 '17 at 03:51
  • @theglossy1 I don't know about the details of the logic of the "newFootnote" function , but the simplistic version above (edited), works. Otherwise I don't see what remains unfixed – user10089632 May 20 '17 at 21:10
  • I'm sure the above would work fine. My question was how to avoid requiring the user to define the constants. I now see that built-in functions in PHP have them predefined in the core of the language. That answers my original question. – theglossy1 May 22 '17 at 01:47