Forum Moderators: coopster

Message Too Old, No Replies

How php executes code

         

franco190453

2:57 pm on Nov 4, 2009 (gmt 0)

10+ Year Member



Which statement do you think it is true?
1.- Php reads code strictly line by line.
2.- Php reads code line by line looking for errors.
If errors are found code is NOT executed at all!.
3.- Php reads code line by line looking for errors.
If errors are not found goes back and executes the whole code line by line.
4.- If code contains functions and classes: Php
reads code line by line looking for errors and parsing functions and classes. If errors are not found executes code line by line and if a function is at the end of the code it will not be a problem.
5.- If a request within a php code exists for(example)
an image located at a remote URL, then PHP will stop
executing until the image is retreived and then proceeds.

Your comments, Or any other ideas that will improve my knowledge of PHP will be appreciated!

Regards
Franco

mack

1:44 am on Nov 5, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I would say php reads the code from top to bottom based on variables passed to it. For example some of the code may not be read due to the ruit taken through the document.

If the file being accessed is file.php?yes=1

if ($yes == "2")
{
nothing here will be read
}

Regarding image loading. This is probably not a php issue, even a php file presents html to the browser, so the browser will handle it just like any other html document.

Mack.

TheMadScientist

4:03 am on Nov 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't remember where I read it (I've been trying to find the resource), but I know functions are loaded into the function engine for use prior to any code being executed, so my guess is:

Page is parsed for functions, classes, 'critical errors', then the code is processed in 'line order' some of which will not be 'executed', but will (can) generate an error... The code inside the if() {} will not be 'executed' since the condition is not met, but will generate an error, if the error is critical.

$yes=1;
if ($yes == "2")
{
'nothing here will be executed, but will be parsed and can generate a critical error'
}

rocknbil

5:12 pm on Nov 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Was a little worried about this one, sounded like a homework question. :-)

Understand the basic description of PHP: an HTML-embedded scripting language. Being such, it's default behavior is to execute inline, top to bottom, just like a web page or most scripting languages.

Whether it does that depends on how you've coded it.

It is not compiled first, as is Perl, which is one (incorrect) argument for being faster than Perl. The entire script and any included libraries are first evaluated for syntax errors, many of which manifest themselves as said, critical/fatal errors. It also generates warnings for non fatal errors such as inline undefined variables, output before attempting to do a header(), or other things that will not cause the program execution to cease.

If you have classes or functions, undefined variables and scripting errors that do not have syntax errors may not reveal themselves until the function or class is actually used. Since these can exist anywhere in the program, this is why I say "depends on how you code it:"

function some_function () {
}

// actually runs it here
some_function();

That would be a goofy way to code a script, but in many large scripts the exeution may not be strictly linear.

5.- If a request within a php code exists for(example)an image located at a remote URL, then PHP will stop executing until the image is retreived and then proceeds.

Again, how you code it, but with a twist, it depends on what you are doing. If you do this,

echo "<img src="some-remote-image.jpg">

The answer is no. It's output the code and goes on to the next line, allowing the browser to begin retrieval.

If you were do do something that actually reads the image, this would be different. A better example is large file uploads.

if (isset($_POST['file-upload-object'])) { upload(); }
output_success();

The file upload in upload() must complete before PHP executes output_success(). So the script indeed must wait for this to complete before continuing. Programmers would love it if it did go on, but the reason is that this is a single process and it only has a single pipe (probably wrong word.)

Whenever you request a page, plain HTML or PHP, you spawn a new process on your server and this process is assigned an ID. It may be passed around between applications - from server port to CGI gateway to interpreter back to server for response - but the system tracks it by this single process id. When it's output to the browser, the process ID dies.

It gets even more complex when you try to "fix" the large file upload issue by using fork(). You can fork a child process, which assigns it a new process ID, and do the upload in the child process, allowing the original process (the parent) to return an immediate response to the browser.

So in a way, you've executed it inline, but the child process is still running when the parent script has completed, output to the browser, and died. Puts a whole new spin on the question doesn't it? :-)

I'm not playing fair. fork() is a system process PHP executes to basically execute a second script from within your script, sort of.

eelixduppy

6:09 pm on Nov 5, 2009 (gmt 0)



PHP is written in C. The way everything happens is likely to follow that of C except that PHP code is parsed not compiled. At least that would make sense to me. ;)

An understanding at this level isn't significant if you're looking to be a great PHP programmer. Write clean, safe code and you'll be good. :)

rocknbil

8:21 pm on Nov 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The entire script and any included libraries are first evaluated for syntax errors, many of which manifest themselves as said, critical/fatal errors.

Been working on a project and this is partially wrong, or can be misread.

Scenario: You have a file with an include.

The include contains warnings or errors.

In the current project I'm working on, the include has an undefined index in the script.

The parent script runs, outputs the page, but when it gets to the part with the errant include, all that is output is "undefined index in include at line x." This leaves the page half rendered in the browser.

The parent script does not continue on from this point.

So while I was partially correct about checking the overall syntax, the above quote is not entirely correct. There can be hidden warnings or errors in an include that will cause the script to die as it executes in a linear fashion.

StoutFiles

8:43 pm on Nov 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$yes=1;
if ($yes == "2")
{
'nothing here will be executed, but will be parsed and can generate a critical error'
}

Hmm, didn't know that. Good to know if the content in that if block is really long and you think it wont slow down load time when it's not being run.

mack

8:50 pm on Nov 12, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Is it only certain errors that will cause a script to fail within the brackets? I know of examples where the script will only fail when a variable is passed?

Mack.