IT Community - Software Programming, Web Development and Technical Support

A variable is defined and when we try to call it within a function, it won't work .W

This is a discussion on A variable is defined and when we try to call it within a function, it won't work .W within the PHP Programming forums, part of the Web Development category; A variable is defined and when we try to call it within a function, it won't work .Why..? For ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-26-2007, 03:34 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Question A variable is defined and when we try to call it within a function, it won't work .W

A variable is defined and when we try to call it within a function, it won't work .Why..?
For example
.$text = “My name is ”;

function my_name($name) {

if(isset($name)) {
print($text . $name);
}

}


name(“James”);
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-26-2007, 03:37 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: A variable is defined and when we try to call it within a function, it won't wor

Hi...The above code wont work because as with other languages, PHP variables have scope. This helps keep the namespace free (imagine having to unset temporary variables every time you needed them in functions!) and preserves accidental overwriting. To call a variable from global scope—i.e., any variable assigned outside of a function, into local scope—i.e., a function, you need to use the global keyword,

it shuld be modified as...

$text = “My name is ”;


function my_name($name) {

global $text;

if(isset($name)) {
print($text . $name);
}

}


name(“James”);
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call a javascript function in a client side click? leoraja8 ASP and ASP.NET Programming 4 10-30-2007 01:43 AM
HTML format and attachment in the mail function are work in gmail but not in yahoo Vani Sri PHP Programming 0 10-18-2007 02:48 AM
difference between call by value and call by reference amansundar Java Programming 2 09-18-2007 01:34 AM
Function call and System call vigneshgets Operating Systems 1 08-01-2007 06:18 AM
Diff inline function and ordinary function vigneshgets C and C++ Programming 1 05-24-2007 11:34 AM


All times are GMT -7. The time now is 04:48 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0