473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2163
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,460 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
3
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.