Jump to content

help with functions with multiple variables


jeff5656

Recommended Posts

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?

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)?

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.

 

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()...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.