Bifalgorithm post · Computer science one · Organizing Functions

How can related PHP functions be organized?

How can related PHP functions be organized? (Computer science one · Organizing Functions) — "40.1.2. Organizing Functions There are many coding standards that…

Ideas in this flowchart

  • Organizing PHP functions
  • Coding standards provide guidelines for organizing PHP code.
  • A simple organization mechanism is to group related functions into separate source files.
  • Functions with similar functionality can be collected in one PHP file.
  • The grouped functions may be stored in a file named utils.php.
  • A main source file can include utils.php using include_once.
  • include_once loads and evaluates the specified PHP file where the invocation occurs.
  • If the file was already included, include_once does not include it again.
  • The same source file can therefore be included in multiple files without a conflict.
  • After inclusion, the main file can call functions such as getMonthlyPayment(1000, 0.05, 12).
Feed/

How can related PHP functions be organized?

No cover

Channel

Computer science one

1 page

"40.1.2. Organizing Functions There are many coding standards that guide how PHP code should be organized. We’ll only discuss a simple mechanism here. One way to organize functions is to collect functions with similar functionality into separate PHP source files. Suppose the functions above are in a PHP source file named utils.php. We could include them in another source file (our “main” source file) using an include_once function invocation. An example:\n\n1 <?php\n\n2\n\n3 include_once(\"utils.php\");\n\n4\n\n5 //we can now use the functions in utils.php:\n\n6 $p = getMonthlyPayment(1000, 0.05, 12);\n\n7\n\n8 ?>\n\nThe include_once function loads and evaluates the given PHP source file at the point in the code in which it is invoked. The “once” in the function refers to the fact that if the source file was already included in the script/code before, it will not be included a second time. This allows you to include the same source file in multiple source files without a conflict."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.