Forum Moderators: coopster
Your comments, Or any other ideas that will improve my knowledge of PHP will be appreciated!
Regards
Franco
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.
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'
}
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.
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. :)
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.