PDA

View Full Version : Using an argument as variable (PHP)



Orange Kingdom
June 4th, 2011, 01:41 AM
Hi,

Is it possible to use an argument in a function as a variable for the rest of the script?

function test ($a,$b) {

//do something

}
I want to use $a or $b as a variable outside of the function.

Reiger
June 4th, 2011, 02:26 AM
If you mean references that is possible and no you shouldn't do that (explicitly) because that road ends where limbs get sacrificed on the altar of bad code.

Instead look into classes and objects with PHP5: http://php.net/manual/en/language.oop5.php

When you grasp how they work, you will see the answer to your problem right there as well.

Petrolea
June 4th, 2011, 10:42 AM
Hi,

Is it possible to use an argument in a function as a variable for the rest of the script?

function test ($a,$b) {

//do something

}
I want to use $a or $b as a variable outside of the function.

Maybe you mean global variables. They can be accessed everywhere.

simeon87
June 4th, 2011, 11:36 AM
You could call test, return a value and then use it elsewhere. But using arguments outside a function (not exactly possible) is asking for messy code.

Orange Kingdom
June 4th, 2011, 12:21 PM
Ok, thanks. I already thought it's not good programming practice.

So i have to look in the OOP, which i did but why do it difficult i thought. :)

Petrolea
June 4th, 2011, 01:59 PM
Ok, thanks. I already thought it's not good programming practice.

So i have to look in the OOP, which i did but why do it difficult i thought. :)

If you want to change value of some variable with function, you can check out SESSION variables. Create one than change its value with functions.



...
$_SESSION['something'] = 1;
...
function plus_one()
{
$_SESSION['something'] += 1;
}
...
//you can access it from anywhere, even other files
...
plus_one();
echo $_SESSION['something'];