Page 1 of 1

PHP functions

Posted: Wed Nov 24, 2004 9:27 am
by Archy
I have made a function that as a string passes through it, it get's manipulated to the way it is supposed to look. However, when I try and write at the end of the script...

Code: Select all

echo"$priceIn";
$newVariable = $priceIn;
I find that it does not work outside of the function. Do I have to make it a global variable or something, or is there something that I am missing, or, are variables created in functions only available within those functions.

Thanks,
Archy

Can you post more code...

Posted: Wed Nov 24, 2004 9:41 am
by neophyte
Can you post more code? Post your function.

Posted: Wed Nov 24, 2004 9:46 am
by phpScott
you will want to check out
http://uk.php.net/manual/en/language.va ... .scope.php
as your problem has to do with variable scope.

Posted: Wed Nov 24, 2004 10:28 am
by Archy
Thanks, I will have a go with global variables; I thought it might be something like that : )

Posted: Wed Nov 24, 2004 4:49 pm
by Archy
Hmm, the global Variables work ok, but it is not ideal, is there any way for me to do it like this:

Code: Select all

$variable = function($otherVariable);
It's a long shot, but who knows :P

Posted: Wed Nov 24, 2004 4:51 pm
by rehfeld

Code: Select all

function add_foo($text) {
    $new_text = $text . 'foo';
    return $new_text;
}


$text = 'some text';

echo add_foo($text); // some textfoo

Posted: Wed Nov 24, 2004 5:05 pm
by neophyte
Archy are you a flash coder by chance?

Posted: Wed Nov 24, 2004 5:14 pm
by Archy
rehfeld, I dont want to echo it out straight away, I want to assign it to a variable, so I can do other things with it, and then echo it out. Do you know if that is possible, and if so how?

In my Java programming history, return $new_text; would simply print the variable, not sure if it's the same with PHP though.

neophyte, no, not at the moment anyways ;)

Posted: Wed Nov 24, 2004 5:30 pm
by rehfeld
$var = add_foo($text);

Posted: Wed Nov 24, 2004 5:55 pm
by Archy
Ah yes, thank you : )