duannai1883 2017-08-19 05:26
浏览 36
已采纳

如何在javascript中使用php整数变量作为整数变量

I have passed my php integer variable $number into javascript. But can't access that php variable without using quotation

<script>
 var number = '<?php echo $number?>';
</script>

if I use quotation it can accessible but don't get the integer value actually. It became string since use quotation. Without quotation can't get anything.

I have googling that but don't get the appropriate answer yet.

How to get the integer value.

  • 写回答

2条回答 默认 最新

  • duannaiying9662 2017-08-19 05:34
    关注

    For int simple remove the single quotes

    <script>
         var number = <?php echo $number ?>;
    </script>
    

    Note : Make sure that $number should not empty . if it's empty use ternary operator if statement to handle that . var number =<?php echo !empty($number)? $number:0 ?>;

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?