PHP's function_exists in TypeScript

How to use

Install via yarn add locutus and import: import { function_exists } from 'locutus/php/funchand/function_exists'.

Or with CommonJS: const { function_exists } = require('locutus/php/funchand/function_exists')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1function_exists('isFinite')true

Dependencies

This function uses the following Locutus functions:

Here's what our current TypeScript equivalent to PHP's function_exists looks like.

import { getPhpGlobalCallable } from '../_helpers/_phpRuntimeState.ts'

export function function_exists(funcName: string): boolean {
// discuss at: https://locutus.io/php/function_exists/
// original by: Kevin van Zonneveld (https://kvz.io)
// improved by: Steve Clay
// improved by: Legaev Andrey
// improved by: Brett Zamir (https://brett-zamir.me)
// example 1: function_exists('isFinite')
// returns 1: true

return typeof getPhpGlobalCallable(funcName) === 'function'
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


« More PHP funchand functions


Star