473,811 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2196
anjanesh
2 New Member
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,456 Recognized Expert Moderator Top Contributor
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.uc hile.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.u chile.cl> wrote in message
news:87******** ****@bender.gut i.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
2680
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 before the variable is used? I'm trying to get an idea of whether the .NET compilers for VB.NET and C# will move all variable declaration to the beginning of a method or whether they will allocate the memory as it is needed by the method to...
3
2018
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 + el2; if (value1 > value2) return 1;
4
14907
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 strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
23
19209
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
25704
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 bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
0
35252
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 the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
2
4720
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 declare the global variable...and it is having scope throughout the file then what is so different in extern variable???
5
2244
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
5499
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 that may print some messages. foo(...) { if (!silent)
3
2047
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 the same file (.c + all relevant .h's)? e.g.
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10393
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.