473,383 Members | 1,742 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,383 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 2167
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.