Hi all,
If you already look in some of ali (or others, but i'm sure ali does it), you will see they use function. A function in php is declared like this function myFunction($args1, $args2){ // some code echo $args1; echo $args2; } where argument are optionnal.
to use it simply call it with appropiate arguments
// some php code before and after myFunction('toto', 'titi'); this will print tototiti.
Use function make your code easier to read and prone for reutilisability. The counterpart, argument are only in read access inside the function. Sometime we want them to be modify. Function couldn't access var that were declared (by a first affectation) outside their body but PHP have a solution, let the function return something.
function addOneAndReturn($arg){ $arg+=1; return $arg; // this make the difference }
and use it, we need to affect the result to a variable
$result= addOneAndReturn(5); echo $result; // will print 6, believe me.
------------- Oh, this is great but i want more args to be modified. Humm, this is easy, just a scope problem. Maybe you want to tell your function that a variable is already declared outside his body and it should access it. This is done by adding global $var directive on first line in the body.
| CODE | <?php function modifyAllInOneShot($password){ global $name, $age; // each vars are separated with a comma global $city; // you could have as many global directive you want; // don't know what to do so just affect 'me' $password= $login= $name= $age = $city = 'me'; echo 'inside function:<br>'; echo 'name=' . $name . "<br>"; // for thos who don't know, dot simbol act the same way + symbol act in swish for string var, it concatenate echo 'age=' . $age . "<br>"; echo 'city=' . $city . "<br>"; echo 'login=' . $login . "<br>"; echo 'pass=' . $password . "<br>"; }
$name='seb'; $age='11'; $city='paris'; $login='cortex'; $pass='word'; echo 'before function:<br>'; echo 'name=' . $name . "<br>"; // for thos who don't know, dot simbol act the same way + symbol act in swish for string var, it concatenate echo 'age=' . $age . "<br>"; echo 'city=' . $city . "<br>"; echo 'login=' . $login . "<br>"; echo 'pass=' . $pass . "<br>";
modifyAllInOneShot($pass); echo 'after function:<br>'; echo 'name=' . $name . "<br>"; // for thos who don't know, dot simbol act the same way + symbol act in swish for string var, it concatenate echo 'age=' . $age . "<br>"; echo 'city=' . $city . "<br>"; echo 'login=' . $login . "<br>"; echo 'pass=' . $pass . "<br>"; ?> |
this will print
| CODE | before function: name=seb age=11 city=paris login=cortex pass=word inside function: name=me age=me city=me login=me pass=me after function: name=me age=me city=me login=cortex pass=word |
cause i forgot global $login and arguments are not affected outside the function. Notice that two variable named login exist at the same time, global scope, function scope.
last one to avoid problem:
| CODE | function modifyAllInOneShotForLogin($pass){ global $login; modifyAllInOneShot($pass); } |
and call this one at the place of the other don't made login change cause each function must do a global directive even if it's called from one that does it. Making a global on a var that doesn't exist outside, will create it outside.
| CODE | function test(){ global $test; $test = 'me'; } // call test echo '-' . $test .'-<br>'; test(); echo '-' . $test . '-<br>'; |
will print -- -me-
------- EXPERT ONLY ----------- For those who knows C language, there is in php a & operator that can be use in function signature. if i take the example above making the function signature like this function modifyAllInOneShot(&$password) will made $password to be changed. Use this with caution cause it could lead to misundertanding code. -----------------------------------
Sébastien
|