Lassie Posted January 12, 2019 Share Posted January 12, 2019 I have a function that needs to return a value and it isn’t.I think it’s a scope issue but can’t solve it. The function is function is_connected() { $addr= 'www.google.com'; if (!$socket = @fsockopen($addr, 80, $num, $error, 5)){ $connection = 0; }else { $connection = 1; { return $connection; } is_connected() echo $connection; The function works, but I do get a warning $connection not defined and I don’t get a value. Any help appreciated. Quote Link to comment Share on other sites More sharing options...
maxxd Posted January 12, 2019 Share Posted January 12, 2019 You're not assigning the returned value from is_connected() to $connection. You're just calling the function and expecting that $connection has been created elsewhere. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted January 12, 2019 Share Posted January 12, 2019 echo is_connected(); Quote Link to comment Share on other sites More sharing options...
Lassie Posted January 12, 2019 Author Share Posted January 12, 2019 Could you expand on your comments please. If on calling the function I do this: is is_connected($connection) //and funtion is_connected($connection) I still don’t get a value. ? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 12, 2019 Share Posted January 12, 2019 (edited) 1 . Get rid of the @ sign. Proper coding and handling of any errors is way better than suppressing the error. You shouldn't have an error that you don't handle. 2 - To get a return value you have to CATCH it. You do that by: $result = is_connected(); Note: you don't need the argument of $connection in the function header. Although - you may need something to define/handle $num and $error. Edited January 12, 2019 by ginerjm Quote Link to comment Share on other sites More sharing options...
gw1500se Posted January 12, 2019 Share Posted January 12, 2019 My code was just to get an output to print that represents the return value. I was guessing you had that for debugging purposes. You don't need to pass $connection. If you want the return value in a variable to use outside the function the use ginerjm's suggestion and print/use $result. Quote Link to comment Share on other sites More sharing options...
maxxd Posted January 12, 2019 Share Posted January 12, 2019 Right. You assign the return value of the function to a variable. A more explicit version of ginerjm's example: $connection = is_connected(); echo $connection; See the difference in what you typed and your second block of code in your first post? Quote Link to comment Share on other sites More sharing options...
Lassie Posted January 13, 2019 Author Share Posted January 13, 2019 Thanks guys, I now have it working as required. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.