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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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”); |
| Sponsored Links |
| |||
| 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”); |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |