0

i want to know if exist function to know if my file have errors before include it just on the script as code bellow

if (file.php==errors ){echo"this file contain errors"} else { include("file.php");}

i want to use this php script to know if my file.php contain errors , if it contain't errors i will not include it , else i include it .

Amino
  • 9
  • 3
  • It sounds like this is what testing is for... if your file has compiler / syntax errors, this should be apparent long before you get this far. If not that, then what other kind of errors are you thinking of? You'll need to be more specific, perhaps with an example. – ADyson Mar 25 '21 at 15:20
  • I explain , i create php files containing variables ,i must include this files to get variables on this files , the problem is some files contains syntaxe errors , i want to test before include , is it clear ? now – Amino Mar 25 '21 at 15:26
  • So...you're planning to simply ignore a whole piece of functionality in your live application, just because it has compiler errors? Won't that affect the functionality of your application, or even cause other errors? It really makes no sense. Just fix the errors. – ADyson Mar 25 '21 at 15:27
  • How do you expect this to work? Errors/Exceptions are only thrown **during runtime**. So either you have a [error handler](https://stackoverflow.com/questions/134331/error-handling-in-php) which takes over in case something happens or your server would need to know the future. – Definitely not Rafal Mar 25 '21 at 15:27
  • 1
    And anyway no, it's not possible. If you include a script in another script, and then execute that script, then PHP evaluates the whole combined script as if it was one file. So it will cause the whole thing to fail with a syntax error. That, naturally, happens _before_ any of your code can run. Your whole concept simply doesn't make sense. If you have syntax errors, test the scripts and fix the syntax errors before the code goes live. Just normal testing...why would that be a problem for you? – ADyson Mar 25 '21 at 15:28
  • this files are generated automatiquelly , the problem is sometimes the generation can't be finished correctly for exemple file1.php – Amino Mar 25 '21 at 15:30
  • Why can't the automatic generation be completed correctly? That is the real issue you need to fix. This sounds like a classic [X-Y problem](http://xyproblem.info/) – ADyson Mar 25 '21 at 15:31
  • And why are you generating code automatically anyway? Are these just variables? Should they not be just stored as settings values in a database or something? The whole thing sounds very bizarre. – ADyson Mar 25 '21 at 15:33
  • the problem is that i generate files in localhost , and upload thems to web server, some files don't finish uploading , and i thig this cause the error – Amino Mar 25 '21 at 15:34
  • Why wouldn't the files finish uploading? Is there some problem with the FTP transfer to the webserver? If they fail, upload them again. And fix your connectivity issues. This is a deployment issue. You don't fix deployment issues with code hacks. (And you can't in this case anyway). Even if what you were proposing could work, it would probably just break your application in other ways because of the missing values. And again if these are just simple variable values that you're changing, consider having them as settings values in the database instead of lots hard-coded entries in the PHP. – ADyson Mar 25 '21 at 15:45
  • it's complicated, sometimes internet is disconnect while file is uploading , but i 'll developp script to verify some errors , thanks ADyson , – Amino Mar 28 '21 at 10:19
  • Maybe you should upload the files to a separate folder on the website, rather than their real folder. Then if the upload fails it won't break your site. Once you're happy that everything is uploaded correctly you can copy them to the real location on the server - that would be a much more reliable action because it happens entirely on the server without relying on the internet connection. – ADyson Mar 28 '21 at 10:25

1 Answers1

0

No, it's not possible. If you include a script in another script, and then execute that script, then PHP evaluates the whole combined script as if it was one file. So if there is a syntax error in any of the included scripts it will cause the whole thing to fail. That, naturally, happens before any of your other code can run. Your whole concept simply doesn't make sense. If you have syntax errors, test the scripts and fix the syntax errors before the code goes live.

And, as the comments suggest, if the real issue is that some files do not get deployed properly, then the solution is to fix the issues which are causing the deployment to fail.

ADyson
  • 57,178
  • 14
  • 51
  • 63