Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-07-2013, 02:07 PM   PM User | #1
sunilmkt
New Coder

 
Join Date: Jan 2013
Location: India
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
sunilmkt can only hope to improve
How to define Variable Scope in Php?

If you define a variable within a function that means variable is only available within that function. This variable cannot CALL in another function or in the body of your program code. These types of variable are known as a variable's scope. The scope of a variable defined within a function is local to that function. If you want to use variable that is defined in the main body of program code. Remember It must reference using global keyword.
<?php
function_SubNumber ( )
{
global $sum = 4 - 4
}

$sum = 0
Subnumber ( )
echo "4 - 4 = ".$sum
?>
sunilmkt is offline   Reply With Quote
Old 02-07-2013, 02:21 PM   PM User | #2
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
You're not really asking anything - or anything that is clearly a question.
TFlan is offline   Reply With Quote
Old 02-07-2013, 02:39 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by TFlan View Post
You're not really asking anything - or anything that is clearly a question.
Not only that, they are showing code that promotes *very* sloppy programming practices.

So to actually answer your question: scope is defined by the context in which you have declared your variables.

The code itself, is completely useless. The function signature is incorrect, it has no semi-colon delimiters, and the use of global is entirely incorrect (global is *not* an assignment operation). So many a syntax errors this has.

This does kind of raise question as to why this thread was posted in the first place though. . .
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
php, php function, php variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:34 AM.


Advertisement
Log in to turn off these ads.