Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php字符串不断变为值0不确定原因_Php_String - Fatal编程技术网

php字符串不断变为值0不确定原因

php字符串不断变为值0不确定原因,php,string,Php,String,我有一个脚本来注册一个用户,或者检查用户信息是否有任何错误,比如密码不匹配或者用户是否已经存在等等 如果已使用用户名,则在打印错误消息时,$errorMessage的值始终为0,我不确定原因: <?php //get user name and passwords $errorMessage = "Error: "; $successMessage; if ($p1 == ""){ $errorMessage = $errorMessage + "You le

我有一个脚本来注册一个用户,或者检查用户信息是否有任何错误,比如密码不匹配或者用户是否已经存在等等

如果已使用用户名,则在打印错误消息时,$errorMessage的值始终为0,我不确定原因:

<?php


 //get user name and passwords


 $errorMessage = "Error: ";

 $successMessage;

 if ($p1 == ""){

    $errorMessage = $errorMessage + "You left your password blank. ";

    echo $errorMessage;

    $fail = $fail + 1;

    }
 if ($p2 == ""){


    $errorMessage = $errorMessage + "You did not type your password in a second time. ";

    $fail = $fail + 1;

    echo $errorMessage;

    }


 //hash passwords
 $hash1 = sha1($p1);
 $hash2 = sha1($p2);


 $fail = 0;

 //check if user name is taken



if($hash1 != $hash2){
     $fail = $fail + 1;

     $errorMessage = $errorMessage + "Your passwords do not match. ";
     echo $errorMessage;

     }



if ($numRows > 0){


    $errorMessage = $errorMessage + "That User name is already taken, please try another username. ";

    $fail = $fail + 1;
    echo $errorMessage;

    }


if ($userName == ""){


    $errorMessage = $errorMessage + "You left your username blank. ";

    $fail = $fail + 1;
    echo $errorMessage;

    }

if ($email == ""){


    $errorMessage = $errorMessage + "You left your email blank. ";

    $fail = $fail + 1;
    echo $errorMessage;

    }



//do not register user if fails
if( $fail > 0){

    //print fail message JSON
    $successMessage = "fail";



    }

//register user
else{
    //add user to database


//print json

    $d = array('status' => "$successMessage" , 'error' => "$errorMessage" );


    $jsonCode = json_encode($d);

    header('Content-Type: application/json');

    echo $jsonCode;



?>

PHP的concat运算符是句点或点。不是加号+。

您必须像这样连接字符串:

$errorMessage = $errorMessage . "You left your password blank. ";

您没有正确连接。在PHP中使用。。连接。我为您修复了代码:

 <?php


     //get user name and passwords


     $errorMessage = "Error: ";

     $successMessage;

     if ($p1 == ""){

        $errorMessage = $errorMessage."You left your password blank. ";

        echo $errorMessage;

        $fail = $fail + 1;

        }
     if ($p2 == ""){


        $errorMessage = $errorMessage."You did not type your password in a second time. ";

        $fail = $fail + 1;

        echo $errorMessage;

        }


     //hash passwords
     $hash1 = sha1($p1);
     $hash2 = sha1($p2);


     $fail = 0;

     //check if user name is taken



    if($hash1 != $hash2){
         $fail = $fail + 1;

         $errorMessage = $errorMessage."Your passwords do not match. ";
         echo $errorMessage;

         }



    if ($numRows > 0){


        $errorMessage = $errorMessage."That User name is already taken, please try another username. ";

        $fail = $fail + 1;
        echo $errorMessage;

        }


    if ($userName == ""){


        $errorMessage = $errorMessage."You left your username blank. ";

        $fail = $fail + 1;
        echo $errorMessage;

        }

    if ($email == ""){


        $errorMessage = $errorMessage."You left your email blank. ";

        $fail = $fail + 1;
        echo $errorMessage;

        }



    //do not register user if fails
    if( $fail > 0){

        //print fail message JSON
        $successMessage = "fail";



        }

    //register user
    else{
        //add user to database


    //print json

        $d = array('status' => "$successMessage" , 'error' => "$errorMessage" );


        $jsonCode = json_encode($d);

        header('Content-Type: application/json');

        echo $jsonCode;



    ?>

更详细地说,当使用+,PHP假设您正在执行算术,所以它将字符串转换为数字,由于找不到更好的匹配项,所以确定为0,然后执行加法,结果输出为0。我现在觉得很笨,哈哈。我一直在为安卓做很多java编程,所以我把自己弄糊涂了。。。。