Jump to content

[SOLVED] Confusing Functions


Cetanu

Recommended Posts

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. :sweat:

 

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!!!  :D

 

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)); 
} 
?>

Link to comment
Share on other sites

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)); 
} 

Link to comment
Share on other sites

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;

 

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.