Results 1 to 2 of 2

Thread: [RESOLVED] what @ and & means in PHP

  1. #1

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Resolved [RESOLVED] what @ and & means in PHP

    Hi!

    I've searched all over PHP.net but can't figure out what @ means in front of a fonction... I think it is to avoid error message, but I can't be sure.. also I have seen object declaration like $db = &new DB; what does & do??? thank you
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    accoustic emo-rock band: a tailormade fable

    Visual Studio 2003 / Framework 1.1

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: what @ and & means in PHP

    @ is the error supression operator. It is equivilent to calling the error_reporting(0) before and after the statement. In effect it prevents PHP from spitting out any notices, warnings or fatal error messages. It DOES NOT however, make the error go away, fatal errors still kill the script and warnings or notices which would once be displayed may go un-noticed. You should use the error suppression opertor in conjunction with a check ie:
    PHP Code:
    if (! ($fhwnd fopen('blah''r')) {
        
    /* file failed to open */
        

    The above statement attempts to open a file, if it fails the code inside the If statement is executed. The error supression operator is used to prevent PHP from displaying the warning message generated if it does fail.

    & is the variable reference opperator. In PHP a variable identifier: i.e: $foo, is a name used to identify a specific location in memory. This location will contain a value. Sometimes, it may be necessary to have several identifiers which point to the same location - this is especially the case with objects and arrays; most languages infact, including PHP 5+, use references by default for objects.

    In contrast to a normal variable identifer, in a reference, the variable name instead referes to a location in memory, which contains another location in memory. When we refer to a referenced variable, PHP looks up that location and fetches the value from there.
    PHP Code:
    $variable1 'hello';
    $reference1 = &$variable1;
    $variable2 $variable1;
    $reference2 = &$variable1;

    echo(
    $variable1); // outputs hello
    $reference2 'goodbye'// this changes $reference1 to a variable
    echo($variable2); // outputs hello

    $variable1 "goodbye";

    echo(
    $reference1); // outputs goodbye 
    As you can see, a change to the variable causes this change to be reflected by all variables that hold a reference to it, as they all refer to the same location. Unfortunatly PHP does not have a dereference operator, so it is not possible to update the real variable via the reference.

    In PHP 4, arrays and objects are always assigned by value, that means that if you create an object and assign it to another variable, all its data is copied into a new location in memory. For objects in particular this behaviour (known as cloning) is rarely required and can lead to problems, for eacmple, cloning an object in PHP 4 causes its contsructor to be called again on the new instance.

    It is for this reason that in PHP 4 objects are often created as follow:
    PHP Code:
    $ref = &new ObjectName
    Now, when the object is assigned to another variable:
    PHP Code:
    $ref2 $ref
    It too will hold a reference to the same object.

    As of PHP 5, you need to worry about this as all objects are passed by reference as default.
    Last edited by visualAd; Apr 11th, 2006 at 05:18 PM.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width