我无法获得此代码来打印错误消息。我对php很陌生,所以对我轻松一点。我正在尝试为我的网站创建一个注册页面,如果需要帮助,请随时请求更多代码。
try {
//Username
if(strlen($username) < 4) {
throw new Exception('Username must be at least 4 Characters!');
}
if(strlen($username) > 16){
throw new Exception('Username cannot be more than 15 characters!');
}
if(!ctype_alnum($username)) {
throw new Exception('Username must only be letters or numbers');
}
// Password
if(strlen($password) < 8) {
throw new Exception('Password must be at least 8 characters!');
}
// email
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
throw new Exception('Invalid Email!');
}
// secretcode
if(strlen($secretcode) < 6) {
throw new Exception('Secret Code is too short!');
}
if(strlen($secretcode) > 10) {
throw new Exception('Secret Code is too long!');
}
//repassword
if(strlen($password!=$repassword)) {
throw new Exception('Passwords did not match');
}
// Submit to database
echo 'ok';
} catch (Exception $e) {
echo $e->getMessage();
}
无法获得回显以打印出错误消息
答案 0 :(得分:0)
这是使用类时的常见问题。只需在最上方但在命名空间声明之后添加这一行
namespace My\Workspace;
use Exception; //<-- this one
class Myclass
{
try {
//your code
} catch (Exception $e) {
echo $e->getMessage();
}
}
这是因为如果您不使用use
,则php会在My\Workspace\Exception
中查找,而这显然不存在,因此catch
将永远不会运行。
如果您不想添加use Exception;
声明,一个简单的捷径就是在Exception
前面添加反斜杠
catch (\Exception $e)