<div style="text-align: left;">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Name:<input type="text" name="name"><br>
Age:<input type="text" name="age"> <br>
Job:<input type="text" name="job"> <br>
<input type="submit" name="">
</form>
<?php
$name=$_POST['name'];
$age=$_POST['age'];
$job=$_POST['job'];
function rabby($name,$age,$job){
if($age>=18){
throw new exception('Your Name Is: $name<br>Age:$age<br> Nowadays You are working as: $job<br>Alooha!!Your are Adult :D');
}
return true;
}
try{
rabby($name,$age,$job);
echo "Sorry $name ,You are not not enable to see this page";
}
catch(exception $e){
echo "Message:" .$e->getMessage();
}
?>
在这里-------- 输出是: 消息:您的姓名是:$ name 年龄:$年龄 如今你正在努力:$ job Alooha !!你是成年人:D
But i want to see---
Message: Your Name Is: rabby //when i will fillup html form
Age: 22
Nowadays You are working as: student
Alooha!!Your are Adult :D
如果我添加的年龄小于18岁,则抛出: 抱歉拉比,你没有看到这个页面
请解决此问题。
答案 0 :(得分:2)
它应该是:
throw new exception('Your Name Is: '.$name.'<br>Age:'.$age.'<br> Nowadays You are working as: '.$job.'<br>Alooha!!Your are Adult :D');
或
throw new exception("Your Name Is: $name<br>Age:$age<br> Nowadays You are working as: $job<br>Alooha!!Your are Adult :D");
在单引号中,您需要连接字符串以打印PHP变量。
答案 1 :(得分:1)
首先,您需要了解单引号('')和双引号(“”)之间的区别。 单引号内的变量将按原样打印,而不是打印变量值。但是当它在double qoute中使用时,它将首先被执行并打印该值。
请参阅此链接以了解更多信息:What is the difference between single-quoted and double-quoted strings in PHP?。