Page 1 of 1

Can't echo Please help

Posted: Sun Jun 26, 2011 7:03 am
by Theramore
i got the following code:

Code: Select all

<?php

class Test
{
public $_text;
public $_submit;

public function __construct()
{
$this->_submit = isset($_POST['submit'])? 1 : 0;
$this->_text = ($this->_submit)? ($_POST['text']) : $_text;
}


public function registervalues()
{
$_text = $this->_text;
}


public function action()
{

echo $_text;

}


}


if(isset($_POST['submit']))
{

$test = new Test();

$test->registervalues();
$test->action();

}

?>

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='POST'>
<input type='text' name='text'><br>
<input type='submit' value='submit' name='submit'>
</form>
When i'm submitting this i get back no values, Why?
I am trying to understeand something so this is why i created this :) :mrgreen:

Re: Can't echo Please help

Posted: Sun Jun 26, 2011 7:10 am
by Idri
Whenever you're going to use a class-level variable you'll have to use the $this-> prefix (in case of statics it's self::).

Code: Select all

error_reporting(E_ALL)
Add this to the top of your file, and take into account what I said at the top of this post, and you'll see why it won't output anything.

Re: Can't echo Please help

Posted: Sun Jun 26, 2011 7:17 am
by Theramore
thanks a lot its working i thought i am going nuts:)

Re: Can't echo Please help

Posted: Sun Jun 26, 2011 7:19 am
by Theramore
one more question . can you explain me what the syntax

Code: Select all

 ($this->_whatever) : $whatevervar 
is doing?

Re: Can't echo Please help

Posted: Sun Jun 26, 2011 7:25 am
by Idri

Code: Select all

$this->_text = ($this->_submit)? ($_POST['text']) : $_text;
Do you mean that?

It's a shorthand if statement. It translates to

Code: Select all

if($this->_submit){
	$this->_text = $_POST['text'];
}else{
	$this->_text = $_text;
}

Re: Can't echo Please help

Posted: Sun Jun 26, 2011 7:27 am
by Theramore
yes that is what i ment i am too lazy to write down the entire code:) thanks now i understeand :)