1

I want to find out whether a particular file is valid PHP file or not?

php -l filename only finds our whether it have syntactical errors or not, it is lint.

But I also want to find out, that on execution, it will run without error or not?

I am running on Ubuntu machine.

EDIT:

Actually my problem is I am allowing users to define formulas for some calculations and I have to execute those formulas, but before submission of formulas by users, I want to make sure whether they are correct or not in PHP

Charles
  • 50,943
  • 13
  • 104
  • 142
Jatin Dhoot
  • 4,294
  • 9
  • 39
  • 59
  • The only way to find out if it will run or not is to run it with a wide range of inputs. Linters and other analysis tools cannot anticipate the range of inputs it might encounter. But realize, per your title, that a script may encounter errors and still be a "valid PHP file". – Jonathan M Jun 25 '12 at 15:22
  • 2
    You need to write unit tests. As 'without error' is undefined. see: http://www.phpunit.de/manual/current/en/ – ctrl-alt-delor Jun 25 '12 at 15:23
  • Thanks Jonathan And Richard. But if we know that our PHP will contain only static things such as messages etc, no inputs, then also? – Jatin Dhoot Jun 25 '12 at 15:25
  • @Jatin, Yes sir, you still just have to run it. Do so in a controlled (development) environment that closely approximates the production environment, of course. But you still just have to run it. – Jonathan M Jun 25 '12 at 15:28
  • You need to define "valid". A PHP file without the `` would be valid. Would it not? – Noufal Ibrahim Jun 25 '12 at 15:30
  • Actually my problem is I am allowing users to define formulas for some calculations and I have to execute those formulas, but before submission of formulas by users, I want to make sure whether they are correct or not in PHP – Jatin Dhoot Jun 25 '12 at 15:31
  • hmm, maybe if he wants to pipe it with another script and to see if the php script failed? like php myphp.php && do_x – Uku Loskit Jun 25 '12 at 15:32
  • @Noufal Ibrahim - U r totally correct,plz try to understand my requirement by above posted comment by me – Jatin Dhoot Jun 25 '12 at 15:32
  • @Uku Loskit -- absolutely correct, – Jatin Dhoot Jun 25 '12 at 15:33
  • I hope you're aware of all the risks involved in executing PHP code provided by users. – JJJ Jun 25 '12 at 15:35
  • @Jatin, one thing you'll want to do as you proceed: limit what you allow the users to do in the formulas. You don't want to give them all PHP capabilities, or they can hack the daylights out of your server. There's likely a better way to do this. What types of formulas are you expecting from them? For what purpose? – Jonathan M Jun 25 '12 at 15:37
  • Don't execute code obtained from untrusted sources in a privileged environment. Either run it inside some kind of jail or expose a mini language you've implemented that people can enter their formulae in and then "compile" it into PHP and run it. – Noufal Ibrahim Jun 25 '12 at 17:11
  • @Jonathan -I am providing user to define the formula for cost calculation depending upon various input parameters like quantity,material used etc many parameters. And the parameters are also dynamic, you can say I need a small language itself. – Jatin Dhoot Jun 26 '12 at 02:17
  • @NoufalIbrahim - What kind of language that can be? Can you please give me an example or resource? – Jatin Dhoot Jun 26 '12 at 02:18
  • @Juhana - Yes, I am, Please suggest me work around if possible. My requirement is mentioned in above 2 comments. – Jatin Dhoot Jun 26 '12 at 02:20

1 Answers1

0

it will be really hard to know if a php script will throw an error or not. unless formulas are well defined and that you can define a grammar in order to check if it's correct. eg: you can only add integer,

SUM -> N | N + N
N -> integer

but even like this, you will have soon problems, eg, if you allow to N1 / N2, you would have to ensure is never null.

my advice, make sure your program recover from an error. and, in case of on error, notify the user that input the formula with details about the error

Mathieu
  • 5,495
  • 2
  • 31
  • 48