Unexpected output from very simple class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Unexpected output from very simple class

Post by mattcooper »

Hi guys,

I'm just trying to get to grips with OO PHP, and am having trouble with the following very simpl class:

Code: Select all

class create_Table {
	
		var $width;
		var $height;
		var $border;
		var $rows;
		var $content;
		var $data;
		
		function create_Frame ( ) { // create the framework for the table
		
				echo "<table border = ". $this->border ." width = ". $this->width ."% height = ". $this->height .">\n";
			
				$num = 1;
				$rows = $this->rows;
			
				while ( $num <= $rows ) {
			
					echo "<tr><td>". $this->content ."</td><td>Function call: ". $this->add_Data ( ) ."</td></tr>\n";
				
					$num++;
				
				}
			
			echo "</table>\n";
			
		} // end of function create_Frame ( )
		
		function add_Data( ) { // Add some data to the cells calling this function
			
			echo $this->data;
   		
		}
		
	} // end of class
Using the following code to instantiate and outpt it

Code: Select all

$table2 = new create_Table ( );
	
	$table2->data = "Hello world";
	$table2->width = "100";
	$table2->height = "200";
	$table2->border = "2";
	$table2->rows = "4";
	$table2->content = "Second table";
	
	$table2->create_Frame ( );
produces the following HTML output:

Code: Select all

<table border = 2 width = 100% height = 200>
Hello world<tr><td>Second table</td><td>Function call: </td></tr>
Hello world<tr><td>Second table</td><td>Function call: </td></tr>
Hello world<tr><td>Second table</td><td>Function call: </td></tr>
Hello world<tr><td>Second table</td><td>Function call: </td></tr>
</table>
You will see from this that $table2->data is outputting in the wrong place. Why would that be? Also, I appreciate that there is no need for a function to echo out $data in this case, but am trying underpin the principal of using one.

Thanks in advance...
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Instead of

Code: Select all

function add_Data( ) // Add some data to the cells calling this function
{ 
			   
    echo $this->data;
	 
}
You should return the data, NOT echo it

Code: Select all

function add_Data( ) // Add some data to the cells calling this function
{ 
			   
    return $this->data;
	 
}
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

I think it's because your "add_Data()" function is doing an echo instead of a return.

If you change the line

Code: Select all

echo $this->data;
to

Code: Select all

return $this->data;
it should work.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Perfect! Can you enlighten me as to why "return" should be used instead of "echo"?

Many thanks guys.
Last edited by mattcooper on Thu Jul 06, 2006 4:51 am, edited 1 time in total.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

Damn it.

Must type faster. :wink:

It's because you are doing your formatting in the calling line of code. Then you are entering a new function that has no knowledge of that formatting, and echoing something to the screen. Then, you are returning to the previous function, which continues with it's own echo statement.

You need to look at it from this point of view: Where is the first echo?

The first echo is in the add_Data function, because that gets called before the echo in which you are constructing the HTML.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

GM wrote:Damn it.

Must type faster. :wink:
...becuase on this line

Code: Select all

echo "<tr><td>". $this->content ."</td><td>Function call: ". $this->add_Data ( ) ."</td></tr>\n";
you are already echoing

So you dont need to echo again.

Im not 100% sure, but it must be something to do with the order in which the commands are executed.

The echo inside the function gets executed before the echo in the line i highlighted above.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Excellent bit of help from you both there, many thanks. Gonna be working on this for most of the day, I'm sure you'll be hearing from me again shortly!!

:D
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

As GM said,

Code: Select all

echo "<tr><td>". $this->content ."</td><td>Function call: ". $this->add_Data ( ) ."</td></tr>\n";
When PHP processes this line, its starts substituting the variables with their values. Teh variables will cause mere substitution of their values within the 'echo' statement which prints the entire row. But, when the function call is being processed PHP encounters another echo statement instead of a return value. This causes PHP to process that statement instead of substituting the value. Hence the echo'ed value within the function gets pronted first and then PHP prints the main echoed statement.

It is better to use a return statement when you are trying to print any variable throughit. If just HTML tags are going to be printed, then you don't need a 'return' statement.

For example,

Code: Select all

<?php

function return_tags() { ?>

<tr><td>Key : </td><td>Value :</td></tr>

<?php } ?>
This function will just add the HTML code contained within it in the place where it is called. In my codes geenrally, I donot prefer to do

Code: Select all

echo "<p>Welcome</p>";


within functions or anywhere. I just close the earlier PHP tag with ?>, write the HTML code and then again continue with <?php tag. I always feel better to leave HTML independent and not inscribed within PHP tags.
Post Reply