Function echo/print problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

r3negade
Forum Newbie
Posts: 4
Joined: Mon Nov 22, 2010 3:27 pm

Function echo/print problem

Post 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!
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Function echo/print problem

Post 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());
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Function echo/print problem

Post 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... :)
r3negade
Forum Newbie
Posts: 4
Joined: Mon Nov 22, 2010 3:27 pm

Re: Function echo/print problem

Post 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:

Code: Select all

50e207ab6946b5d78b377ae0144b9e07
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Function echo/print problem

Post 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;

Code: Select all

$bar = md5("");
An md5 of nothing. For this to work you need to do;

Code: Select all

<?php
    function foo() {
        return "hellothere";
    }
	
	$bar = md5(foo());
?>
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Function echo/print problem

Post 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;
?>
Last edited by phphelpme on Mon Nov 22, 2010 5:21 pm, edited 3 times in total.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Function echo/print problem

Post 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:

Code: Select all

50e207ab6946b5d78b377ae0144b9e07
You are hashing nothing too.

Essentially performing;

Code: Select all

$bar = md5("");
What are you trying to do?
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Function echo/print problem

Post by phphelpme »

thanks Neilos, forgot to put that in there. cheers for that pal. :)
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Function echo/print problem

Post by Neilos »

phphelpme wrote:thanks Neilos, forgot to put that in there. cheers for that pal. :)
:wink:
r3negade
Forum Newbie
Posts: 4
Joined: Mon Nov 22, 2010 3:27 pm

Re: Function echo/print problem

Post by r3negade »

Neilos wrote: You are hashing nothing too.

Essentially performing;

Code: Select all

$bar = md5("");
What are you trying to do?
If I'm hashing nothing then how can "echo $bar;" produce "50e207ab6946b5d78b377ae0144b9e07"?
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Function echo/print problem

Post 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
Last edited by phphelpme on Mon Nov 22, 2010 5:08 pm, edited 1 time in total.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Function echo/print problem

Post by Neilos »

r3negade wrote:
Neilos wrote: You are hashing nothing too.

Essentially performing;

Code: Select all

$bar = md5("");
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;

?>
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Function echo/print problem

Post 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. :wink:
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Function echo/print problem

Post 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?
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Function echo/print problem

Post 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;
?>
Post Reply