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

时间:2016-08-23 07:36:07

标签: php

例如

<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

所以我认为主要的问题是类型转换,因为我尝试了另外一件事,其中String(1)转换为int(1)没有问题,但是使用字符串(16),所有东西都分开并导致结果为0。 / p>

2 个答案:

答案 0 :(得分:0)

您不能在服务器端语句中使用Angular表达式,因为Angular语句在运行所有服务器逻辑后在客户端运行。

在您的代码中:

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

<?php 

 $user_post_id = "{{list.user.id}}"          //list.user.id is a JS variable, you cannot use it in PHP.
 $user_post_int_id = (int)$user_post_id;     // something wrong here, you do not need this step, normally if you retrieved the code from server side.

 {{ $user_post_int_id }}                     //this should not work

But still this blade function fails

@if(Auth::user()->id == $user_post_int_id)
do something //returns false, makes alot of sense, as again you are trying to get variables from JS and passing them to PHP scripts. This will never work.
@endif

答案 1 :(得分:0)

我有3个建议4u(在我的服务器上一切正常)

作为我的评论的原文:

<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

然后我建议您尝试使用您的代码,在您的服务器上运行更改:

  1. 替换 $ user_post_id =“{{list.user.id}}”; $ user_post_id =(int){{list.user.id}}; //希望你的php不会显示错误

  2. 替换 $ user_post_int_id =(int)$ user_post_id; $ user_post_int_id = floor((int)$ user_post_id); $ user_post_int_id = ceil((int)$ user_post_id);

  3. 替换 $ user_post_id =“{{list.user.id}}”; $ user_post_id =(int)({{list.user.id}})+ 1; 或者加零,这些黑客有时对我有用!

  4. P.S。 (int)可以对你做一个技巧,php可以说你使用了十六进制bin或其他。重新测试两次! php.net/manual/en/language.types.integer.php php.net/manual/en/language.types.type-juggling.php