duandai2178 2015-03-23 17:12
浏览 19
已采纳

PHP concat int - 结果int

I've to append a fixed int to a variable int. $a=variable number.

$b has to be $a+002 (for example). So, for example,

$a = 1; $b=1002
$a = 2; $b=2002
$a = 58; $b=58002

I could do $b=$a.'002'; but this will be a string, right?

I've to put $b into a mySql database in a int(11) column.

Which is the most correct and safe way to do this?

  • 写回答

1条回答 默认 最新

  • donglang8008 2015-03-23 17:25
    关注

    Yes, you could just concatenate the string '002' onto $a.

    $b = $a . '002'

    The result will be a string, but it doesn't really matter. MySQL will parse it as an integer when you run your query.

    If you really wanted an integer result you could just cast it as such:

    $b = (int) ($a . '002');

    Even if you cast it to an integer, your query is still going to be a string. On MySQL's end the query is going to be the same either way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度