|
-
Nov 13th, 2004, 11:09 AM
#1
Thread Starter
Ex-Super Mod'rater
Function Variables -[RESOLVED]-
If I have a function and the variables inside I want to be private to that function is this possible? If so how do I make them private or are they by default?
Last edited by Electroman; Nov 13th, 2004 at 10:36 PM.
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

-
Nov 13th, 2004, 02:55 PM
#2
Do you mean functions in a calss? If so in PHP 4 there is no private/public but it has been introduced in PHP 5.
If you are talking about variable scope then this is how it works.
- All variables used inside a function have local scope.
- Local variables with the same name as global variables elsewhere in a script are treated as different entities with the exception of super globals
- To use a global variable declared outside a function you must either declar it inside the function using the global keyword or access it through the super global array $GLOBALS.
Here's some examples:
PHP Code:
<?php
$global_var = 'no function called';
echo("Calling myfunction: ");
myfunction();
echo("\$global_var=$global_var");
echo("Calling myfunction2: ");
myfunction2();
echo("\$global_var=$global_var");
echo("Calling myfunction3: ");
myfunction3();
echo("\$global_var=$global_var");
function myfunction()
{
/* this creates a new variable with called - $global_var with local scope */
$global_var = 'myfunction called';
}
function myfunction2()
{
/* we can declare a global variable here */
global $global_var;
$global_var = 'myfunction2 called';
}
function myfunction3()
{
/* we can also use the $GLOBALS super global array */
$GLOBALS['global_var'] = 'myfunction3 called';
}
?>
-
Nov 13th, 2004, 10:35 PM
#3
Thread Starter
Ex-Super Mod'rater
The global thing isn't like extern (in C++) is it? Like in the following code I've highlighted two of them, are they using the same space? I'm doughting this but hopeing at the same time. Just cos I have variables that I might want to be global and also used in more than one function I guess to do that I'd have to use the super global.
PHP Code:
<?php
$global_var = 'no function called'; <---- This one
echo("Calling myfunction: ");
myfunction();
echo("\$global_var=$global_var");
echo("Calling myfunction2: ");
myfunction2();
echo("\$global_var=$global_var");
echo("Calling myfunction3: ");
myfunction3();
echo("\$global_var=$global_var");
function myfunction()
{
/* this creates a new variable with called - $global_var with local scope */
$global_var = 'myfunction called';
}
function myfunction2()
{
/* we can declare a global variable here */
global $global_var;
$global_var = 'myfunction2 called'; <---- This one
}
function myfunction3()
{
/* we can also use the $GLOBALS super global array */
$GLOBALS['global_var'] = 'myfunction3 called';
}
?>
I think for now all I needed to know was that my function variables were private .
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

-
Nov 14th, 2004, 02:53 AM
#4
Originally posted by Electroman
The global thing isn't like extern (in C++) is it?
Yes
-
Nov 14th, 2004, 09:49 AM
#5
Thread Starter
Ex-Super Mod'rater
Posted by visualAd
Yes
I knew I shouldn't have asked a negative question . So it is like extern?
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

-
Nov 14th, 2004, 09:55 AM
#6
It is like extern, except that it is mandatory if you want to access a global. That's because of the lack of variable declarations in PHP.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 14th, 2004, 10:05 AM
#7
Originally posted by Electroman
I knew I shouldn't have asked a negative question . So it is like extern?
Sorry - I misread. It is like extern.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|