title: "Undefined variable" notice in truncate function, but only when certain strings are passed in
body: <p>I have this function to truncate strings longer than a specified parameter.
  It works with all the strings I passed to it since now.But when I test with the following string I have problems:</p>
  
  <pre><code>Fifth post is the worst ever.Dont you believe?Just read it!
  </code></pre>
  
  <p>It says:</p>
  
  <pre><code>"Undefined variable:string"  
  </code></pre>
  
  <p>the function is:</p>
  
  <pre><code>public function limitString($message,$position,$limitString)
  {
  
      if(strlen($message)&lt;$position)
      $string=$message;
  
      else
      {
      $post = substr($message,$position,1); // Find what is the last character displaying.
  
      if($post !=" ")
      {
  
          for($i=$position-1;$i&gt;0;$i--)
          {
            $post = substr($message,$i,1);
            if ($post ==" ")
            {
              break;
            }
          }
  
          $string=substr($message,0,$i).$limitString;
      }
  
      }
  
      return $string; 
  }
  </code></pre>
  
  <p>This is how I call It:</p>
  
  <pre><code>limitString($string,33,"...")
  </code></pre>
  
  <p>Where am I wrong?</p>
  
  <p><strong>SOLVED:</strong> As you guys make me notice The returned $string value wasnt defined outside of </p>
  
  <pre><code>if($post!==' ')
  </code></pre>
  
  <p>So I added an else statement defining the $string as:</p>
  
  <pre><code>$string=substr($message,0,$position).$limitString;
  </code></pre>
  
  <p>Thanks
  Luca</p>