Rivet;11032569 wrote:1. Functions are apparently read before any input, validation, etc. occur so other than logically it should be at the (top?) or (botttom?) of the script. Is TOP the right answer?
This quote comes from the PHP manual page [man]functions.user-defined[/man]:
PHP Manual wrote:Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
(See that manual page for the examples given.)
Rivet;11032569 wrote:2. Same goes for arrays; it doesn't seem to matter whether I place them before or after the code that uses them; they still work just fine.
This one is false; arrays are just one type of data that can be stored in a variable, and you must define a variable before you use it.
In other words, this code:
<?php
shuffle($myaray);
echo "The first element of myarray is: $myarray[0]";
$myarray = array( "one", "two", "three" );
will produce output similar to:
Warning: shuffle() expects parameter 1 to be array, null given in <file> on line 3
Notice: Undefined variable: myarray in <file> on line 4
The first element of myarray is:
This is because variable assignments are not like function definitions; the former are evaluated during "run-time" (i.e. when the statements are actually encountered during the course of the script's execution), whereas the latter (minus the exception noted above) are done at "compile-time" (i.e. when the PHP interpreter is converting your script into the executable code that it will then "run").
Rivet;11032569 wrote:More directly, where could I find further information on such workings of PHP?
Well the manual is obviously the first resource that should be considered.
As for your other questions... it really depends on how in depth you want to get. In a nutshell, when a file is passed into the PHP interpreter, it's going to parse the script into actual machine code that can be executed, and then it... executes that code. PHP, despite being an interpreted language, is very similar to C. The execution starts with the first statement in the script. Where it moves from there would depend on things like control structure ([man]language.control-structures[/man]) that alter the otherwise sequential execution of statements.
The function definition location addressed above is similar to C as well; the C compiler doesn't need to come across a function definition before it can parse a function call. (The one that will have an issue if you never define that function would be the linker... but that's a bit off-topic I think.)