Cetanu Posted October 18, 2009 Share Posted October 18, 2009 So, I've been reading up on Functions and I like them! However, I need some help using variables in functions, since it's kind of confusing. I just made these functions: <?php function Protect($var){ $var = mysql_real_escape_string(htmlentities(stripslashes($var))); } function Preserve($var){ $content = nl2br(str_replace(" ", " ", $var)); } ?> Neither of them work. Dunno why. From what I understand... If I do this: function HelloThere($var){ Then when I use the function I can go... HelloThere($hello); And the $hello variable will be substituted for $var from when I defined the function. Am I not right, confusing, or misled? Please help!!! Thanks. PS I originally had this, but it didn't work: <?php function Protect($var){ $variable = $var; $protect1 = mysql_real_escape_string($variable); $protect2 = htmlentities($protect1); $protect3 = stripslashes($protect2); } function Preserve($var){ $content = nl2br(str_replace(" ", " ", $var)); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178116-solved-confusing-functions/ Share on other sites More sharing options...
Garethp Posted October 18, 2009 Share Posted October 18, 2009 You are right. But remember the variable scope http://php.net/manual/en/language.variables.scope.php function Test($var) { $var .= " appending test"; } echo Test("This is an"); Won't do anything. You need to return a value, or Test("This is an"); won't equal anything. So function Test($var) { $var .= " appending test"; return $var; } echo Test("This is an"); Would work. Try changing function Preserve($var){ $content = nl2br(str_replace(" ", " ", $var)); } to function Preserve($var){ return nl2br(str_replace(" ", " ", $var)); } Quote Link to comment https://forums.phpfreaks.com/topic/178116-solved-confusing-functions/#findComment-939131 Share on other sites More sharing options...
cags Posted October 18, 2009 Share Posted October 18, 2009 It's all to do with scope. You can pass the variable in using the method you have used in your example, but you are passing the variable 'by value'. Basically it means your making a copy of the variable and passing it to the function which the function can use at will, but it will not change the value of your original $var variable. Here's an example... $var = 10; triple($var); function triple($var) { $var = $var * 3; echo $var; } echo $var; It probably makes alot more sense if you use a different variable name inside the function. Like so... $var = 10; triple($var); function triple($var2) { $var2 = $var2 * 3; echo $var2; } echo $var; If you wanted to triple your original variable you would do it like so... $var = 10; $var = triple($var); function triple($var2) { $var2 = $var2 * 3; return $var2; } echo $var; Another method, which I'd considered slightly more advanced (probably because I learnt it after the above) is using the global keyword. $var = 10; triple(); function triple() { global $var; $var = $var * 3; } echo $var; Quote Link to comment https://forums.phpfreaks.com/topic/178116-solved-confusing-functions/#findComment-939132 Share on other sites More sharing options...
Cetanu Posted October 18, 2009 Author Share Posted October 18, 2009 Thanks guys, this makes it a little clearer for me. I'm going to take both of your suggestions and play around with my code. I'll get back to you to let you know the results. EDIT: It works, and I understand functions more...thanks a bunch!! Quote Link to comment https://forums.phpfreaks.com/topic/178116-solved-confusing-functions/#findComment-939142 Share on other sites More sharing options...
Cetanu Posted October 18, 2009 Author Share Posted October 18, 2009 I do have one question: what does "global" do? Quote Link to comment https://forums.phpfreaks.com/topic/178116-solved-confusing-functions/#findComment-939155 Share on other sites More sharing options...
Garethp Posted October 18, 2009 Share Posted October 18, 2009 Well, Global variables can be used and edited both inside and outside the functions. So you don't need to pass them as arguments or return them Quote Link to comment https://forums.phpfreaks.com/topic/178116-solved-confusing-functions/#findComment-939159 Share on other sites More sharing options...
mikesta707 Posted October 18, 2009 Share Posted October 18, 2009 the global keyword tells the function to use the variable that is in global scope, rather than in local scope. for example, in a simple function function func() { $var = "Hello world!"; echo $var; } the variable $var only exists inside the function, and can only be accessed inside the function. It has what is known as a local scope (local to that particular function) However, variables used in your script(not inside functions or classes) have a global scope, for example $var1;//global scope $var2;//global scope function func(){ $var1;//local scope $var3;//local scope } Global basically means it can be used anywhere (well mostly any where) and local scope means it can only be used in its code block (the {}) note that the two variables that are not in the function have a global scope. Also note that the $var1 variable in the function has a local scope even though it has the same name as a variable with global scope. This is because when you simply declare a variable, by default, it will gain the scope of the part of the program it belongs to. if its in a function, it gains a local scope. if its in the body of the program itself, it has a global scope. If I want to use the global $var1 I have to specify that, by using the global keyword, as cags pointed out $var1 = "hello world"; function func() { global $var1; echo $var1;//hello world } Quote Link to comment https://forums.phpfreaks.com/topic/178116-solved-confusing-functions/#findComment-939162 Share on other sites More sharing options...
Daniel0 Posted October 18, 2009 Share Posted October 18, 2009 I do have one question: what does "global" do? It kills a baby and a kitten each time you use it. Quote Link to comment https://forums.phpfreaks.com/topic/178116-solved-confusing-functions/#findComment-939176 Share on other sites More sharing options...
Cetanu Posted October 18, 2009 Author Share Posted October 18, 2009 I do have one question: what does "global" do? It kills a baby and a kitten each time you use it. :happy-03: :happy-03: I hate cats. xD Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/178116-solved-confusing-functions/#findComment-939294 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.