JREAM Posted August 5, 2008 Share Posted August 5, 2008 Heya, i guess globals are bad i read a lot, I got this: $a=1; $b=2; function x(){} function y(){} function z(){} how can I make $a available in function x-z? global $a = 1; // is this bad? what about define('var_a', '1'); // is this bad? Any tips =) ?? I dunno if you can local_scope function, or if that would be an option. How do most people do this? Link to comment https://forums.phpfreaks.com/topic/118218-solved-local-variables-in-fucntions/ Share on other sites More sharing options...
ratcateme Posted August 5, 2008 Share Posted August 5, 2008 you can write this into your functions this is how i do it so only the functions you want have access to it function x(){ global $a; } Scott. Link to comment https://forums.phpfreaks.com/topic/118218-solved-local-variables-in-fucntions/#findComment-608394 Share on other sites More sharing options...
php_dave Posted August 5, 2008 Share Posted August 5, 2008 Not sure why globals are bad but if you want to avoid them why not pass the value to the function? $a = 2; function_x($a); function definition function_x($a) { $local_a = $a; } Link to comment https://forums.phpfreaks.com/topic/118218-solved-local-variables-in-fucntions/#findComment-608409 Share on other sites More sharing options...
BioBob Posted August 5, 2008 Share Posted August 5, 2008 You could pass by REFERENCE, not VALUE also... The & character allows you to pass an argument to a function by its reference (or where it is stored in memory on the servers ram) rather than a COPY of its value. <?php function today($arg) { $day = "Tuesday"; echo "It is $day inside the function"; //should see Tuesday } $day = "Monday"; today($day); echo "It is $day out side the function"; //should see Monday ?> $day in the function is not the same as $day inside the function. GLOBAL makes them the same but has some drawbacks. <?php function today(&$arg) { $day = "Tuesday"; echo "It is $day in the function"; //displays Tuesday } $day = "Monday"; today($day); echo "It is $day out side the function"; //also displays Tuesday ?> In the 2nd piece of code, when $day was passed as an argument to the function, it was passed by its reference so any changes to the reference change its value whenever it is called. When its not passed by reference, new memory space is allocated for $day inside the function so it becomes a copy of $day, not the original $day. THink of it like a copier machine. Photocopy a form, then fill it out. The original doesnt change. Probably read up on it here: http://devzone.zend.com/node/view/id/637 under "Checking References" Link to comment https://forums.phpfreaks.com/topic/118218-solved-local-variables-in-fucntions/#findComment-608459 Share on other sites More sharing options...
JREAM Posted August 6, 2008 Author Share Posted August 6, 2008 cool thanks for all the good idas I have fun playing in them Link to comment https://forums.phpfreaks.com/topic/118218-solved-local-variables-in-fucntions/#findComment-609234 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.