• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Fopen() unexpected end of file error

Kaworu

New Coder
My problem

Hello!

I am learning how to code in PHP. I created a simple page that is simulating a car parts store. I encountered some error after opening a file and checking if it is working. What surprises me most is the fact, that PHP is telling me of some kind of “unexpected end of file error” that is happening after the </html> ending tag.

[In fact I do not know if opening a file is a problem in this case or just if/else statement. It’s hard to tell, to be honest.]

It’s a really simple code, yet it is not working at all. And I do not know what I did wrong. If some of you could help me out, I would be grateful.

Explanation of the code

Okay, so I have a html page when you can tell how many tires/car oil etc you want to buy. This form links to a php script that should open orders.txt file. If it cannot be opened, there should be some kind of explanation to the user (if statement). If it can be opened, the user should also get a confirmation.

My bug

The error message is as follows:

[B] Parse error[/B]: syntax error, unexpected end of file in [B]D:\xampp\htdocs\scripts\Ch1\place_an_order_v2.php[/B] on line [B]36 [/B]

Line 36 is a line after </html> closing tag and well after ?> tag.

Source code

place_an_order_v2.php:
<!DOCTYPE html>

<?php
    
    $document_root = $_SERVER['DOCUMENT_ROOT'];

?>

<html>
    <head>
        <title> Car parts - an order </title>
    </head>
    
    <body>
        <h1>Car parts</h1>
        <h2>Sent order</h2>
        <?php
        
            $wp = fopen("$document_root./orders - OK/orders.txt", "ab");
            
            if($wp == false):
            {
                echo "<p><b>We are sorry, we cannot take your order right now.
                    Please, try again later.</b></p>";
                exit;
            }
            else:
            {
                echo "<p><b>We did take your order. Thank you for your business.</b></p>";
            }
        
        ?>
    </body>
</html>

HTML code linking to PHP code (in case you need it):
<form action = "place_an_order_v2.php" method= "post">
    <table style = "border: 0px;">
        <tr style = "background: #cccccc;">
            <td style = "width: 150px; text:align: center;"> Product </td>
            <td style = "width: 15px; text-align: center;"> Quantity </td>
        </tr>
        <tr>
            <td> Tires </td>
            <td><input type="text" name="number_of_tires" size ="3" maxlength="3" /></td>
        </tr>
        <tr>
            <td> Car oil </td>
            <td><input type="text" name="number_of_oil" size="3" maxlength="3" /></td>
        </tr>
        <tr>
            <td> Spark plugs </td>
            <td><input type="text" name="number_of_spark_plugs" size="3" maxlength="3" /></td>
        </tr>
        <tr>
            <td> Adress</td>
            <td><input type="text" name="adress"</td>
        </tr>
        <tr>
            <td colspan="2" style="align: center"><input type="submit" value="Send order" /></td>
        </tr>
        
    </table>
</form>
 

Ghost

Active Coder
It's because you are using else: { and if($wp == false): {
You should not have a colon after the if statement or the else while using the { } squiggly lines.

If you want to use the colon syntax then remove the squiggly brackets and add an endif like this:
Code:
if($wp == false):
    echo "<p><b>We are sorry, we cannot take your order right now. Please, try again later.</b></p>";
    exit;
else:
    echo "<p><b>We did take your order. Thank you for your business.</b></p>";
endif;

If you want to use { } syntax without colons, then do this:
Code:
if($wp == false){
    echo "<p><b>We are sorry, we cannot take your order right now. Please, try again later.</b></p>";
    exit;
} else {
    echo "<p><b>We did take your order. Thank you for your business.</b></p>";
}

You can use colons or curly brackets, but you cannot use both.

One other thing to note is that if you are going to use an exit then you can do something like this:
exit("<p><b>We are sorry, we cannot take your order right now. Please, try again later.</b></p>");
It's ont a big deal, but you save a tiny bit of space and reduce your script by 1 line.

Another thing you could do is a ternary operation.
Code:
echo $wp == false ? "<p><b>We are sorry, we cannot take your order right now. Please, try again later.</b></p>" : "<p><b>We did take your order. Thank you for your business.</b></p>";
This works like this:
language construct (echo) + evaluate (if statement $wp == false) + ? + if-true (wp is false, show error) + : + if-false (wp is not false, show success)
so basically it will echo if-true or if-false depending on the evaluation of your if statement. It's a cool 1-liner way to do things in PHP (other languages have similar syntax too) if you just need a quick if statement. You can go several levels deeper though by doing like...
echo $blah == 1 ? "it is 1" : ($blah == 2 ? "it is 2" : "it is something else")

That's where you have multiple evaluations/comparisons so that if it's not true the first time, you check for something else.

Anyways, hopes this helps you in future :)
 

Top