Search code examples
phpecho

PHP syntax for using echo or pint within echo or print


I'm using the server to write the html of a lot of my page to process some pagination. I'm getting a syntax error on the line just below the comment insert as shown below. I'm guessing it is because I'm trying to place echo within a echo? I've tried a lot of things here and am having no luck figuring out what I'm doing wrong. Any help appreciated greatly!

<?php

PDO STUFF HERE. . . 
if() {
foreach() {
echo ' <input name="flyer" type="file" id="'.$row['ad_link'].'" tabindex="9" /></td>
//following line showing syntax error in my text editor
  <input name="transaction" type="radio" tabindex="11" value="0"' . if($transaction == '0') echo '$chkvalue'; . '/><label for="listings">Listings</label>
//following line showing syntax error in my text editor
  <input style="margin-left:20px;" name="transaction" type="radio" tabindex="12" value="1"' . if($transaction == '1') echo $chkvalue; . '/> ' 
          }
     }

?>

Solution

  • if and echo are language constructs and as such cannot be used inside a string. You'll have to change the flow of the logic to fix this.

    Easiest fix is to just use a bunch of semicolons.

    This is not allowed:

    echo 'abc' . if( $a == 1 ) { echo '3'; };
    

    But this is fine, because it breaks everything up into steps:

    echo 'abc';
    if( $a == 1 ) { echo '3'; }