Install via yarn add locutus and import:
import { strval } from 'locutus/php/var/strval'.
Or with CommonJS: const { strval } = require('locutus/php/var/strval')
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.
#
code
expected result
1
strval({red: 1, green: 2, blue: 3, white: 4})
'Object'
Dependencies
This function uses the following Locutus functions:
// php/var/is_float (Locutus dependency module) functionis_float(mixedVar: number): boolean { // discuss at: https://locutus.io/php/is_float/ // parity verified: PHP 8.3 // original by: Paulo Freitas // bugfixed by: Brett Zamir (https://brett-zamir.me) // improved by: WebDevHobo (https://webdevhobo.blogspot.com/) // improved by: Rafał Kukawski (https://blog.kukawski.pl) // note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes // note 1: it different from the PHP implementation. We can't fix this unfortunately. // example 1: is_float(186.31) // returns 1: true
functiongettype(mixedVar: TypeInput): string { // discuss at: https://locutus.io/php/gettype/ // original by: Paulo Freitas // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Douglas Crockford (https://javascript.crockford.com) // improved by: Brett Zamir (https://brett-zamir.me) // input by: KELAN // note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes // note 1: it different from the PHP implementation. We can't fix this unfortunately. // example 1: gettype(1) // returns 1: 'integer' // example 2: gettype(undefined) // returns 2: 'undefined' // example 3: gettype({0: 'Kevin van Zonneveld'}) // returns 3: 'object' // example 4: gettype('foo') // returns 4: 'string' // example 5: gettype({0: function () {return false;}}) // returns 5: 'object' // example 6: gettype({0: 'test', length: 1, splice: function () {}}) // returns 6: 'object' // example 7: gettype(['test']) // returns 7: 'array'
lets: string = typeof mixedVar let name = '' const _getFuncName = function (fn: TypeInput): string { const funcNameMatch = /\W*function\s+([\w$]+)\s*\(/.exec(String(fn)) if (!funcNameMatch) { return'(Anonymous)' } return funcNameMatch[1] ?? '(Anonymous)' }
if (s === 'object') { if (typeof mixedVar === 'object' && mixedVar !== null) { const objectLike = mixedVar const objectLength = getPhpObjectEntry(objectLike, 'length') const objectSplice = getPhpObjectEntry(objectLike, 'splice') // From: https://javascript.crockford.com/remedial.html // @todo: Break up this lengthy if statement if ( typeof objectLength === 'number' && !Object.prototype.propertyIsEnumerable.call(objectLike, 'length') && typeof objectSplice === 'function' ) { s = 'array' } else { const constructorValue = getPhpObjectEntry(objectLike, 'constructor') if (!constructorValue) { return s } name = _getFuncName(constructorValue) if (name === 'Date') { // not in PHP s = 'date' } elseif (name === 'RegExp') { // not in PHP s = 'regexp' } elseif (name === 'LOCUTUS_Resource') { // Check against our own resource constructor s = 'resource' } } } else { s = 'null' } } elseif (typeof mixedVar === 'number') { s = isFloat(mixedVar) ? 'double' : 'integer' }
return s }
// php/var/strval (target function module) typeStringValue = PhpRuntimeValue
// Comment out the entire switch if you want JS-like // behavior instead of PHP behavior switch (type) { case'boolean': if (str === true) { return'1' } return'' case'array': return'Array' case'object': return'Object' }
returnString(str) }
// php/_helpers/_phpRuntimeState (Locutus helper dependency) functiongetPhpObjectEntry(value, key) { if ((typeof value !== 'object' && typeof value !== 'function') || value === null) { returnundefined }
let current = value while (current) { const descriptor = Object.getOwnPropertyDescriptor(current, key) if (descriptor) { if (typeof descriptor.get === 'function') { const getterValue = descriptor.get.call(value) returntypeof getterValue === 'undefined' ? undefined : getterValue } const directValue = descriptor.value returntypeof directValue === 'undefined' ? undefined : directValue } current = Object.getPrototypeOf(current) }
returnundefined }
// php/var/is_float (Locutus dependency module) functionis_float(mixedVar) { // discuss at: https://locutus.io/php/is_float/ // parity verified: PHP 8.3 // original by: Paulo Freitas // bugfixed by: Brett Zamir (https://brett-zamir.me) // improved by: WebDevHobo (https://webdevhobo.blogspot.com/) // improved by: Rafał Kukawski (https://blog.kukawski.pl) // note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes // note 1: it different from the PHP implementation. We can't fix this unfortunately. // example 1: is_float(186.31) // returns 1: true
functiongettype(mixedVar) { // discuss at: https://locutus.io/php/gettype/ // original by: Paulo Freitas // improved by: Kevin van Zonneveld (https://kvz.io) // improved by: Douglas Crockford (https://javascript.crockford.com) // improved by: Brett Zamir (https://brett-zamir.me) // input by: KELAN // note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes // note 1: it different from the PHP implementation. We can't fix this unfortunately. // example 1: gettype(1) // returns 1: 'integer' // example 2: gettype(undefined) // returns 2: 'undefined' // example 3: gettype({0: 'Kevin van Zonneveld'}) // returns 3: 'object' // example 4: gettype('foo') // returns 4: 'string' // example 5: gettype({0: function () {return false;}}) // returns 5: 'object' // example 6: gettype({0: 'test', length: 1, splice: function () {}}) // returns 6: 'object' // example 7: gettype(['test']) // returns 7: 'array'
let s = typeof mixedVar let name = '' const _getFuncName = function (fn) { const funcNameMatch = /\W*function\s+([\w$]+)\s*\(/.exec(String(fn)) if (!funcNameMatch) { return'(Anonymous)' } return funcNameMatch[1] ?? '(Anonymous)' }
if (s === 'object') { if (typeof mixedVar === 'object' && mixedVar !== null) { const objectLike = mixedVar const objectLength = getPhpObjectEntry(objectLike, 'length') const objectSplice = getPhpObjectEntry(objectLike, 'splice') // From: https://javascript.crockford.com/remedial.html // @todo: Break up this lengthy if statement if ( typeof objectLength === 'number' && !Object.prototype.propertyIsEnumerable.call(objectLike, 'length') && typeof objectSplice === 'function' ) { s = 'array' } else { const constructorValue = getPhpObjectEntry(objectLike, 'constructor') if (!constructorValue) { return s } name = _getFuncName(constructorValue) if (name === 'Date') { // not in PHP s = 'date' } elseif (name === 'RegExp') { // not in PHP s = 'regexp' } elseif (name === 'LOCUTUS_Resource') { // Check against our own resource constructor s = 'resource' } } } else { s = 'null' } } elseif (typeof mixedVar === 'number') { s = isFloat(mixedVar) ? 'double' : 'integer' }
return s }
// php/var/strval (target function module) functionstrval(str) { // discuss at: https://locutus.io/php/strval/ // original by: Brett Zamir (https://brett-zamir.me) // improved by: Kevin van Zonneveld (https://kvz.io) // bugfixed by: Brett Zamir (https://brett-zamir.me) // example 1: strval({red: 1, green: 2, blue: 3, white: 4}) // returns 1: 'Object'
let type = ''
if (str === null) { return'' }
type = gettype(str)
// Comment out the entire switch if you want JS-like // behavior instead of PHP behavior switch (type) { case'boolean': if (str === true) { return'1' } return'' case'array': return'Array' case'object': return'Object' }
returnString(str) }
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.