Search code examples
phphtmlechoisset

Php isset inside an echo validation


Hi I want to check the following validation:

echo '<div id="msg"><?php echo isset($msg)?$msg:''; ?></div>';

I know that is wrong, what is the correct way to do that?


Solution

  • You're already within the PHP realm, you're running PHP.. you can't start another PHP. Also single quote ' wont expand variables. I'm running on a lot of assumptions from your project

    Using your logic you could do this:

    echo '<div id="msg">' . (isset($msg) ? $msg : '') . '</div>';
    

    but it would be best to not just output the div when the variable isn't set

    if(isset($msg)) {
        echo "<div id=\"msg\">$msg</div>";
    }
    

    More info on what you're trying to achieve would let me educate you more on best practices.

    Your edit suggestion still has you trying to start a new php block within a php block.

    You suggested to edit my answer (which was weird) with this:

    echo '<div class="container">
     <form action="includes/ajax.php" id="profilePictureUpload" class="dropzone">
       <div class="fallback">
         <input name="file" type="file" multiple />
       </div>
     </form>
    
     <h1>Gallery</h1>
     <div class="alert alert-warning my-2">
       <i class="fa fa-2x fa-exclamation-circle float-right"></i>
       <ol class="m-0">
         <li>Image uploading limit is 5.</li>
         <li>One image not more then 5MB.</li>
       </ol>
     </div>
      <div id="msg"><?php echo isset($msg)?$msg:''; ?></div>
     </div>';
    

    If you had read what i posted, you need to replace that 1 <div id="msg"... with what i posted. Did you check?