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="example1@example.com"; sendMail(); $recipient="example2@example.com"; 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 Quote Link to comment 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. Quote Link to comment 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="example1@example.com"; sendMail($fromname,$recipient); $recipient="example2@example.com"; sendMail($fromname,$recipient); Regards Liam Quote Link to comment 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 Quote Link to comment 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="example1@example.com"; sendMail($fromname,$recipient); $recipient="example2@example.com"; 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.