What does @ do?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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... :?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I have loads of commented stuff that I didn't post :)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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"...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...;)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Excellent explaination, Maugrim.

Keep up the good posts! :)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...
Post Reply