How to use PHP code in echo function?

Discussion in 'PHP' started by extraspecial, Sep 29, 2012.

  1. #1
    Hello guys, I have a PHP where I show results in a table and this table codes are used in a echo function as below, but I can't run other PHP codes there, how can I solve it? I need to publish results like name, date and time. Thanks

    
    echo"<table>
    <tr>
    	<td width='50%'>
    <h3>Contact Details:</h3>
    <table>
    <tr>
    <td id='header' width='35%'>Name:</td>
    <td id='data' width='65%' style='text-transform: uppercase;'><?php echo $name ;?></td>
    </tr>
    <tr>
    <td id='header' width='35%'>Date:</td>
    <td id='data' width='65%'><?php echo $date ;?></td>
    </tr>
    <tr>
    <td id='header' width='35%'>Time:</td>
    <td id='data' width='65%'><?php echo $time ;?></td>
    </tr>
    </table>";
    
    PHP:

     
    extraspecial, Sep 29, 2012 IP
  2. imocoder

    imocoder Member

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    The problem is that you are using echo inside the echo.
    So the right code is:
    
    <?php 
    echo"<table>
    <tr>
        <td width='50%'>
    <h3>Contact Details:</h3>
    <table>
    <tr>
    <td id='header' width='35%'>Name:</td>
    <td id='data' width='65%' style='text-transform: uppercase;'>".$name."</td>
    </tr>
    <tr>
    <td id='header' width='35%'>Date:</td>
    <td id='data' width='65%'>".$date."</td>
    </tr>
    <tr>
    <td id='header' width='35%'>Time:</td>
    <td id='data' width='65%'>".$time."</td>
    </tr>
    </table>";
    
    PHP:
     
    imocoder, Sep 29, 2012 IP
  3. extraspecial

    extraspecial Member

    Messages:
    788
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    45
    #3
    Yeah, that's right, I didn't know how to use it when html code is added...

    But then I figured out that I should only end php code and start after the html code again so it would be like;

    
    <?php 
    echo $mycode;
    ?>
    <table>
    <tr>
        <td width='50%'>
    <h3>Contact Details:</h3>
    <table>
    <tr>
    <td id='header' width='35%'>Name:</td>
    <td id='data' width='65%' style='text-transform: uppercase;'><?php echo $name ;?></td>
    </tr>
    <tr>
    <td id='header' width='35%'>Date:</td>
    <td id='data' width='65%'><?php echo $date ;?></td>
    </tr>
    <tr>
    <td id='header' width='35%'>Time:</td>
    <td id='data' width='65%'><?php echo $time ;?></td>
    </tr>
    </table>
    <?php 
    echo $rest;
    ?>
    
    PHP:
     
    extraspecial, Sep 30, 2012 IP
  4. qabati

    qabati Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Glad it worked out...
     
    qabati, Sep 30, 2012 IP
  5. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    138
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Your first code you pasted could of worked, because you didn't specify if your using the .html extension or .php
     
    Vick.Kumar, Sep 30, 2012 IP