-2
 <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();
    }
    ?>

In here -------- Output Is: Message: Your Name Is: $name Age: $age Nowadays You are working as: $job Alooha!!Your are Adult :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

if i add age less than 18 than it throws: Sorry rabby,You are not not enable to see this page

Please fix this problem.

Rabby shah
  • 103
  • 1
  • 6
  • Change single quotes `'` to double quotes `"` in `throw new exception('Your Name Is: $name
    Age:$age
    Nowadays You are working as: $job
    Alooha!!Your are Adult :D'); `
    – Sahil Gulati Jun 20 '17 at 05:04

2 Answers2

2

it should be:

throw new exception('Your Name Is: '.$name.'<br>Age:'.$age.'<br> Nowadays You are working as: '.$job.'<br>Alooha!!Your are Adult :D');

or

throw new exception("Your Name Is: $name<br>Age:$age<br> Nowadays You are working as: $job<br>Alooha!!Your are Adult :D");

In single quote you need to concatenate string to print PHP variable.

Jigar Shah
  • 6,143
  • 2
  • 28
  • 41
1

First you need to understand the difference between Single quotes('') and double quotes(""). The variables inside single quote will get printed as it is instead of printing the variable value. But when use it inside double qoute it will get executed first and print the value.

Please refer to this link to know more about it: What is the difference between single-quoted and double-quoted strings in PHP?.

Aman Agarwal
  • 132
  • 7