dsff788655567 2016-08-23 07:36
浏览 35
已采纳

如何在php中将String(16)转换为Int(1)

For instance

<div ng-repeat = 'list in lists'>

<?php 

 $user_post_id = "{{list.user.id}}";          //this syntax works
 var_dump($user_post_id);                     // gives String(16) '1'     
 $user_post_int_id = (int)$user_post_id;      // change from string to int
 var_dump($user_post_int_id);                 // gives int(1) 0, I don't know why isn't type conversion working!

 echo $user_post_id;                         // echoes out 1
 echo $user_post_int_id;                     //echoes out the 0

So the main problem I think is of type conversion, as I tried one more thing where String(1) is converting to int(1) with no problem but with string(16) everything blows apart and results to 0.

  • 写回答

2条回答 默认 最新

  • doutang0335 2016-08-25 07:38
    关注

    i got 3 suggestion 4u (on my server all works fine)

    as a original with my comments:

    <div ng-repeat = 'list in lists'>
    
    <?php 
    
     $user_post_id = "{{list.user.id}}";          //this syntax works
     var_dump($user_post_id);                     // gives String(16) '1'     
     $user_post_int_id = (int)$user_post_id;      // change to int
     var_dump($user_post_int_id);                 
    // gives int(1) 1, is not test your php/server conf!
    
     echo $user_post_id;                         // echoes out 1
     echo $user_post_int_id;                     //echoes out 1
    

    then i advise you to try with your code, run on your server with changes:

    1. replace $user_post_id = "{{list.user.id}}"; to $user_post_id = (int){{list.user.id}}; //hopes your php wudn't show an error

    2. replace $user_post_int_id = (int)$user_post_id; to $user_post_int_id = floor((int)$user_post_id); or $user_post_int_id = ceil((int)$user_post_id);

    3. replace $user_post_id = "{{list.user.id}}"; to $user_post_id = (int)({{list.user.id}}) + 1 ; or plus zero, these hack sometimes works for me !

    p.s. (int) can do a trick with you, php can thout that you uses hex bin or smth else. retest it twice ! php.net/manual/en/language.types.integer.php php.net/manual/en/language.types.type-juggling.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起