Building text inside a while loop I use the following code &lt;div&gt;&lt;?php echo $obj-&gt;text; ?&gt;&lt;/div&gt; in while loop.

Is this the best way?

Is there a better way, either to optimize or replace this code?

&lt;?php
    $akhbarkotah1 = $conn-&gt;prepare("SELECT text,time FROM small WHERE active='0' ORDER BY time DESC LIMIT 10");
    $akhbarkotah1-&gt;execute();
    while($obj = $akhbarkotah1-&gt;fetch(PDO::FETCH_OBJ)){

?&gt;
&lt;div&gt;&lt;?php echo $obj-&gt;text; ?&gt;&lt;/div&gt;
&lt;?php
    }
?&gt;
It looks like you don't really need to exit out of the PHP tag on this one.

&lt;?php
    $akhbarkotah1 = $conn-&gt;prepare("SELECT text,time FROM small WHERE active='0' ORDER BY time DESC LIMIT 10");
    $akhbarkotah1-&gt;execute();
    while($obj = $akhbarkotah1-&gt;fetch(PDO::FETCH_OBJ)){
        echo "&lt;div&gt;" . $obj-&gt;text . "&lt;/div&gt;";
    }
?&gt;


This simplifies the code and shortens it.  I think that I have the Syntax Correct.  I am rather new to PHP myself but I am pretty sure that this will work exactly the same.

If you leave your code the other way you should indent the HTML to match what is inside the PHP since it is really inside the PHP (kind of/sort of)  like this

&lt;?php
    $akhbarkotah1 = $conn-&gt;prepare("SELECT text,time FROM small WHERE active='0' ORDER BY time DESC LIMIT 10");
    $akhbarkotah1-&gt;execute();
    while($obj = $akhbarkotah1-&gt;fetch(PDO::FETCH_OBJ)){
?&gt;
        &lt;div&gt;&lt;?php echo $obj-&gt;text; ?&gt;&lt;/div&gt;
&lt;?php
    }
?&gt;


I personally don't like the way that this looks though.