scooter41 Posted October 3, 2007 Share Posted October 3, 2007 Hi There, I just decided to switch from using Perl/CGI for my web development, and start to code with PHP to speed things up, although I am already at the first hurdle When using functions in perl, I could predefine variables at the top of the script, and then call the function..... example: $sendtoname="example"; $subject="This is the subject to send"; $fromname="This is the from name"; $message="Please checkout this website" $recipient="[email protected]"; sendMail(); $recipient="[email protected]"; sendMail(); echo "send to $counter addressess" function sendMail ( ... sendmail to the recepient, using the details above, such as $message, $fromname $counter++; ) However this appears not to work in PHP.......... it seems as though I have to pass variables around funtions all the time.... unless there is a way to specify global variables? Im only thinking say that I had to pass 20 different variables to the sendMail function, isnt that a very long winded way of doing things? Thanks for any comments in advance. However Link to comment https://forums.phpfreaks.com/topic/71684-coming-from-a-perl-user-global-variables-between-functions/ Share on other sites More sharing options...
MmmVomit Posted October 3, 2007 Share Posted October 3, 2007 You need to tell the function to go grab the global variables. <?php $this_is_global = "I am a global variable"; function foo() { echo $this_is_global; } function bar() { global $this_is_global; // gives the function access to the global variable echo $this_is_global; } ?> bar() will work, but foo() will not. Link to comment https://forums.phpfreaks.com/topic/71684-coming-from-a-perl-user-global-variables-between-functions/#findComment-360878 Share on other sites More sharing options...
shocker-z Posted October 3, 2007 Share Posted October 3, 2007 it starts parsing at the top you need to define functions before they are called also you need global $counter; i would write it like this $counter=0; function sendMail($from,$to) ( global $counter; $counter++; echo 'Counter: '.$counter.' From: '.$from.' To: '.$to.'<Br>'; ) $sendtoname="example"; $subject="This is the subject to send"; $fromname="This is the from name"; $message="Please checkout this website" $recipient="[email protected]"; sendMail($fromname,$recipient); $recipient="[email protected]"; sendMail($fromname,$recipient); Regards Liam Link to comment https://forums.phpfreaks.com/topic/71684-coming-from-a-perl-user-global-variables-between-functions/#findComment-360884 Share on other sites More sharing options...
scooter41 Posted October 3, 2007 Author Share Posted October 3, 2007 cheers guys! I knew there must be some kind of easy answer Link to comment https://forums.phpfreaks.com/topic/71684-coming-from-a-perl-user-global-variables-between-functions/#findComment-360890 Share on other sites More sharing options...
MmmVomit Posted October 3, 2007 Share Posted October 3, 2007 it starts parsing at the top you need to define functions before they are called also you need global $counter; i would write it like this $counter=0; function sendMail($from,$to) ( global $counter; $counter++; echo 'Counter: '.$counter.' From: '.$from.' To: '.$to.'<Br>'; ) $sendtoname="example"; $subject="This is the subject to send"; $fromname="This is the from name"; $message="Please checkout this website" $recipient="[email protected]"; sendMail($fromname,$recipient); $recipient="[email protected]"; sendMail($fromname,$recipient); Regards Liam AFAIK, it doesn't matter what order things are in. You could put the global variable anywhere in the code, as long as it is defined before the function is actually called. Link to comment https://forums.phpfreaks.com/topic/71684-coming-from-a-perl-user-global-variables-between-functions/#findComment-360892 Share on other sites More sharing options...
shocker-z Posted October 3, 2007 Share Posted October 3, 2007 i meant the function has to be defined before it is called, also if you have an initial value for counter that would also need to be defined before the function is called. Liam Link to comment https://forums.phpfreaks.com/topic/71684-coming-from-a-perl-user-global-variables-between-functions/#findComment-360899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.