PHP's ini_get in TypeScript

How to use

Install via yarn add locutus and import: import { ini_get } from 'locutus/php/info/ini_get'.

Or with CommonJS: const { ini_get } = require('locutus/php/info/ini_get')

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
1ini_set('date.timezone', 'Asia/Hong_Kong') ini_get('date.timezone')'Asia/Hong_Kong'

Notes

  • The ini values must be set by ini_set or manually within an ini file

Dependencies

This function uses the following Locutus functions:

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

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

export function ini_get(varname: string): string {
// discuss at: https://locutus.io/php/ini_get/
// original by: Brett Zamir (https://brett-zamir.me)
// note 1: The ini values must be set by ini_set or manually within an ini file
// example 1: ini_set('date.timezone', 'Asia/Hong_Kong')
// example 1: ini_get('date.timezone')
// returns 1: 'Asia/Hong_Kong'

const runtime = ensurePhpRuntimeState()
const entry = runtime.ini[varname]

if (entry && entry.local_value !== undefined) {
if (entry.local_value === null) {
return ''
}
return String(entry.local_value)
}

return ''
}

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 info functions


Star