1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

echo out a value from outside of a function in php

Discussion in 'PHP' started by xbat, Feb 4, 2018.

  1. #1
    I can't seem to figure this one out. I have spent hours trying to get this to work and no such luck. Basic idea..
    SEMrush

    function myfunc($dbconnect){
    
    
    $test="test1"
    
    return $test;
    
    }
    
    
    
    echo here -
    
    echo $test; - doesn't work
    
    myfunc($test);
    PHP:
     
    xbat, Feb 4, 2018 IP
    SEMrush
  2. malky66

    malky66 Acclaimed Member

    Messages:
    3,661
    Likes Received:
    2,070
    Best Answers:
    84
    Trophy Points:
    515
    #2
    Like this?

    function myfunc($dbconnect){
    
    
    $test="test1";
    
    return $test;
    
    }
    
    echo myfunc($test);
    PHP:
     
    malky66, Feb 4, 2018 IP
  3. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #3
    When function returns some value, you need to echo it if you want the value to be displayed. Otherwise, it's just a returned value (which you can use in other functions, etc.).

    So your options are:
    echo myfunc($test);
    Code (markup):
    or

    $var=myfunc($test);
    echo $var;
    Code (markup):
    The first one is quicker, but there will be many situations when you will need to use second one.
     
    phpmillion, Feb 4, 2018 IP
  4. xbat

    xbat Active Member

    Messages:
    318
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    55
    #4




    if i do
    echo myfunc($test);
    Code (markup):
    or
    $var=myfunc($test);
    echo $var;
    Code (markup):
    I get this error then - Fatal error: Call to a member function prepare() on a non-object in

    I tried all different ways but i always get some strange error :(
     
    xbat, Feb 4, 2018 IP
  5. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #5
    You get this error because you did something incorrectly before calling this function.

    Also, what's the purpose of $dbconnect as your function's parameter if you do nothing with it?
     
    phpmillion, Feb 5, 2018 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
  7. xbat

    xbat Active Member

    Messages:
    318
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    55
    #7

    I do apologize. I am doing stuff with it. They are connecting to a database - A select prepared select statement. I did that leave that code out. Its just a simple select query.
     
    xbat, Feb 5, 2018 IP
  8. xbat

    xbat Active Member

    Messages:
    318
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    55
    #8
    haha thats a funny answer. Thank you for your effort but that really doesn't solve anything.
     
    xbat, Feb 5, 2018 IP
  9. xbat

    xbat Active Member

    Messages:
    318
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    55
    #9
    if it helps any I was just looking for a quick answer to fix some old code. Thought maybe I was missing something. But as I have spent more and more time on it looks like I am going to have rebuild some stuff no matter what. haha I spent way to much time on already. You use to be able to use the & sign and bam you could pass it that way. But I just do not believe this there is a really simple way to go about because of the php update 5.3 to 5.4. Anyways thanks for everyones help.


    This well give you a long answer. haha I hate to "thephpmaster" everyone http://php.net/manual/en/language.references.pass.php
     
    Last edited: Feb 5, 2018
    xbat, Feb 5, 2018 IP
  10. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #10
    This is not working because $test is a local variable for myfunc.

    
    $test=""
    function myfunc($dbconnect){
        $test="test1";
    }
    echo $test; this will work.
    
    PHP:
    
    function myfunc($dbconnect){
        echo "test1";
    }
    myfunc($dbconnect);
    This will work.
    
    PHP:
    Being at your level, I would refrain using references.
     
    Blizzardofozz, Feb 20, 2018 IP
  11. xbat

    xbat Active Member

    Messages:
    318
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    55
    #11
    Haha and what is my level? haha are you spying on me? J/k I appreciate your passion and the effort but I think you misunderstood part of my question. I do apologize if my English isn't the best I was born partially deaf and still am trying to improve my english today. If helps any in a perfect world everything is done perfectly and people work with each but its not that world so sometimes you run into code like this or other crazy stuff and I just try to find a quick solution or better solution. Sometimes people want quickness and there is a good reason for that and I understand that. Anyways I wish you the best. I always give a A for effort even if it is a bit negative. :)
     
    xbat, Feb 20, 2018 IP
  12. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #12
    Sorry if you have felt offended but you are trying to:
    echo $test; - doesn't work
    where $test is local to the function!

    This is a very basic mistake.
     
    Blizzardofozz, Feb 20, 2018 IP
  13. xbat

    xbat Active Member

    Messages:
    318
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    55
    #13
    haha no worries I am just not a fan of (I would refrain using references) no worries brah :) I believe you misunderstood my question of how to pass the parameters through the function and out of etc.. Its all good now I just rebuilt most of the code I thought maybe someone would have a quick easy solution that I was missing.

    I could have explained it better I do admit that now and do apologize for it. I should have used the words passing references and added a lot more to my question. I absolutely am slapping myself for that now. I just didn't want to dump a ton more code to make it more confusing - but this sums it up a lot more - http://php.net/manual/en/language.references.pass.php Because of the version the php and some other stuff that's what made the question arise in the first place and overall fix.
     
    Last edited: Feb 20, 2018
    xbat, Feb 20, 2018 IP
  14. JEET

    JEET Notable Member

    Messages:
    2,611
    Likes Received:
    188
    Best Answers:
    4
    Trophy Points:
    215
    #14
    The solution was the very first one that was posted...

    function myfunc($dbconnect){
    $test="test1"
    return $test;
    }

    echo myfunc($dbconnect);

    You were getting a fatal error because you were trying to do this:
    echo myfunc($test);

    $test is empty/undefined at this time...
    So when you passed $test instead of $dbconnect,
    the rest of the code that was using $dbconnect failed...
    hence the fatal error...

    You've already recoded most of your work, so this is pretty much useless for now,
    but use this in future sometime may be...
     
    JEET, Mar 30, 2018 IP
  15. xbat

    xbat Active Member

    Messages:
    318
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    55
    #15

    Thank you that didn't work so much. But I really do appreicate the effort. I did end up figuring out a few ways. A rebuilding alot of stuff or globals in the end. haha yes I get alot of peoples messes but I am glad to do it. thank you again
     
    xbat, Mar 30, 2018 IP
    JEET likes this.