Files
Cactidocumentation/Standards-PHP-Spec-Constructs.md
T
Thomas Vincent 77578446a4 docs(plugins): [1.2.x] correct how-to, plugin development and standards pages against the source (#271)
* docs(plugins): [1.2.x] correct the hook reference against the hooks the core fires

Add the hooks the 1.2.x core fires that the page did not list, correct file locations and introduced/retired versions, mark hooks that are no longer fired as retired, and drop duplicate sections and an unlinked index entry.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs(plugins): [1.2.x] correct plugin pages against the plugin API and plugin sources

Fix the INFO key name and delimiter, the hook used in the registration examples, the setup function requirements, the dependency example, and menu, table and template details in the Mactrack and Syslog pages.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs(howto): [1.2.x] correct how-to pages against the 1.2.x user interface and data query code

Fix form labels, resource directory names, re-index method names, the index_order delimiter, the maximum value advice, and the cacti_snmp_get call in the script query example.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs(standards): [1.2.x] correct standards pages against the 1.2.x tree

Fix the prepared statement example, the CLI include path, the CHANGELOG location, the CI reference and the include/vendor directory list.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs(plugins): [1.2.x] align the Syslog SQL mode section with the plugin README

The plugin README says not to copy NO_AUTO_CREATE_USER into a current MySQL configuration and to inspect the active modes instead.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs(standards): [1.2.x] fix example typo, indentation rule, constants and table name

Correct the $$ typo in the script query example, the vim rules to use tabs as CLAUDE.md requires, the example constants to match include/global_constants.php, and the user_auth_realm table name.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs(standards): [1.2.x] correct the CI and MD001 statements for this branch

The 1.2.x branch has no Travis CI configuration or workflow, and .mdl_style.rb disables MD001.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* docs(translations): [1.2.x] correct the locales listing, template update steps and typos

Describe what update-pot.sh, build_gettext.sh and build_mo.sh do on 1.2.x, add the plugin script, link Poedit's home page, and fix typos.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
2026-11-23 15:11:29 -05:00

2.8 KiB

PHP Specific Constructs

Traversing Arrays

Use the foreach ($array as $name => $value) syntax instead of while (list($name, $value) = each($array)) syntax when traversing arrays. The former has been demonstrated to be more efficient and does not increment the internal pointer of the array. The foreach construct will error if the array is empty or is not an array, so you must check this first.

if ((is_array($array)) && (sizeof($array) > 0)) {
    foreach ($array as $name => $value) {
        // code
    }
}

It should be noted that with PHP 7.2 use of list() = each() syntax will result in warnings and/or errors. It should also be noted that 7.2 also introduced the need for the variable to implement ICountable which are arrays and objects. You should therefore check if variable !=== false before using sizeof() or count()

Error Return Values

When an a function needs to return a generic error, it is best to return boolean false in PHP. This makes it very straightforward to check for.

if (file_list() === false) {
    // there was an error
}

Regular Expressions Are Slow

Only use PHP's regular expression parsing functions (preg_match, preg_replace, preg_quote, etc) when absolutely needed. Often simpler functions such as strstr(), str_replace(), substr(), and explode() can be used, and are much faster.

$items = explode(':', $string));

It's much quicker than:

$items = split(':', $string));

Use preg Functions Instead of ereg

The POSIX based regular expressions in PHP tend to be slower than their Perl-based equivalents. Use Perl-based regular expression functions unless there is an explicit reason to do otherwise.

NOTE: Do not use any deprecated functions: eregi_replace, ereg, or eregi

Quotes

Single quotes are used instead of double where possible. Per string, it doesn't really make a huge difference, but when you have thousands upon thousands of them, every little bit helps.

include_once('./lib/api_tree.php');

However, there isn't any difference between

"This is a $test"

and

'This is a ' . $test

Don't bother trying to break out variables out of the string if you are just going to have to concatenate them.

Including other PHP files

When including other PHP files, you should use the $config['base_path'] to ensure that the base path is configured properly.

If the main file is a web-based page, it should include include/auth.php to ensure that the current user is authenticated.

If the main file is a CLI-based program to be run by end users, it should include include/cli_check.php and support -h and -v parameters which run display_help() and display_version() respectively.

include($config['base_path'] . '/lib/database.php');

Copyright (c) 2004-2026 The Cacti Group