Page 1 of 2
Function echo/print problem
Posted: Mon Nov 22, 2010 3:56 pm
by r3negade
I have this code:
Code: Select all
<?
function foo () {
echo "hello";
echo "there";
}
$bar = md5(foo());
?>
This also outputs "hellothere" instead of just setting $bar to the md5 value of the output of foo();. What can I do to fix this problem? =/
Also I can't put all the functions output into one variable which I then return, because I'm using a lot of printf() in my real function.
Thank you in advance!
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 4:18 pm
by Neilos
I'm not sure quite what your problem is but...
Try;
Code: Select all
function foo () {
$string = "hello ";
$string .= "there.";
return $string;
}
$bar = md5(foo());
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 4:20 pm
by phphelpme
Why not assign your values as a string for the function foo. Your coding is assigning foo = hellothere then you want $bar to be an MD5 of foo.
I ran your short script on my servers and it does not echo the values for me but this might be a way around it for you anyway.
Code: Select all
<?php
function foo() {
$bar = "hellothere";
}
$bar = md5(foo());
?>
But if you say you cant do this because of your printf() real function then please supply the real printf() function coding...

Re: Function echo/print problem
Posted: Mon Nov 22, 2010 4:36 pm
by r3negade
I'm sorry if I were too vague. My original function is huge, but this is more similar to the kind of stuff I have (...and I need to keep that printf stuff).
Code: Select all
<?
function foo() {
$apples = 36;
$trees = 5;
printf('There are %2$s apples in the %1$s trees with %s monkeys and %s bananas.<br />', $apples, $trees);
$bees = 234;
$dogs = 2;
printf("There are %d bees, and %d dogs.", $bees, $dogs);
}
$bar = md5(foo());
echo $bar;
?>
It outputs:
Code: Select all
There are 5 apples in the 36 trees with 36 monkeys and 5 bananas.
There are 234 bees, and 2 dogs.50e207ab6946b5d78b377ae0144b9e07
...but I want this:
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 4:41 pm
by Neilos
phphelpme wrote:
Code: Select all
<?php
function foo() {
$bar = "hellothere";
}
$bar = md5(foo());
?>
)
This doesn't work, you aren't returning anything in foo(), basically you are performing this;
An md5 of nothing. For this to work you need to do;
Code: Select all
<?php
function foo() {
return "hellothere";
}
$bar = md5(foo());
?>
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 4:51 pm
by phphelpme
What about adding a hidden field to your printf statement?
Code: Select all
<?
function foo() {
$apples = 36;
$trees = 5;
printf('<input type='hidden' value='There are %2$s apples in the %1$s trees with %s monkeys and %s bananas.'></><br />', $apples, $trees);
$bees = 234;
$dogs = 2;
printf('<input type='hidden' value='There are %d bees, and %d dogs.'></>', $bees, $dogs);
}
$bar = md5(foo());
echo $bar;
?>
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 4:53 pm
by Neilos
r3negade wrote:I'm sorry if I were too vague. My original function is huge, but this is more similar to the kind of stuff I have (...and I need to keep that printf stuff).
Code: Select all
<?
function foo() {
$apples = 36;
$trees = 5;
printf('There are %2$s apples in the %1$s trees with %s monkeys and %s bananas.<br />', $apples, $trees);
$bees = 234;
$dogs = 2;
printf("There are %d bees, and %d dogs.", $bees, $dogs);
}
$bar = md5(foo());
echo $bar;
?>
It outputs:
Code: Select all
There are 5 apples in the 36 trees with 36 monkeys and 5 bananas.
There are 234 bees, and 2 dogs.50e207ab6946b5d78b377ae0144b9e07
...but I want this:
You are hashing nothing too.
Essentially performing;
What are you trying to do?
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 4:57 pm
by phphelpme
thanks Neilos, forgot to put that in there. cheers for that pal.

Re: Function echo/print problem
Posted: Mon Nov 22, 2010 4:59 pm
by Neilos
phphelpme wrote:thanks Neilos, forgot to put that in there. cheers for that pal.


Re: Function echo/print problem
Posted: Mon Nov 22, 2010 5:04 pm
by r3negade
Neilos wrote:
You are hashing nothing too.
Essentially performing;
What are you trying to do?
If I'm hashing nothing then how can "echo $bar;" produce "50e207ab6946b5d78b377ae0144b9e07"?
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 5:08 pm
by phphelpme
I made a mistake on my coding idea so he was correcting my coding not yours matey... lol

i missed the return function from your foo function mate... lol
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 5:08 pm
by Neilos
r3negade wrote:Neilos wrote:
You are hashing nothing too.
Essentially performing;
What are you trying to do?
If I'm hashing nothing then how can "echo $bar;" produce "50e207ab6946b5d78b377ae0144b9e07"?
Run this code and then you'll be able to answer your own question;
Code: Select all
<?
function foo() {
$apples = 36;
$trees = 5;
printf('<input type='hidden'>There are %2$s apples in the %1$s trees with %s monkeys and %s bananas.</><br />', $apples, $trees);
$bees = 234;
$dogs = 2;
printf('<input type='hidden'>There are %d bees, and %d dogs.</>', $bees, $dogs);
}
$bar = md5(foo());
echo $bar;
echo "<br />";
$bar2 = md5("");
echo $bar2;
?>
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 5:09 pm
by Neilos
phphelpme wrote:I made a mistake on my coding idea so he was correcting my coding not yours matey... lol

I was correcting both of you, you both had the same mistake.

Re: Function echo/print problem
Posted: Mon Nov 22, 2010 5:13 pm
by phphelpme
yes, sorry again... lol thats right. we both missed out the return function for your function foo pal... But his first code was an example only not the actual code he was using. So i asked for an example of his printf function coding to see what this was all about.
I think the hidden field should do the job wont it? what you think?
Re: Function echo/print problem
Posted: Mon Nov 22, 2010 5:13 pm
by Neilos
Pardon me, this code I mean;
Code: Select all
<?php
function foo() {
$apples = 36;
$trees = 5;
printf('There are %2$s apples in the %1$s trees with %s monkeys and %s bananas.<br />', $apples, $trees);
$bees = 234;
$dogs = 2;
printf("There are %d bees, and %d dogs.", $bees, $dogs);
}
$bar = md5(foo());
echo $bar;
echo "<br />";
$bar2 = md5("");
echo $bar2;
?>