Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Accessing a variable in a function from the main script

  1. #11
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by keyboard1333 View Post
    That's interesting. It's the & that makes it work like that yes?
    Yes.
    Quote Originally Posted by php.net
    References in PHP are a means to access the same variable content by different names ...Note that in PHP, variable name and variable content are different, so the same content can have different names.


    Quote Originally Posted by traq
    That's a good use. I had thought you just wanted to ignore the error.
    Quote Originally Posted by keyboard1333 View Post
    Really? I thought you meant what I was doing was a bad thing to be doing.
    Originally, I did think that, but then you explained that you simply want to handle the error yourself, in a different way than PHP does. That's a perfectly appropriate reason to use @.
    By and large, @ is simply used to hide errors, which is not a good idea.

  2. #12
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Quote Originally Posted by traq
    I made an oversight. function sqli_connect( $username,$password,$link=null ){ ... should be function sqli_connect( $username,$password,&$link=null ){ .... I'm very sorry for the confusion. That & passes the variable $link by reference: instead of copying the value into the function's scope (as normally happens), it literally points at the same value, so anything you do to $link inside the function happens to $link outside the function as well.
    Ah, now that makes sense. I thought I was missing something before. This still isn't something I do often, but it would work. I'd note that I do think this varies by PHP version (even 4 vs. 5 vs. 6, I think they behave differently in subtle ways, but I can't recall for sure).
    Can you, as in your example, create a variable at the same time as using it by reference in a function? Or does it need to already exist in the current scope before you do that?

    Agreed. The potential to confuse scope - function-within-function (or class) - is the main reason I avoid it.
    Definitely. But I don't mind too much when I know I'm explicitly referring to truly global things (like system settings). I never use it to "refer to the thing I was just working with outside the function" because it might be within another non-global scope, true.
    [Well, maybe not never-- for a lazy/simple script with only one level of functions or just to try out some code, I might do that. But I avoid it in anything complicated/important.]




    Just for convenience, because these threads are related, I'll add a link here in case we want to refer back to both in a while (or for anyone who comes across this in the future):
    http://www.dynamicdrive.com/forums/s...ession-be-used
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    Can you, as in your example, create a variable at the same time as using it by reference in a function? Or does it need to already exist in the current scope before you do that?
    Yes; works just fine. No errors, warnings, nothing.

    Also, this should work everywhere (although I never mess around with PHP 4 anymore). Sometime - early in PHP 5, *I think* - something called "call time pass by reference" was deprecated and stopped working. That's when you use & with the argument (or return value) instead of in the function definition:
    PHP Code:
    some_function$a ){ return $a; }

    $arg 'value';

    // bad! doesn't work
    some_function( &$arg );

    // bad! doesn't work
    $return =& some_function$arg );

    reference_function( &$a ){ return &$a; }

    // $arg passed in by reference
    reference_function$arg );

    // return value assigned by reference
    // this is actually *exactly* the same as doing $return = 'value'; but wastes much more time doing it
    $return reference_function'value' ); 
    Basically, in recent versions of PHP, you can define functions that accept/return by reference, but you can't force a function to accept/return by reference if it wasn't designed to.

  4. The Following User Says Thank You to traq For This Useful Post:

    djr33 (05-17-2013)

  5. #14
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    ...something called "call time pass by reference" was deprecated and stopped working. That's when you use & with the argument (or return value) instead of in the function definition:
    ...
    Basically, in recent versions of PHP, you can define functions that accept/return by reference, but you can't force a function to accept/return by reference if it wasn't designed to.
    Ah, yes. That's exactly what I was thinking of. Ok, so it can't be something you do in the function call, but in the function definition. That means you'll always need to use it like that.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Similar Threads

  1. Calling a function in Iframe from main page help please!
    By shellymabelly83 in forum JavaScript
    Replies: 4
    Last Post: 12-14-2011, 01:16 AM
  2. AJAX TABS accessing ajax script with field variable
    By euro121253 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 09-29-2009, 05:55 PM
  3. Replies: 1
    Last Post: 06-19-2009, 01:53 AM
  4. Accessing A javascript variable in HTML
    By rehanazher in forum JavaScript
    Replies: 1
    Last Post: 03-16-2009, 03:18 AM
  5. Replies: 2
    Last Post: 09-06-2006, 05:11 AM

Bookmarks

Posting Permissions

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