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="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

 

Link to comment
Share on other sites

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
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.