Calling a function inside of a string?

A

Anonymous

Guest
Hi guys and gals...I'm relatively new to this PHP stuff, so bear with me...:)

Anyway, here's the question: Is it possible to call a function inside of a string?

Here's the scenario: I created a function named "list_products_and_quantities()", which I wanted to call upon at a certain point inside the body of a PHP-server-generated email.....I set the function equal to a variable $list, and attempted to display $list (and subsequently execute "list_products_and_quantities()") inside the email message string....I know you can display values of variables inside strings, so I was hoping that if I set the value of $list equal to a function, then $list would call the associated function...but it's not working....any ideas? Maybe there is another way to do this besides employing a function inside of a string?

If you're interested, here is the portion of the code you need to be concerned with:


<?php

function list_products_and_quantities()
{
for ($n=1; $n<=3; $n++)
if (!trim($product_code[n]))
$n = $n;
else
echo "

- $quantity[n] blue widget(s) (product code: $product_code[n])";
}

$list = list_products_and_quantities();

mail($mail_to,
$subject,
"
Thank you, $first_name, for your recent purchase from Media Moguls.

Our records indicate that you made the following purchase(s): $list

\$000.00 will be billed to your $payment_method credit card.

We have the following billing information for you:

Address: $address
Suite #: $suite
City: $city
State: $state
ZIP: $zip

We have the following shipping information for you:

Address: $address2
Suite #: $suite2
City: $city2
State: $state2
ZIP: $zip2

If any of this information is incorrect, please contact us immediately via the Media Moguls web site. Do not reply to this confirmation email. Replies will not be answered.

We recommend that you save or print this confirmation email for your records. This email functions as your receipt of purchase.

Thank you for shopping with Media Moguls. Please visit us again!


\t - The Media Moguls team");

?>
 
function list_products_and_quantities()
{
for ($n=1; $n<=3; $n++)
if (!trim($product_code[n]))
$n = $n;
else
$return="- $quantity[n] blue widget(s) (product code: $product_code[n])";
return $return ;
}
NOW it will work... your finction will need to retun a value if you want to assign it to the varialbe otherwise just use the function without variable assignment right into the string..
 
Thank you very much for your help.... :D

I revised the function as you had it, including the return statement and such (I should have seen that, Duh!)...

I tried calling the function by name (list_products_and_quantities()) inside the email string, and all that did was display the name of the function inside of the email....Then, I assigned the function to the variable $list and inserted $list inside of the email string....That didn't work either: This time the output was just a blank line....Uh oh....

One thing that might help you to know that I didn't state in my initial message: Some elements of $product_code[] and $quantity[] may not return values, and that is as it should be....The whole point of creating and calling the function is to iterate through the array elements and ONLY display those elements that HAVE values, in the context of the string assigned to $return....

I'm at a loss....The only other thing I can think of is that I'm using the variable $product_code[] inside of the function ($product_code[] appears in other areas of the code, outside of the function)....Maybe I should specify that $product code[] is a global variable?

I included this statement immediately before the function code to verify this:

global $product_code[1], $product_code[2], $product_code[3], $quantity[1], $quantity[2], $quantity[3];

Now I get an error message for this line reading:

Parse error: parse error, expecting `','' or `';''

Again, I'm at a loss....I'd appreciate any more insight you could give me on this situation....Thanx...
 
You can apply the global statement to a variable or set of variables, not to individual elements of an array. This should take care of whatever you're trying to do:

Code:
<?php
global $product_code, $quantity;
?>

By the way, it kind of concerns me that none of your array indices are 0. In PHP (and in fact all programming languages) it's customary for the first array index to be 0, the next 1, the next 2, and so on.
 
I changed the global statement to the way you had it, and adjusted the indices of the array elements to start at "0" instead of "1".

Nevertheless, the resulting email is still displaying the name of the function instead of executing the function.

Sigh....Thanks for the help, though....

R.J.
 
wait a second...

how about this type of syntax:

"Text part 1".function_that_retuns_a_value."second part of the text"

??? that usually works fine... note that its best that your function returns a value... or use a variable like you did before... but the function must return something...
 
I'll try the concatenation schtick, but I seem to remember trying that before...maybe it will work now with the other changes....

The other thing: The function should always return the value of $return at least once (I think that's what you are saying?).....I made sure of that via some other PHP code that I used to validate the HTML form fields...

One more thing: Does it make a difference that the value the function is returning is a string? Would the function work if I made it return some random number instead? Maybe I'm reaching here... 8-O

Thanks, R.J.
 
it doesnt really matter... because you should write like this... this will mean least changes for you

function blabla(){
$bla1="annaan";
return $bla1;
}

$bla2=blabla(); // Bla2 is now "annaan"

$msg="Text part 1 ".$bla2." 2nd part"; //$msg is now "Text part 1 annaan 2nd part"

This should help...
 
Well, I just tried it that way, and it seemed like a good idea, but $bla2 (as you call it) didn't display any value in the subsequent email....

#$%#$!

thanx, R.J.
 
Interesting!

I tested $bla2 in an echo statement as you suggested....It still doesn't display a value!

Very odd....now what? :-D
 
function list_products_and_quantities()
{
for ($n=0; $n<=2; $n++)
if (!trim($product_code[n]))
$n = $n;
else
$return="- $quantity[n] blue widget(s) (product code: $product_code[n])";
return $return;
}

$list = list_products_and_quantities();

echo $list;
exit();


This code returns no value for $list? (The echo and exit stament are just in there to allow me to test if $list displays a value.)


- R.J.
 
okej... i kind of dont get what you are trying to do with the code... u trim the array? trim removes only whitespaces from the begining and the end of a string..

but there is one problem... you forgot to put $ in the arrays id -- $product_code[n] should be $product_code[$n] same goes to $quantity[n]

But still... can you explain whay you are trying to trim() an array? maybe you ment to use empty()?
 
Alrighty then...

I substituted $n for n for the array indices appearing in the function....that was a good catch...I should have seen that...

Then I tested the value of $list --- still no value.

I changed !trim() to empty(), and tested the value of $list again --no value displayed....

Arggg....

I've been using !trim() up until now to detect empty form fields (primarily so that I may validate my forms)...I'm not familiar with empty()...does it serve the same purpose?

R.J.
 
trim -- Strip whitespace from the beginning and end of a string
empty -- Determine whether a variable is empty

it would be wise to use empty(trim(STRING))

i will take a look at your function again when i will get to my office..
 
Example. A simple empty() / isset() comparison.

Code:
<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
   echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
   echo '$var is set even though it is empty';
}
?>
 
I really appreciate all your help with this.... :)

