472,119 Members | 935 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

variable scope

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
7 2052
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
2,452 Expert Mod 2GB
Welcome to wonderful world of easy php scripting lol.

Thanks for the post anjanesh.
Aug 15 '05 #3
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

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

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

Similar topics

3 posts views Thread by Grant Wagner | last post: by
4 posts views Thread by Gery D. Dorazio | last post: by
23 posts views Thread by Russ Chinoy | last post: by
pbmods
1 post views Thread by pbmods | last post: by
2 posts views Thread by Shraddha | last post: by
112 posts views Thread by istillshine | last post: by
3 posts views Thread by SRoubtsov | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.