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-03-2010, 09:59 AM   PM User | #1
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 789
Thanks: 208
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
Question Undefined variable?

HI People

How come the following code generates undefined variable error?

Error:
Code:
Notice: Undefined variable: abc in D:\xampp\htdocs\test2.php on line 6
Code:
<?php 
error_reporting(E_ALL);
 $abc  = 'hello';

function xyz(){
	echo $abc;
}

echo xyz();

?>

Any help will be appreciated


Thanks
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 02-03-2010, 10:14 AM   PM User | #2
er4o
New Coder

 
Join Date: Apr 2008
Location: Blagoevgrad, Bulgaria
Posts: 47
Thanks: 0
Thanked 8 Times in 8 Posts
er4o is an unknown quantity at this point
Set the $abc into the function

PHP Code:
<?php 
function xyz() {
$abc  'hello';
return 
$abc;
}
echo 
xyz();
?>
er4o is offline   Reply With Quote
Old 02-03-2010, 10:16 AM   PM User | #3
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 789
Thanks: 208
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
No, I want to access a variable that is declared outside a function, within that function. can we?
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 02-03-2010, 10:23 AM   PM User | #4
thekooliest
New Coder

 
Join Date: Mar 2009
Posts: 25
Thanks: 2
Thanked 3 Times in 3 Posts
thekooliest is an unknown quantity at this point
You have to pass variables into a function, they cannot read variables created outside. So now, function xyz reads the first parameter entered (in this case $abc is sent in the line echo xyz($abc) and uses it as variable $test within the function xyz...

PHP Code:
<?php 
$abc  
'hello';

function 
xyz($test){
    echo 
$test;
}

echo 
xyz($abc);

?>
http://www.w3schools.com/php/php_functions.asp (scroll down to adding parameters)
thekooliest is offline   Reply With Quote
Old 02-03-2010, 11:06 AM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,855
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
you could also make use of the superglobals (in this case the $GLOBALS array) that PHP provides.
Dormilich is offline   Reply With Quote
Old 02-03-2010, 12:36 PM   PM User | #6
dmgroom
New to the CF scene

 
Join Date: Feb 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
dmgroom is an unknown quantity at this point
For a start you effectively are duplicating the echo command, once within the fuction and once when calling the function. Secondly you need to declare the variable global within the function.

Code:
<?php 
error_reporting(E_ALL);
$abc  = 'hello';

function xyz(){
   global $abc;
    echo $abc;
}

xyz();
?>
OR
Code:
<?php 
error_reporting(E_ALL);
$abc  = 'hello';

function xyz(){
   
    echo $GLOBALS['abc'];
}

 xyz();
?>
dmgroom is offline   Reply With Quote
Old 02-03-2010, 12:42 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 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
globals destroy reusability. Pass you're variable as a parameter to you're function instead.
__________________
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

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 08:55 PM.


Advertisement
Log in to turn off these ads.