By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,622 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,622 IT Pros & Developers. It's quick & easy.

variable scope

P: n/a
I am a little surprised that the following that $x is visible outside of the
scope in
which it is (?)defined(?) (not sure if that is the correct term here). I am
trying
to find where in the php docs that this is discussed. Haven't found anything
yet.
Any help appreciated.

I am also assuming that the following scoping rule holds for other
constructs
as well.

Thanks, Mike.

if(true)
{
$x= '_hello';
}

if(false)
{

}
else
{
$x= $x.'_world';
}

echo $x;

//output: _hello_world_

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 15 '05 #1
Share this Question
Share on Google+
7 Replies

P: 2
Expand|Select|Wrap|Line Numbers
  1. if(true)
  2.  {
  3.         $x = '_hello';
  4.  }
  5.  
  6. if(false)
  7.  {
  8.  
  9.  }
  10. else
  11.  {
  12.         $x = $x.'_world';
  13.  }
  14. echo $x;
  15.  
In PHP $x created in a block ( {...}) becomes global with that script or function.
Aug 15 '05 #2

niheel
Expert Mod 100+
P: 2,334
Welcome to wonderful world of easy php scripting lol.

Thanks for the post anjanesh.
Aug 15 '05 #3

P: n/a
Michael G wrote:
I am a little surprised that the following that $x is visible outside of the
scope in
which it is (?)defined(?) (not sure if that is the correct term here). I am
trying
to find where in the php docs that this is discussed. Haven't found anything
yet.
Any help appreciated.

I am also assuming that the following scoping rule holds for other
constructs
as well.

Thanks, Mike.

if(true)
{
$x= '_hello';
}

if(false)
{

}
else
{
$x= $x.'_world';
}

echo $x;

//output: _hello_world_

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

I'm not 100% up with what your trying to do. The If statments should
work. The only thing that maybe comes to mind is Global Variables for
use in things like functions etc.

This may help.
http://au2.php.net/variables.scope
Aug 15 '05 #4

P: n/a

"Smitro" <no****@myh0use.c0m> wrote in message
news:43********@quokka.wn.com.au...
Michael G wrote: I'm not 100% up with what your trying to do. The If statments should work.
The only thing that maybe comes to mind is Global Variables for use in
things like functions etc.

This may help.
http://au2.php.net/variables.scope


I am used to working with strongly typed languages. I am just trying to find
out what the scoping rules are for php.

Thanks for the link. I've been reading that but it doesn't address my
concerns. It discusses
scope in terms of file (global) and functions but not in terms of construct
scope, such as if
statements, etc. The little snippet of code I presented in the OP just shows
or implies what
the construct scoping rules are for php. I wanted to find something more
explicit than depending
upon an example.

Mike

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 15 '05 #5

P: n/a
Hi Mike,
I am used to working with strongly typed languages. I am just trying to find
out what the scoping rules are for php.


You nearly ansewered your question by yourself.

Because the lack of typing variables are always in the function scope.
In PHP all varibles are declared by default (AFAIK you may not rely on a
value).

echo("hello $x") will print "hello " and not yield an error like
"undeclared variable x in line ...

I don't want to raise a religious ;-) discussion but
http://premshree.seacrow.com/writings/typing wrote a good Article about
the different typing.

The greatest advantage of a strong typing is that typos are reduced.
The advantage of "giving hints to a compiler" how storage should be
organized is of lesser importance in intrepreted languages.

However scope and and typing shouldn't be mixed up. The variable Scope
in C is defined by the curly braces.

The curly braces in PHP have only the grouping purpose of code and not
the declaration purpose of C.
Variables Scope in PHP is borderd by the keywords function, class.

Apart of this, I always considered it as a bad idea to declare variables
on another place but on the beginning of the function.

HIH

Jo
Aug 15 '05 #6

P: n/a
Hi,

Joachim Weiß wrote:
Hi Mike,
I am used to working with strongly typed languages. I am just trying to find
out what the scoping rules are for php.
You nearly ansewered your question by yourself.

Because the lack of typing variables are always in the function scope.
In PHP all varibles are declared by default (AFAIK you may not rely on a
value).


AFAICT this is not an issue if strong/weak typing, but of lexical scope
(more precisely, the lack of it). Variables could be as well typeless
but at the same being subject to lexical scoping, that is, the variable
exists only from where it is defined to the end of the code block that
contains its declaration, such as in Perl (see <http://tnx.nl/php#scope>
for a more detailed comparison).
echo("hello $x") will print "hello " and not yield an error like
"undeclared variable x in line ...
Unless of course you have the interpreter configured to give you
"notices" on these events (setting E_NOTICE in error_reporting). They
don't stop or alter your script execution at all, but at least they give
you a warning that something *may* have gone wrong.

And in PHP5 there is E_STRICT which supposedly would abort the execution
if one of this notices is found. Sadly enough, you can't check for
strictness without attempting to actually _run_ the script :-(

[...]
However scope and and typing shouldn't be mixed up. The variable Scope
in C is defined by the curly braces.

The curly braces in PHP have only the grouping purpose of code and not
the declaration purpose of C.
Yes, that's exactly the point.
Variables Scope in PHP is borderd by the keywords function, class.

Apart of this, I always considered it as a bad idea to declare variables
on another place but on the beginning of the function.


Well, I tend to declare them close to where they are going to be used
(loops, mostly), so the chunk of code sort of contains most of involved
stuff and can be understood at a glance. But that's a matter of taste,
of course :)

--
Cristian Gutierrez /* cr******@dcc.uchile.cl */
"Never put off until run time what you can do at compile time."
-- David Gries, in "Compiler Construction for Digital Computers",
circa 1969.
Aug 16 '05 #7

P: n/a

"Cristian Gutierrez" <cr******@dcc.uchile.cl> wrote in message
news:87************@bender.guti.cl...
Hi,

Joachim Weiß wrote:
Hi Mike,
I am used to working with strongly typed languages. I am just trying to
find
out what the scoping rules are for php.


You nearly ansewered your question by yourself.

Because the lack of typing variables are always in the function scope.
In PHP all varibles are declared by default (AFAIK you may not rely on a
value).


AFAICT this is not an issue if strong/weak typing, but of lexical scope
(more precisely, the lack of it). Variables could be as well typeless
but at the same being subject to lexical scoping, that is, the variable
exists only from where it is defined to the end of the code block that
contains its declaration, such as in Perl (see <http://tnx.nl/php#scope>
for a more detailed comparison).


I do understand now that I was mixing typing with scoping as Joachim
also mentioned.
echo("hello $x") will print "hello " and not yield an error like
"undeclared variable x in line ...


$x;
echo $x;

Doesn't not through a notice.

$y = $temp['temp'];

will through a notice if $temp['temp'] is not defined.
AFAICT there are no declarations in php just definitions.

I have enjoyed working with php. A new experience for me and still learning.

Mike

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.