2016-08-23 62 views
0

串(16)转换为INT(1)例如如何在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 

所以,我认为主要的问题是转换的类型,因为我想一两件事,其中字符串(1)转换到int(1)没有问题,但用字符串(16),所有东西都吹散,结果为0.

+0

这可能有所帮助https://scotch.io/tutorials/quick-tip-using-laravel-blade-with-angularjs –

回答

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 wudn't显示错误

  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; 或加零,这些破解有时候适合我!

p.s. (int)可以跟你做一个诡计,php可以使用十六进制bin或其他的smthth。重新测试两次! php.net/manual/en/language.types.integer.php php.net/manual/en/language.types.type-juggling.php

+0

抱歉忘记settype()函数请参阅php.net/manual/ru/function.settype。php –

+1

嘿,Vladmir,但是我在处理我的项目时放弃了这种方法,并且在不将JS与PHP混合的情况下工作,但感谢您的建设性答案,所以我会接受它以防其他人需要帮助,以及我会在晚上尝试这种方式,让你知道,干杯! – Drazxier

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 
+0

嘿@Mina,其实我已经改变了我的一些代码,请你再看看它,知道现在有什么问题吗? – Drazxier

+0

嗯,好吧,仍然欢呼的帮助,我会问一个新的问题与进一步的细节,但谢谢你的时间! – Drazxier

+0

这不是一个新问题。这是关于如何使用后端脚本和前端语言。两个生活在不同的空间,不能混合。 –