PHP回显html的格式

I am trying to echo out multiple table rows on my sql query and provide them with alternating colors to improve the aesthetic value of my site. I am fairly new to php and I am extremely fussy over the presentation of my code and therefore I would like to include the output html into my PHP block to improve readability.

I have browsed some past threads but I am still rather unclear of how the formatting of a string works in PHP, the code below shows my attempt of formatting the output:

echo '<tr class=" . 'if( $class_style %2 == 0 ){ echo "row_dark"; } else echo "row_light"' . ">';

What am I doing wrong here?

Regards Alex.

You should use like this right syntax:

echo '<tr class="'.($class_style %2 == 0 ? "row_dark" : "row_light").'">';

It should be

echo '<tr class=" '. instead of echo '<tr class=" .'

You can't put an if structure in an echo.
Use that :

echo '<tr class="'. ($class_style %2 == 0) ? 'row_dark' : 'row_light' . '">';

It's a ternary operation.

<?php echo '<tr class="'.(($class_style %2 == 0)?'row_dark':'row_light').'">';?>

or

<?='<tr class="'.(($class_style %2 == 0)?'row_dark':'row_light').'">';?>