Page 2 of 2

Posted: Thu Nov 10, 2005 3:38 am
by Jenk
test.php:

Code: Select all

<?php

function @ myFunc () {
	echo $_GET['nonexistantindice'];
}

myFunc();

?>
Results:

Code: Select all

>C:\PHP\php.exe -l test.php

Parse error: syntax error, unexpected '@', expecting T_STRING in test.php on line 12
Errors parsing test.php
PHP Parse error:  syntax error, unexpected '@', expecting T_STRING in test.php on line 12
>Exit code: 255    Time: 38.478
and lol@this crappy work machine

Posted: Thu Nov 10, 2005 3:41 am
by n00b Saibot
PHP Error wrote:expecting T_STRING in test.php on line 12
where is that o_O you have only 9 lines of code... :?

Posted: Thu Nov 10, 2005 3:42 am
by Jenk
I have loads of commented stuff that I didn't post :)

Posted: Thu Nov 10, 2005 8:19 am
by Burrito
ok final question:

what would a & do in place of the @ in that same scenario?

ex:

Code: Select all

function & myFunction()
{
   //stuff
}
looking for an answer other than "reference"...

Posted: Thu Nov 10, 2005 8:28 am
by Jenk
The results of the function are returned by reference, instead of copy.

only really applicable to arrays and objects and the only real functional use is for performance, but in PHP this is automanaged and is not necessary :P

Posted: Thu Nov 10, 2005 10:27 am
by Maugrim_The_Reaper
I can't find the page for it atm, but it only supresses non-fatal errors (E_WARNING and E_NOTICE errors and E_STRICT in PHP5), if it is E_CORE_*, E_COMPILE_*, E_ERROR or E_PARSE error (all fatal errors), then you will still see an error message.
If @ is used in conjunction with include() or require() it will supress all error (even parse) from within the included files. In that case it is an absolute disastor waiting to happen... Apparently few realise just how messy that usage makes finding fatal bugs that are never reported or logged...
The results of the function are returned by reference, instead of copy.

only really applicable to arrays and objects and the only real functional use is for performance, but in PHP this is automanaged and is not necessary Razz
Referencing creates a "reference" to a variable. Basically you create something akin to a "bookmark" which points to the original variable. Without referencing in PHP4, every time you passed a variable to, or returned a variable from a function - it would be a copy. If you do a lot of each, or have very large variables (e.g. a data object) those copies will quickly eat up memory and effect performance. Worse, if you are trying to pass around just one object, and all changes are to be made to that object only - without references you'd end up with multiple copies, instead of the single completed object you wanted

Referencing in PHP5 is different - PHP5 already references all objects by default (a big difference from PHP4).

Summary - a reference is a "bookmark" to the original variable. Any changes to a reference, are applied to the original variable (wherever you initialised it).

For example:

Code: Select all

function addOne(&$foo) {
    $foo = $foo +1; // the referenced var
}

$foo = 0; // the original var

addOne($foo);

echo $foo; // returns 1 (operation on a reference, changes the original variable)
The other side is returning by reference:

Code: Select all

function &createFoo() {
    $foo = 0; // the original var
    return $foo;
}

$foo_ref =& createFoo(); // $foo_ref is a reference to the $foo created INSIDE the function

// important when returning references to use =& not just =
Any q's fire away...;)

Posted: Thu Nov 10, 2005 10:50 am
by m3mn0n
Excellent explaination, Maugrim.

Keep up the good posts! :)

Posted: Thu Nov 10, 2005 11:15 am
by Maugrim_The_Reaper
What? It made sense? ;) Cool. I just find references to be almost up with OOP in terms of new developers coming to grips with them... Forge on...