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


How to convert String(16) to Int(1) in 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)没有问题,但String(16)一切都分开了,结果为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美元=地板((int) user_post_id美元); $ user_post_int_id =装天花板((int) user_post_id美元);

  3. 替换 $ user_post_id ="{{list.user.id}}"; user_post_id美元= (int) + 1 ({{list.user.id}}); 或者加零,这些技巧有时对我有用!

注。(int)可以骗你,PHP可以不让你使用十六进制bin或SMTH else。重新测试两次!php.net/manual/en/language.types.integer.phpphp.net/manual/en/language.types.type-juggling.php

你不能在服务器端语句中使用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