Goat Posted July 24, 2010 Share Posted July 24, 2010 let's say I have this code: <?php mysql_connect(yadda yadda yadda....); mysql_select_db(yadda yadda...); $arr = mysql_fetch_assoc(mysql_query('SELECT yadda yadda')); if($arr['timestamp']<=$_REQUEST['timestamp']) {die();} /////// /// if table timestamp is more recent than _REQUEST timestamp, execute this huge lump of code: /// HUGE LUMP OF CODE STARTS HERE include('something big'); include('something else big'); /// yadda yadda...... /// yadda yadda...... /// yadda yadda..... ?> Bottom line, I have very short code that extracts some timestamp from some database and checks if there is something new. If there is nothing new, it dies. If there is, it executes massive code. Now the question is this: Does the interpeter first interpret whole code and then execute it (slow) or does it interpret and execute code line by line,thus skipping whole huge code when he doesn't need it? regards, Goat Quote Link to comment Share on other sites More sharing options...
smonkcaptain Posted July 24, 2010 Share Posted July 24, 2010 The whole point of IF and ELSE statement is really the answer to your question, <?php if($name='jimmy'){ /*short piece of code*/ }else if ($name!='jimmy'){ //*long piece of code*// } ?> Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 24, 2010 Share Posted July 24, 2010 The interpreter first tries to compile entire script (that's why you get syntax error messages even in parts of code that are never run). Quote Link to comment Share on other sites More sharing options...
Goat Posted July 24, 2010 Author Share Posted July 24, 2010 The interpreter first tries to compile entire script (that's why you get syntax error messages even in parts of code that are never run). okay thanks. But can you tell me how to circumvent that? Put part of the code into another file and then include it inside else statement? Quote Link to comment Share on other sites More sharing options...
Alex Posted July 24, 2010 Share Posted July 24, 2010 That wouldn't help; it would still interpret the included file(s) as well. Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 24, 2010 Share Posted July 24, 2010 Just don't bother with it. There are other areas where you can optimise your code. You can also use tools like APC, which cache compiled PHP bytecode. Quote Link to comment Share on other sites More sharing options...
Goat Posted July 24, 2010 Author Share Posted July 24, 2010 Thanks Mchl and AlexWD for extensive info. I haven't quite figured out what is smonkcaptain saying but thank to him for replaying. Idea: Maybe I put include() inside eval()? I know eval is usually very slow, but I have heard that if you call function from it, it's faster. So something like: eval("include('big_file.php');"); Eval itself is slow, but I think big_file.php will be interpreted and executed separately... right? I am developing AJAX chat in pure php so I am obsessed about speed right now. Thoughts? Quote Link to comment Share on other sites More sharing options...
Alex Posted July 24, 2010 Share Posted July 24, 2010 That's not really a good idea either. You're really not going to see much of a speed difference anyway, nothing noticeable. As Mchl said, there are other ways that you should be seeking to optimize your code. Quote Link to comment Share on other sites More sharing options...
Goat Posted July 24, 2010 Author Share Posted July 24, 2010 Well I tried some things and code with include('something.php'); and eval("include(something.php');"); has same speed. I know eval is seen as bad by many, but I don't see why I should not take advantage of it. Quote Link to comment Share on other sites More sharing options...
Goat Posted July 24, 2010 Author Share Posted July 24, 2010 Wait, I don't even need eval, just variable in include, like this: $a = 'big_file.php'; include($a); well, I hope this is useful to someone else, too Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 24, 2010 Share Posted July 24, 2010 The thing with eval is this. PHP interpreter starts compiling your script, line by line. It gets to middle of it and encouters eval(). What it does? It needs to dump all code that was compiled and start a new taking into account the code to be evaluated in eval. So in fact you're slowing your script down. Quote Link to comment Share on other sites More sharing options...
Goat Posted July 24, 2010 Author Share Posted July 24, 2010 Well now I figured how to do that without eval. See second post . Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 24, 2010 Share Posted July 24, 2010 Can't see how's that different from include('big_file.php'); Quote Link to comment Share on other sites More sharing options...
Goat Posted July 24, 2010 Author Share Posted July 24, 2010 Can't see how's that different from include('big_file.php'); Interpreter can't figure out ahead of time (unless it has AI in it or something) that you want to include big_file.php, therefore it can't interpret that file until it gets to it. Therefore, script from OP will die without interpreting that file unless that file is really needed. Which is exactly what I wanted to do in the first place. Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 24, 2010 Share Posted July 24, 2010 You still do not quite understand how it works. Quote Link to comment Share on other sites More sharing options...
Goat Posted July 24, 2010 Author Share Posted July 24, 2010 You still do not quite understand how it works. look at this code <?php mysql_connect(yadda yadda yadda); mysql_select_db(yadda yadda); $arr = mysql_fetch_assoc(mysql_query('SELECT yadda yadda')); if($arr['timestamp']<=$_REQUEST['timestamp']) { die(); // big_file.php never gets included and is thus never interpreted, speeding up script a little } else { $a='big_file.php'; // cheats interpeter so it doesn't know right away we want big_file.php include($a); // now that we actually need big_file.php , it gets included, interpreted and executed } ?> Sorry if I misunderstood you (and you are rather knowledgeable guy so I wouldn't want to), but you seem to be saying that interpreter will find out ahead of time (before getting to else part) that I am including big_file.php and convert it to byte-code (thus needlessly slowing down execution when that slice of code is unnecessary). Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2010 Share Posted July 24, 2010 A quick experiment shows that an include() statement is evaluated at runtime and the content in the included file is parsed/tokenized/interpreted at that point in time. Create a main file (a.php) that outputs something and includes a file (b.php) that intentionally contains a fatal parse error - a.php - <?php echo '1'; include('b.php'); echo '2'; ?> b.php - <?php a parse error; ?> Running this produces the output from the first echo statement, followed by the parse error in the b.php file - 1 Parse error: syntax error, unexpected T_STRING in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\b.php on line 2 Edit: Also by putting the include() statement inside of an if(){} statement that is first true (produces the parse error), then false (no parse error and produces the expected 12 output from the echo statements in the main file) confirms if and when the code in an include statement is parsed. Quote Link to comment Share on other sites More sharing options...
Goat Posted July 25, 2010 Author Share Posted July 25, 2010 Thanks, PFMaBiSmAd . Now I feel somewhat stupid for not trying that right away . regards, Goat Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.