Ivan Topić Ivan Topić - 1 year ago 59
PHP Question

Unwanted space between php and html

I have this code, the problem is that there is an empty space before each comma.

Name Lastname , (1990.) , Title ...

It should be

Name Lastname, (1990.), Title ...

I need to have data in this format using echo.



<?php // DATE ?>
<?php if(!empty($ref['godina_izdanja'])) :
echo ', (<span class="date">' . $ref['godina_izdanja'] . '</span>.)';
endif; ?>

<?php // TITLE?>
<?php if(!empty($ref['title'])) :
echo ', <em><span class="title">' . $ref['title'] . '</span></em>';
endif; ?>

Is there something I could do to fix it but to have code readable? I know I can concat everything together but it will become hard to read and mantain

Answer Source


endif; ?>
                            <----right here
<?php // TITLE?>

In a PHP script, ANYTHING that's not within <?php ... ?> tags is treated as output. You have a blank line there, so you're outputting a blank line, which will be rendered as a space by your browser.

There is exactly ZERO point in repeatedly hopping in/out of php mode:

<?php echo 'hi'; ?>
<?php echo 'mom'; ?>

just makes for massively ugly code to read/maintain. Once you're in PHP mode, stay in PHP mode, especially if you're just going to hop in/out:

echo 'hi';
echo 'mom';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download