-2

Hey guys so my task is to print out a volume using a variable with a random number from 1 to 100. If the volume is lesser than 30 it has to print out a text called quiet at a certain size listed in my code, and the same thing with a different text called normal if the volume is lesser than 70. However, when I try to implement the if statements I keep getting a Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in C:\xampp\htdocs\PracticeLabExam\task3.php on line 17 and don't understand what I am doing wrong

<html>
<title> Task 3 </title>
<style>
  p { width: 75%; margin: auto; color:blue; background-color: silver;
      padding:20px;  }
</style>
<body>
<h1> Task 3: PHP Program #1 </h1>
<?php 
    $number = mt_rand(1,100);
?>
<p style="text-align: center"> Volume is <?php echo $number; ?></p>
<?php 
     if($number < 30) { 
        ?> <p style="font-size: 0.5em; text-align: center"> quiet </p>
     } 
     <?php elseif($number < 70 && $number > 30) { ?>
        <p style="font-size: 1.25em; text-align: center"> normal </p>
     } 
     <?php else { ?>
        <p style="font-size: 3em; text-align: center"> loud </p>
    } 


</body>

</html>

If I try to change it like this:

<?php 
     if($number < 30) { 
        ?> <p style="font-size: 0.5em; text-align: center"> quiet </p>
     } 
     <?php if($number < 70 && $number > 30) { ?>
        <p style="font-size: 1.25em; text-align: center"> normal </p>
     } 
     <?php if($number >= 70) { ?>
        <p style="font-size: 3em; text-align: center"> loud </p>
    } 


</body>
</html>

I get Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\PracticeLabExam\task3.php on line 27

Please help.

Rookie
  • 675
  • 1
  • 7
  • 9

4 Answers4

1

The error is stating that you aren't closing one of your if statements. This is because your closing brackets } are not housed within the PHP tags and is interpreted as HTML instead.

I'd recommend using embedded if/else statements. Your code will be much cleaner and easier to read.

<?php if($number < 30): ?>
    <p style="font-size: 0.5em; text-align: center"> quiet </p>
<?php elseif($number < 70 && $number > 30): ?>
    <p style="font-size: 1.25em; text-align: center"> normal </p>
<?php else: ?>
    <p style="font-size: 3em; text-align: center"> loud </p>
<?php endif; ?>

See http://php.net/manual/en/control-structures.alternative-syntax.php

SteppingHat
  • 1,199
  • 4
  • 19
  • 50
0
<?php 
     if($number < 30) { 
        ?> <p style="font-size: 0.5em; text-align: center"> quiet </p>
<?php } ?> 
     <?php elseif($number < 70 && $number > 30) { ?>
        <p style="font-size: 1.25em; text-align: center"> normal </p>
<?php } ?> 
     <?php else { ?>
        <p style="font-size: 3em; text-align: center"> loud </p>
<?php } ?> 
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
0
<?php 
     if($number < 30) { 
        ?> <p style="font-size: 0.5em; text-align: center"> quiet </p><?php
     } 
     if($number < 70 && $number > 30) { ?>
        <p style="font-size: 1.25em; text-align: center"> normal </p><?php
     } 
     if($number >= 70) { ?>
        <p style="font-size: 3em; text-align: center"> loud </p><?php
    } 
?>

</body>
</html>
flauntster
  • 2,008
  • 13
  • 20
0

You don't have closing PHP tag. That's why PHP also rendering the HTML tags that should be outside the PHP tags.

<?php 
    if($number < 30) { 
?>
        <p style="font-size: 0.5em; text-align: center"> quiet </p>
<?php
    } 
    else if($number < 70 && $number > 30) {
?>
        <p style="font-size: 1.25em; text-align: center"> normal </p>
<?php
    } 
    else if($number >= 70) {
?>
        <p style="font-size: 3em; text-align: center"> loud </p>
<?php
    } 
?>

</body>
</html>

And also, your code is a little bit messy. Try to clean it up a little bit. And I suggest to use else if instead of multiple if statement.

  • I tried using elseif and else initially but got the parse error as noted before. But your feedback still helped a lot thank you. – Rookie Oct 23 '17 at 06:16
  • That's because you don't have PHP closing tags. The `unexpected end of file` commonly occured when there is an opening PHP tag and there's no matching closing tag. If you want to use a HTML tag, use `echo` or try my code, replace the `else if` with `if` then comeback here if it worked. – Pia Wurtzbach Oct 23 '17 at 06:22