tag:blogger.com,1999:blog-4364916510451597580.post5259636282399176904..comments2018-09-11T04:50:49.110-07:00Comments on PC Technology: PHP: file_get_contents catch exceptionultimatumhttp://www.blogger.com/profile/04127680290757641553noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-4364916510451597580.post-14661366022967722802010-07-15T13:06:16.588-07:002010-07-15T13:06:16.588-07:00Why would you want to call the fetch twice? That&#...Why would you want to call the fetch twice? That's VERY expensive. Just all it once and check the return value as follows:<br /><br />if (($a = file_get_contents( $url )) === false ) {<br /> echo "some error";<br />}else{<br /> .... whatever it is that you wanted to do with the $a string<br />}<br /><br />This is important, you will also want to use === or !== instead of just == Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4364916510451597580.post-43446422012168958212010-05-13T09:05:39.436-07:002010-05-13T09:05:39.436-07:00For those of you that want to use exception handli...For those of you that want to use exception handling then you can take this approach which will increase performance by not having to call the file_get_contents() function twice:<br /><br />$result = @file_get_contents($url);<br />try<br />{<br /><br />if ( $result == FALSE )<br />{<br />throw new Exception ("Failed to get page contents.");<br />}<br /><br />}<br /><br />catch(Exceptionultimatumhttps://www.blogger.com/profile/04127680290757641553noreply@blogger.comtag:blogger.com,1999:blog-4364916510451597580.post-34737623971856748742010-05-13T09:03:06.194-07:002010-05-13T09:03:06.194-07:00This comment has been removed by the author.ultimatumhttps://www.blogger.com/profile/04127680290757641553noreply@blogger.com