if ( $fp = fopen( "test.txt", "w" ) ) { // do something with $fp } Or you can use a logical operator to end execution if an essential file can't be opened: ( $fp = fopen( "test.txt", "w" ) ) or die ("Couldn't open file, sorry");