Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2015
    Posts
    47
    Thanks
    15
    Thanked 1 Time in 1 Post

    Variable in Function Scope

    I know there is probably an easily findable answer, but I'm not finding one in my searching.
    Seems like below should return"foobar" for res, but I get nothing in return.
    Code:
    <?
    $res="";
    $str="foo";
    //
    	function go($str)
    	{
    	$res=$str.='bar';
    	return $res;
    	}
    //
    go($str);
    print ($res);
    ?>
    It's been a long time since I visited here and my inactivity is catching up with me.

  2. #2
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,596
    Thanks
    10
    Thanked 162 Times in 157 Posts
    Here you go...

    PHP Code:
    <?php
    function go($str)
    {
        
    $res $str .= 'bar';
        return 
    $res;
    }
    $str "foo";
    echo 
    go($str);
    Last edited by benanamen; May 18th, 2019 at 06:11 AM.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    Make A Donation https://www.paypal.me/KevinRubio

  3. Users who have thanked benanamen for this post:

    jimandy (May 18th, 2019)

  4. #3
    Senior Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    1,130
    Thanks
    205
    Thanked 141 Times in 141 Posts
    @jimandy

    why not just keep it simple and use concat?

    Do you need to return?


    PHP Code:
    function go($str)
    {
        echo 
    $str ' bar';
    }

    $str "foo";

    go($str); 

    Review the shorthand concatenating assignment operator HERE
    0000

  5. Users who have thanked low tech for this post:

    jimandy (May 19th, 2019)

  6. #4
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,596
    Thanks
    10
    Thanked 162 Times in 157 Posts
    @low tech, what are you going to do when you want to use the value instead of echo it? The function should return the value.

  7. #5
    New Coder
    Join Date
    Feb 2015
    Posts
    47
    Thanks
    15
    Thanked 1 Time in 1 Post
    OK, so what I am doing is trying to convert, line by line, a working JS script which uses a more complicated function (but does not use a concat) The concat was used in my example just to prove if the return line was functioning. Actually I found one solution was to add one line "global $str;" as first in the function block. Then I don't even need the"return". My lack of understanding about scope was confusing the situation. But as always I have learned even more by returning to this forum.
    THANKS

  8. #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,264
    Thanks
    3
    Thanked 554 Times in 539 Posts
    Don't use global. It creates spaghetti code/variables that makes writing code (you must now keep straight all the variable names used everywhere) and finding problems harder. A function should return its result to the calling code. See this link - https://en.wikipedia.org/wiki/Black_box
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  9. Users who have thanked CFMaBiSmAd for this post:

    jimandy (May 19th, 2019)

  10. #7
    New Coder
    Join Date
    Feb 2015
    Posts
    47
    Thanks
    15
    Thanked 1 Time in 1 Post
    Good advice.
    I had forgotten the term "spaghetti code" which I learned was what you got when programming in "BASIC" on an Apple IIe.

  11. #8
    New to the CF scene
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    Solution 1: You can access the globals variables inside the function. (This is a part of variable scope)
    Code:
    <?
    $res="";
    $str="foo";
    function go() {
       global $res, $str;
       $res = $str . 'bar';
    }
    go();
    print ($res); // foo bar
    ?>
    Solution 2: Parse variables into the function and echo the return.

    Code:
    <?
    $res="";
    $str="foo";
    function go($str) {
       return $str . 'bar';
    }
    $str = go($str);
    print ($str); // foo bar
    ?>

  12. Users who have thanked SupunKavinda for this post:

    jimandy (May 20th, 2019)


 

Tags for this Thread

Posting Permissions

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