PHP functions

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

Post Reply
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

PHP functions

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Can you post more code...

Post by neophyte »

Can you post more code? Post your function.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

Thanks, I will have a go with global variables; I thought it might be something like that : )
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Archy are you a flash coder by chance?
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post 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 ;)
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

$var = add_foo($text);
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

Ah yes, thank you : )
Post Reply