[英]Convert to Integer and back to String after calculations
我有一个如下数组,将其放入名为$ testcoords的数组中
array (
0 => '\'263',
1 => '252',)
我想提取数字值并对其进行操作,然后将其放回字符串中。 我正在尝试使用以下代码:
$tc2 = explode(":",$testcoords);
$tcx = (int)(trim($tc2[0],"'\'");
$tcy = (int)(trim($tc2[1],"'");
$tcx2 = (($tcx)*(600/386));
$tcy2 = (($tcy)*(600/386));
$xtrans = (string)$tcx2;
$ytrans = (string)$tcy2;
到目前为止,我知道修剪函数可以正常工作-只是(trim($ tc2 [0],“'\\'”))或(trim($ tc2 [1],“'”)在两个字符串中返回数值。
现在,我想做的就是获取这些数值,将其转换为我尝试与修剪函数结合的整数。 一旦它们是整数,我想转回字符串并发布结果。
当我尝试执行此操作时,没有任何结果。 直到整理数据的步骤都很好。
例如,如果我只是做
$tcx = (trim($tc2[0],"'\'");
$tcy = (trim($tc2[1],"'");
对于上面列出的数组,并将结果回显,我的响应为263和252。
感谢有关如何完成其余部分的任何指示。
这是语法错误。 您没有意识到这一点,因为您已禁用错误报告。 您可以使用php.ini设置display_errors=1
或log_errors=1
以及诸如error_reporting=E_ALL
类的设置来实现。 您还可以通过发出以下命令在脚本中执行此操作:
ini_set('display_errors', 1); // for development
ini_set('display_errors', 0); // for production (display_errors would be a security risk)
ini_set('log_errors', 1); // for both development and production
ini_set('error_reporting', E_ALL);
错误:转换为int时,您缺少右括号。 )
用这个:
$tcx = (int)(trim($tc2[0],"'\'")); // <-- note the second closing ')'
$tcy = (int)(trim($tc2[1],"'"));
进一步注意,这可以通过以下方式简化
$tcx = (int)(trim($tc2[0],"'\'"); // <-- note the second closing ')'
$tcy = (int)(trim($tc2[1],"'");
trim
前,这些行上有不必要且未封闭的括号
$tcx = (int)(trim($tc2[0],"'\'");
$tcy = (int)(trim($tc2[1],"'");
将它们更改为:
$tcx = (int)trim($tc2[0],"'\'");
$tcy = (int)trim($tc2[1],"'");
另外,在进行开发时打开error_reporting,您会清楚地看到这些错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.