Jump to content

Coming from a Perl User - global variables between functions?


scooter41

Recommended Posts

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

 

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.

 

 

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

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.