Jump to content


Photo

Global String


  • Please log in to reply
3 replies to this topic

#1 lucerias

lucerias
  • Members
  • PipPipPip
  • Advanced Member
  • 101 posts

Posted 03 October 2006 - 02:36 AM

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 03 October 2006 - 03:13 AM

what you want to do is either pass in the variable to the function either as a reference or return the new value from the function instead:
<?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;
?>

good luck
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 lucerias

lucerias
  • Members
  • PipPipPip
  • Advanced Member
  • 101 posts

Posted 03 October 2006 - 06:57 AM

Thanks for your code but why the output is "St ing" instead of "String" and what is the meaning of this syntax "&"

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

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 03 October 2006 - 12:28 PM

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 quotes. read up on the manual page for stripslashes to see what the deal is. if you're wanting to remove all 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 reference instead of by value.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users