UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

79 lines (64 loc) 2.11 kB
--- layout: page title: "JavaScript strval function" comments: true sharing: true footer: true alias: - /functions/view/strval:557 - /functions/view/strval - /functions/view/557 - /functions/strval:557 - /functions/557 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's strval {% codeblock var/strval.js lang:js https://raw.github.com/kvz/phpjs/master/functions/var/strval.js raw on github %} function strval (str) { // From: http://phpjs.org/functions // + original by: Brett Zamir (http://brett-zamir.me) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Brett Zamir (http://brett-zamir.me) // - depends on: gettype // * example 1: strval({red: 1, green: 2, blue: 3, white: 4}); // * returns 1: 'Object' var type = ''; if (str === null) { return ''; } type = this.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'; } return str; } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/var/strval.js) Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are the closest match to this hashtable-like data structure. Please also note that php.js offers community built functions and goes by the [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just: - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/var/strval.js) ### Example 1 This code {% codeblock lang:js example %} strval({red: 1, green: 2, blue: 3, white: 4}); {% endcodeblock %} Should return {% codeblock lang:js returns %} 'Object' {% endcodeblock %} ### Other PHP functions in the var extension {% render_partial _includes/custom/var.html %}