I encountered the concept of exception handling in a java applet I was back engineering, in that case there was no way around using those try and catch exceptions. I found out they have them in PHP so I wanna learn how to use them but I can't see how they are any different to just using if statements. For example, I'm making a web spider right now and I decided to try and make use of exceptions:
try {
@$new_input_file = file_get_contents($wiki_url);
if ($new_input_file == FALSE) {
throw new Exception("Could not find the wiki page");
}
else { file_put_contents($input_file_path, $new_input_file); }
}
catch (Exception $e) { echo $e->getMessage(); }
As you can see, it tries to get content from the wiki page, if it can't and the file_get_contents function returns FALSE, then it throws a new exception. From what I read, without the @ symbol in front of the $new_file_input variable, the file_get_contents() function would return an error message rather than FALSE.
The code works but it seems completely pointless, why wouldn't I just do:
if (@$new_input_file !== FALSE) {
file_put_contents($input_file_path, $new_input_file);
}
SIDE QUESTION: Whats the point in using fread() to get a files contents when you can just use file_get_contents()? Are there any advantages fread()? I know that fwrite() has the advantage over file_put_contents() of being able to append strings to a file, but if you just wanna write to a file once, then I'm guessing file_put_contents() is better since.