tags:

views:

213

answers:

5
<?php
#Console Application
#Öppnar Fil.
$file = fopen("words.txt","r")

#Finns Filen? 
if(file_exists("words.txt")) {
echo "Filen Finns";
}
?>

Gives me error

PHP Parse error: parse error in C:\main.php on line 7

What im going wrong? do you see the problem?

+15  A: 
$file = fopen("words.txt","r")

You need a ; at the end of this line.

Also, since you are a beginner, you might be stop when the program says the error is on line 7 when its actually on line 4. This happens a lot when you are missing the closing semicolon ;

PHP keeps on going down your script looking for it, and when you program in actions that are surely new actions (like an if statement) PHP will throw the error, and it can only return to you the line where it stopped at, since its pretty hard to know exactly where the error is supposed to be at.

Ólafur Waage
+4  A: 

I realize this answer doesn't answer the actual question as Olafur answered just fine. However, you should be checking to see if the file exists before you try to open it.

<?php
#Console Application

#Finns Filen? 
if(file_exists("words.txt")) {
echo "Filen Finns";
} else {
 #Öppnar Fil.
 $file = fopen("words.txt","r");
}
?>
Suroot
I agree with Suroot.
Ólafur Waage
Also, obviously, close all streams with fclose() when you're done. Especially if you use a Windows server.
Alan
A: 

Ólafur is right. I find that when I'm writing PHP, the most common errors I run across are mixing up " and ', capitalization mishaps, and missing semicolons. Most of this you just learn to fix before it becomes as problem as you get a chance to work with PHP (or any similar language) enough. Another one is mismatched brackets [(((.....)...)], and small typos that may not cause an error, but cause your code to not work, so there's a little bit to look for as you code in PHP. I hope you do well in PHP and enjoy it!

Sukasa
A: 

recommend you to use a good PHP editor (or HTML editor). and when you test, turn on the server error message output. Don't hide it. so that you can see what error & the line #.

Murvinlai
A: 

I would suggest either using VIM / Notepad++ or Eclipse with PHP edit support as it will help you with these types of errors in the future

PSU_Kardi