Jump to content

Global String


lucerias

Recommended Posts

I have started my learning on php few weeks ago, i found that the 'global' and 'local' really mess me up, i can't keep myself stick to way of using them because in VB and other languages, a predefined variable can be passed in function and overwrite the previous value if necessary.

However, when comes to php, i must be alert of that all the time since i had established the "No care" habit on it. Anyone can help me to solve the following code problem, the idea is to stripslashes the $value, which is a string defined locally and used in function and echo the final result eventually. Thank you.

<?php
$value="St\ring";

function stripslashes_checkstrings($value)

    global $value;
         
if(is_string($value))
    {
        stripslashes($value); 
}   
return $value;   
}
     
stripslashes_checkstrings($value);
echo $value;

?>

I did this by referring to the following code,

<?php
$a = 1;
$b = 2;

function Sum()
{
  global $a, $b;

  $b = $a + $b;
}

Sum();
echo $b;
?>
Link to comment
Share on other sites

what you want to do is either pass in the variable to the function either [i]as a reference[/i] or [b]return the new value[/b] from the function instead:
[code]
<?php
// first option: pass by reference
function stripslashes_checkstrings(&$value) {
  if (is_string($value)) {
    $value = stripslashes($value);
  }
}

$value = "St\ring";
stripslashes_checkstrings(&$value);
echo $value;

// second option: return a value
function stripslashes_checkstrings($value) {
  if (is_string($value)) {
    $value = stripslashes($value);
  }
  return $value;
}

$value = "St\ring";
$value = stripslashes_checkstrings($value);
echo $value;
?>
[/code]

good luck
Link to comment
Share on other sites

the lack of the 'r' is due to the fact that "\r" is a carriage return, and if you read the manual for stripslashes, it only works on [i]quotes[/i]. read up on the manual page for stripslashes to see what the deal is. if you're wanting to remove [b]all[/b] slashes completely (which could be dangerous), you'd have to write your own function.

as for the ampersand, that is how you tell PHP to pass the variable by [i]reference[/i] instead of by [i]value[/i].
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.