返回什么更好:null或false?

时间:2018-08-08 08:38:28

标签: php function null

所以我有注册功能:

<?php   

function sign_up($login, $password, $email){
global $db;

if(is_login_exist($login)){
    return ['error' => 'Login already exists', 'result' => null];
}

if(is_email_exist($email)){
    return ['error' => 'Email already exists', 'result' => null];
}

//hash password
$password = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
$stmt = $db->prepare("INSERT INTO users (login, password, email) VALUES (?, ?, ?)");

//add check
$stmt->bind_param("sss", $login, $password, $email);
$stmt->execute();
$stmt->close();
return ['error' => null, 'result' => 'success']; 
}?>

因此,如果例如当登录已经存在时出现错误,我将返回关联数组['error'=>'登录已存在','result'=> null]

问题:将“结果”参数返回为null或false是否正确?

3 个答案:

答案 0 :(得分:0)

据我所知。最好返回可以在另一端解释的字符串数字值。如果返回类型超过两种,则是最合适的。

例如:-

  • 返回“ 00”,表示由于系统错误而失败
  • 返回“ 01”表示成功
  • 返回“ 02”表示参数错误
  • 返回“ 10”个其他错误

因此,很容易传达一条不仅表示成功/失败而且更为详细的消息。每个值表示一个特定的错误类型。

如果这不是您想要的,则“ False”比“ null”要好。它给出了正确的含义。

答案 1 :(得分:0)

我会返回: 每当状态为错误时,EndpointAddress endPointAddr = new EndpointAddress(new Uri("https://optima--dev.custhelp.com/cgi-bin/optima--dev.cfg/services/kf_soap")); // Minimum required BasicHttpsBinding binding2 = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport); binding2.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; // Optional depending upon use cases //binding2.MaxReceivedMessageSize = 5 * 1024 * 1024; //binding2.MaxBufferSize = 5 * 1024 * 1024; //binding2.e = ; // Create client proxy class _rightNowKnowledgePortClient = new RightNowKnowledgePortClient(binding2, endPointAddr); BindingElementCollection elements = _rightNowKnowledgePortClient.Endpoint.Binding.CreateBindingElements(); // elements.Find<SecurityBindingElement>().IncludeTimestamp = false; _rightNowKnowledgePortClient.Endpoint.Binding = new CustomBinding(elements); //_rightNowKnowledgePortClient = new RightNowKnowledgePortClient(binding2, // new EndpointAddress(new Uri("https://optima--dev.custhelp.com/cgi-bin/optima--dev.cfg/services/kf_soap"))); _rightNowKnowledgePortClient.ClientCredentials.UserName.UserName = "xxxxx"; _rightNowKnowledgePortClient.ClientCredentials.UserName.Password = "xxxx"; _header = new ClientInfoHeader(); _header.AppID = "KF Operations"; Task<StartInteractionResponse> startInteractionResponse = _rightNowKnowledgePortClient.StartInteractionAsync(_header, "Sample KF Operations", "10.0.0.0", null, ".NET Application"); if (startInteractionResponse.Result.SessionToken != null) _knowledgeInteractionId = startInteractionResponse.Result.SessionToken; 以及可选的status => 'ok'/'error''error_message' => 'message'。但这只是我的意见。

答案 2 :(得分:0)

如果有问题,我会抛出异常。

function sign_up(mysqli $db, $login, $password, $email){
    // hash password
    $password = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
    // try to insert
    $stmt = $db->prepare("INSERT INTO users (login, password, email) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $login, $password, $email);
    $stmt->execute();

    return true; 
}

要求:

  • mysqli被设置为在出现错误=> mysqli_report()
  • 时引发异常
  • UNIQUElogin字段上有email约束