jeff5656 Posted March 24, 2012 Share Posted March 24, 2012 I have this function: function echo_exist($pre1,$ee,$post1) { if ($ee!='') { return $pre1; return $ee; return $post1; } else { //do nothing } } But when I try to echo it echo_exist("(","$row['payment_type']",")"); I get syntax error, unexpected T_ENCAPSED_AND_WHITESPACE If $row['payment_type'] exists, I want it to be displayed in parentheses like: (AUTO) I suspect it has to do with my ' characters for the associative array, but I am new with functions so perhaps it is the multiple arguments that are wrong? Quote Link to comment https://forums.phpfreaks.com/topic/259627-help-with-functions-with-multiple-variables/ Share on other sites More sharing options...
KevinM1 Posted March 24, 2012 Share Posted March 24, 2012 1. You can't have multiple return statements like the way you have them. Return returns that value from the function and then EXITS the function. Everything else below the initial returns won't be executed. 2. I'm not sure what you're attempting to do when you try to invoke the function. What's with all the double quotes and extra parenthesese? Where's your echo statement? Have you read about functions in the online manual (functions)? Quote Link to comment https://forums.phpfreaks.com/topic/259627-help-with-functions-with-multiple-variables/#findComment-1330733 Share on other sites More sharing options...
AyKay47 Posted March 24, 2012 Share Posted March 24, 2012 1. passing parenthesis into a function is pointless, as the display can be handled with PHP after the function has returned the necessary data. 2. If you simply want to check for a variable being empty, use the empty function that PHP provides. Quote Link to comment https://forums.phpfreaks.com/topic/259627-help-with-functions-with-multiple-variables/#findComment-1330734 Share on other sites More sharing options...
jeff5656 Posted March 24, 2012 Author Share Posted March 24, 2012 Well, I am constantly typing if ($row['name'])!=''){ echo "the name is ". $row['name']; so I figured I would make a function to save keystrokes. so all I would have to do would type echo_if_exist($row['name']) . But also, sometimes I want to precede the variable with "the name is" and sometimes I want to simply surround it in a parenthesis. I guess my experiment on trying to use a function failed. Oh I just saw that new post - I will look into empty()... Quote Link to comment https://forums.phpfreaks.com/topic/259627-help-with-functions-with-multiple-variables/#findComment-1330735 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.