2017-06-20 1 views
-1
<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(); 
    } 
    ?> 

は-------- 出力されます: メッセージ:あなたの名前は:$名 年齢:$ジョブ Alooha !!あなたは大人です:あなたのように働いている今日では、年齢 $: Dなぜ変数printは変数値を出力するのではなく、新しい例外をスローするのですか?ここで

 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

あなたの名前は:$ name
年齢:$ age
今日あなたは次のように働いています:$ job
Alooha !!あなたは大人: D)); –

答えて

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の変数を印刷する文字列を連結する必要が単一引用符で。

+0

この問題を解決しました –

+0

喜んでお手伝いします:) –

+1

@Rabbyshah、おそらく上記の答えを正しいものとしてチェックしてください。 – Daniel

1

まず、一重引用符( ')と二重引用符( ")の違いを理解する必要があります。 一重引用符内の変数は、変数値を出力する代わりにそのまま出力されます。しかし、二重qouteの中で使用すると、最初に実行され、値を出力します。

詳細については、このリンクを参照してください:What is the difference between single-quoted and double-quoted strings in PHP?

関連する問題