0

As the title reads I am using the echo function to create an h3 string which will insert a php value $lowprice and $highprice. The goal is to have the text read

Here are all the houses whos prices are between $lowprice and $highprice. The code breaks into individual lines like this

Here are all the houses whose prices are between $

100000 and $

500000 :

This is the code I have written, how do I get it all onto one line.

<?php
echo '<caption>';
echo '<h3> Here are all the houses whose prices are between $ </h3>'.$lowprice.'<h3> and $</h3>'.$highprice.'<h3> : </h3>';
echo '</caption>';
?>

2 Answers2

1

<h3> is a block element, meaning it will take up a whole line. I think you want to replace your inner <h3>'s with <span> tags which are inline elements:

Like this:

<?php
  echo '<caption>';
  echo '<h3> Here are all the houses whose prices are between $ <span>'.$lowprice.'</span> 
  and $<span>'.$highprice.'</span></h3>';
  echo '</caption>';
?>

Or you can simply remove all the inner tags all together, like this:

<?php
  echo '<caption>';
  echo '<h3> Here are all the houses whose prices are between $'.$lowprice.' and $'.$highprice.'</h3>';
  echo '</caption>';
?>
Bryan Elliott
  • 4,055
  • 2
  • 21
  • 22
  • Thanks mate. I was messing around and was getting one error after another. – Chris Lynch Mar 02 '20 at 23:30
  • @ChrisLynch, awesome, glad I could help! If my answer was helpful and answered your question, would you mind accepting it as answer. (check mark to the left of my answer), thanks! :) – Bryan Elliott Mar 02 '20 at 23:56
  • Of course. I have found this site amazing. Feel free to browse around, I occasionally post questions when I cannot get things to function. This place has expanded my coding ability and understanding of CS exponentially! Thanks Bryan! – Chris Lynch Mar 03 '20 at 03:29
0

The line breaks appear because you've made several h3 elements. You're closing and reopening h3 tags at every insertion, which is not necessary. The html output of your code is the following:

<h3>Here are all the houses whose prices are between $</h3>
<h3>100000 and $</h3>
<h3>500000 : </h3>

Which automatically adds breaks, since that is the behaviour of h3 elements.

What you need is this:

echo '<h3> Here are all the houses whose prices are between $'.$lowprice.' and $'.$highprice.':</h3>';

Better yet, don't use echo to define your html; html and php are interchangeable within the same file. A cleaner, more readable and more easily maintainable solution would be to form your script like this:

<caption>
    <h3>Here are all the houses whose prices are between $<?= $lowprice ?> and $<?= $highprice ?>:</h3>
</caption>

Generally, you switch between php and html like this:

<?php
...do some php
?>
<somehtml></somehtml>
<?php do some more php ?>
<morehtml>...

Note that <?= is a shorthand for <?php echo.

El_Vanja
  • 3,660
  • 4
  • 18
  • 21