I am having problems with PHP I had to reinstall apache and php and MySQL and this code worked great but now that I reinstalled its giving me this error
Notice: Undefined variable: out in G:\file server\SERVER\server\admin\index.php on line 51

$title = $_POST['title'];
	$post = addslashes($_POST['text']);
	
	function nl2p($str) {
    $arr=explode("\n",$str);

    for($i=0;$i<count($arr);$i++) {
        if(strlen(trim($arr[$i]))>0)
            $out.='<p>'.trim($arr[$i]).'</p>';
    }
    return $out;
}
	$post = nl2p($post);

but this is for adding a post to a blog thing I'm trying to build however when I got to edit one with this exact same function it works. What's wrong?

Recommended Answers

All 2 Replies

Just as error says: $out is defined in the Loop and it dies out once loop is finished.
Move it out of the loop!

$title = $_POST['title'];
	$post = addslashes($_POST['text']);
	
	function nl2p($str) {
    $arr=explode("\n",$str);
    $out="";
    for($i=0;$i<count($arr);$i++) {
        if(strlen(trim($arr[$i]))>0)
            $out.='<p>'.trim($arr[$i]).'</p>';
    }
    return $out;
}
	$post = nl2p($post);

It's really a big word to say "PHP is being DUMB". if PHP is being a dumb, we'll be sure not posting PHP stuffs here...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.