空发送作为字符串"零" [英] Null being sent as a string "null"

查看:169
本文介绍了空发送作为字符串"零"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我们搬到服务器和已注意到一些奇怪的问题。 我们已经发现的主要问题是,如果在PHP中执行一个空检查任何变量是PTED作为一个字符为跨$ P $。

例如。

 如果(是$ var == NULL){
        //做一点事
    }
 

已经取得两个关键的变化是: 1.由HTTP服务器到HTTPS 2.改变从PHP版本5.3.14到5.3.13

该请求使用jQuery.ajax电话后制成。

我的问题是,是否有任何已知的问题/原因,发生这种情况(如上面的变化),是有什么事情可以做,以解决这个问题(最小的变化)。

一如往常感谢您的答复!

编辑: 示例

JS / jQuery的

  VAR测试= NULL;
    $阿贾克斯({
        网址:'的functions.php,
        数据:{行动:testNullFunction'的testvar:测试},
        类型:'后',
        成功:
        功能(输出){
            警报(输出)
        }})
 

PHP的:

 如果(使用isset($ _ POST ['行动'])及和放大器;!空($ _ POST ['行动'])){
         $行动= $ _ POST ['行动'];
         开关($行动){
             案testNullFunction:
                 如果($ _ POST ['的testvar'] == NULL || is_null($ _ POST ['的testvar'])){
                       回声的空;
                }
             打破;
 

解决方案

当你这样做:

  $。阿贾克斯({
    网址:'的functions.php,
    数据:{行动:testNullFunction'的testvar:测试},
    类型:'后',
    成功:函数(输出){
        警报(输出)
    }
})
 

您发送数据为字符串,因此,所有的值也成为字符串。

当的PHP接收到它,它仍是一个字符串。正是在这种情况下,为PHP无法确定是否的testvar 包含 - 类型的信息丢失了。


您可以修复通过发送:

 数据:{行动:testNullFunction'的testvar:JSON.en code(测试)}
 

和接收:

  $ =的testvar json_de code($ _ POST ['的testvar'])
 

We've recently moved servers and have been noticing some weird problems. The main problem that we've noticed is that any variable where a null check is performed in php is being interpreted as a string.

e.g.

    if($var == null){
        //do something
    }

The two key changes that have been made are: 1. Change from HTTP server to HTTPS 2. Change from php version, 5.3.14 to 5.3.13

The requests are made using jQuery.ajax post call.

My question is, are there any known problems/reasons why this is happening (such as the changes above) and is there anything that can be done to resolve the problem (with minimal changes).

As always Thanks for your responses!

Edit: Example

Js/jQuery

    var test = null;
    $.ajax({
        url: 'functions.php',
        data: { action: 'testNullFunction', testVar:test },
        type: 'post',
        success:
        function (output) {
            alert(output)
        }})

Php:

    if (isset($_POST['action']) && !empty($_POST['action'])) {
         $action = $_POST['action'];
         switch ($action) {
             case 'testNullFunction':
                 if ($_POST['testVar']==null || is_null($_POST['testVar'])){
                       echo "its null"; 
                } 
             break;

解决方案

When you do:

$.ajax({
    url: 'functions.php',
    data: { action: 'testNullFunction', testVar:test },
    type: 'post',
    success: function (output) {
        alert(output)
    }
})

You send the data as a string, thus all values also become strings.

When PHP receives it, it is still a string. It is impossible in this case for the PHP to determine whether testVar contained null or "null" - the type information was lost.


You can fix that by sending:

data: { action: 'testNullFunction', testVar: JSON.encode(test) }

And receiving:

$testVar = json_decode($_POST['testVar'])

这篇关于空发送作为字符串"零"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