Hi I can help with some of these.
ganz_friedrich wrote:Firstly, and most importantly, does php cache the scripts that it compiles? (I'm guessing it doesn't re-compile a script every time it is requested and only checks if it has been modified). I would like to know, because if it does cache, it makes no sense to declare functions in if statements (where the purpose would be to save function space and processing) because the compiled function will be cached anyway (am i correct in thinking this?). Also, if php caches compiled scripts then there should be no performance dissadvantage to using assoc arrays as oppose to numeric array (that is, after the first complie)?
Ok, PHP does not compile scripts in the sense of a traditional compiled language. PHP is an interpreted language, and uses a dual pass system. First, it runs through and checks syntax, etc, and converts the source code to "opcodes", which are instructions for the interpreter. Then the interpreter runs the opcodes, and the script does whatever it was coded to do. Hence a dual pass system. At least this is my understanding of how it basically works.
Now to your question, PHP does this on every run. However, you can install an opcode cache extension, which caches the result of the 1st pass, hence saving one pass of the interpretation process on each run.
ganz_friedrich wrote:When passing variables to functions normally, is php clever enough to know only to make a copy of the variable data when necessary (i.e. passes by reference, until some change to the variable is made)? What about foreach loops, etc. is it the same for these?
With PHP 5, it passes objects by reference and variables by copy by default. If you want to pass a variable that is not an object by reference, you have to explicitly say so in the function definition [function funcName(&$var1, $var2, ..)]
The same is true for returning references as well, object are returned by reference, but other variable types are not unless explicitly set in the function definition [function &funcName($var1, ... )]
With PHP 4, all variables, even object, are always passed by copy by default.
ganz_friedrich wrote:Is it a lot more expensive to output concatinations of "single-quotated strings" and variables than to output everthing as one "double-quotations string" (which php checks for variables)? Or is there a faster way to output a string that needs to have variables inserted in various places? (I've heard that starting a buffer and echoing can sometimes be more efficient, but are there any other methods?)
No, double quotes will be more expensive, since PHP does not just output that data, but actually parses it. Single quotes will be less expensive. And for concatenations, for max performance, use a comma instead of a period:
echo 'Hello ', $name, '! How are you today?';
ganz_friedrich wrote:When making variables global within a function are they then accessible as global anywhere? Or does this just make them available to the function? (i.e. is using the global statement inside a function identical to using it outside a function?)
You'd use the global keyword in a function to access a variable that does not exist to that function, ie, it exists outside of the function. The opposite also works, meaning if it does not exist outside the function, and you then set it inside the function, then it will exist outside the function:
php > function getMe() {
php { global $me;
php { if (!isset($me)) $me = 'Set by Function';
php { return $me;
php { }
php >
php > echo getMe();
Set by Function
php > echo $me;
Set by Function
php >
ganz_friedrich wrote:If mysql queries are correct, can they still fail e.g. due to mysql server overload?
How can you be certain the query won't fail? even if the query is technically correct, there are still other reasons a query might fail, so you should always check that it did not error. if you're using a db connection/abstraction layer, you can do this right in the layer so it is "hidden" from the rest of your code.
My query() method of my MySQL db connection driver looks like this for example:
class MysqlDB extends BasicDB {
//...
public function query($query) {
if ($this->db && ($this->db !== NULL)) {
if ($this->result = $this->db->query($query)) {
return true;
} else {
$this->_addError($this->db->error, "query($query)");
}
} else {
$this->_addError('Not connected to a db!', 'disconnect()');
}
return false;
}
//...
}
Which is used like (where $db is an instance of MysqlD๐:
$db->query('SELECT * FROM foo');
Since the error checking is being done in MysqlDB, I don't have to redo it every time I run a db query. MysqlDB takes care of that for me. ๐