strval() - php 变量处理函数
strval()
(PHP 4, PHP 5, PHP 7)
获取变量的字符串值
描述
strval(mixed $var): string返回$var的string值。参见string文档获取更多关于字符串转换的信息。
$var可以是任何标量类型。不能将strval()用于数组或对象。
参见floatval()、intval()、settype()和类型戏法。
As of PHP 5.2, strval() will return the string value of an object, calling its __toString() method to determine what that value is.
Some notes about how this function has changed over time, with regards the following statement: > You cannot use strval() on arrays or on objects that > do not implement the __toString() method. == Arrays == In PHP 5.3 and below, strval(array(1, 2, 3)) would return the string "Array" without any sort of error occurring. From 5.4 and above, the return value is unchanged but you will now get a notice-level error: "Array to string conversion". == Objects == For objects that do not implement __toString(), the behaviour has varied: PHP 4: "Object" PHP 5 = 5.2: Catchable fatal error: Object of class X could not be converted to string
If you want to convert an integer into an English word string, eg. 29 -> twenty-nine, then here's a function to do it. Note on use of fmod() I used the floating point fmod() in preference to the % operator, because % converts the operands to int, corrupting values outside of the range [-2147483648, 2147483647] I haven't bothered with "billion" because the word means 10e9 or 10e12 depending who you ask. The function returns '#' if the argument does not represent a whole number. Usage:
The only way to convert a large float to a string is to use printf('%0.0f',$float); instead of strval($float); (php 5.1.4). // strval() will lose digits around pow(2,45); echo pow(2,50); // 1.1258999068426E+015 echo (string)pow(2,50); // 1.1258999068426E+015 echo strval(pow(2,50)); // 1.1258999068426E+015 // full conversion printf('%0.0f',pow(2,50)); // 112589906846624 echo sprintf('%0.0f',pow(2,50)); // 112589906846624
I can't help being surprised that (string)"0" == (string)"0.00" evaluates to true. It's the same with strval and single quotes. === avoids it. Why does it matter? One of my suppliers, unbelievably, uses 0 to mean standard discount and 0.00 to mean no discount in their stock files.
It seems that one is being treated as an unsigned large int (32 bit), and the other as a signed large int (which has rolled over/under). 2326201276 - (-1968766020) = 4294967296.
As of PHP 5.1.4 (I have not tested it in later versions), the strval function does not attempt to invoke the __toString method when it encounters an object. This simple wrapper function will handle this circumstance for you:
In complement to Tom Nicholson's contribution, here is the french version (actually it's possible to change the language, but you should check the syntax ;) ) function int_to_words($x) { global $nwords; if(!is_numeric($x)) $w = '#'; else if(fmod($x, 1) != 0) $w = '#'; else { if($x 0) $w .= '-'. $nwords[$r]; } else if($x 0) $w .= ' '.$nwords['separator'].' '. int_to_words($r); } else if($x 0) { $w .= ' '; if($r 0) { $w .= ' '; if($r "thirty", 40 => "forty", 50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty", 90 => "ninety" , "hundred" => "hundred", "thousand"=> "thousand", "million"=>"million", "separator"=>"and", "minus"=>"minus"); echo 'There are currently '. int_to_words(-120223456) . ' members logged on.
'; //Utilisation en Francais $nwords = array( "zéro", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix", "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit", "dix-neuf", "vingt", 30 => "trente", 40 => "quarante", 50 => "cinquante", 60 => "soixante", 70 => "soixante-dix", 80 => "quatre-vingt", 90 => "quatre-vingt-dix" , "hundred" => "cent", "thousand"=> "mille", "million"=>"million", "separator"=>"", "minus"=>"moins"); echo 'Il y a actuellement '. int_to_words(-120223456) . ' membres connectés.
';
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)