So when you say $var is "set", you mean $var has been introduced in the code but has not been assigned a value? In the same vein, isset() evaluates as true when $var has been introduced but has not been assigned a value?

This "for" statement/loop alone does what I want it to do:

for ($n=0; $n<=2; $n++)
{
if (!trim($product_code[$n]))
$n = $n;
else
{
$return="- $quantity[$n] blue widget(s) (product code: $product_code[$n])\n";
echo $return;
}
}

The only thing it doesn't do is recognize the "\n" at the end of the string assigned to $return. I wonder why?

Anyway, if I could turn this "for" statement into a function, I'd be set (I think)....any ideas?

R.J.
 
Code:
function lol($product_code, $quantity){
	for ($n=0; $n<=2; $n++)
	{
		if (!trim($product_code[$n]))
		$n = $n;
		else
		{
			$return.="- $quantity[$n] blue widget(s) (product code: $product_code[$n])\n";
			
		}
	}
	return $return;
}

The thing is that you have to parse product_code array and a quantity array to the function...
 
Let's assume all the elements of the arrays $quantity and $product_code have values (this, in all likelyhood, will happen on occasion).

It look like:

$return will only have one value at the end of the for loop -- the value "- $quantity[2] blue widget(s) (product code: $product_code[2])\n" ...previous assignments to $return, which would include the values of $quantity[1] and $product_code[1], and $quantity[0] and $product_code[0], will have been overwritten by this last assignment to $return....The function would return only one and the SAME value of $return every time you executed it....

That's not a good thing, if I'm right....What do you think?

One more thing, the arguments of your function confuse me: Why are they there?

R.J.
 
Back
Top