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.. function myfunc($dbconnect){ $test="test1" return $test; } echo here - echo $test; - doesn't work myfunc($test); PHP:
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.
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
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?
I would learn the basics before starting to coding: http://php.net/manual/en/functions.user-defined.php http://php.net/manual/en/language.variables.scope.php http://php.net/manual/en/language.variables.php
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.
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
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.
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.
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.
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.
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...
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