Search code examples
phpif-statementquotes

How to put quotes correctly in this statement


I need to return an input box from a function but i could put the quotes correctly. Any one please help me to solve the error.

<?php
 return "
<input  style='background-color:#CCC;'type='text' name='contactName' id='contactName' value='".if(isset($_POST['contactName'])) echo $_POST['contactName']."' class='requiredField' />";

Solution

  • use this

    <?php
     return '
    <input  style="background-color:#CCC;" type="text" name="contactName" id="contactName" value="'.(isset($_POST['contactName'])?$_POST['contactName']:'').'" class="requiredField" />';
    

    for inline if that will output somthing use this syntax:

    ( condition ? 'the thing that will return when condition true' : 'the false returned string' )