new to functions

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
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

new to functions

Post by penguinboy »

I created a function to apply css border atributes to <td>'s

Code: Select all

<?php
	function border($top, $right, $bottom, $left, $style, $col)
	{
		if($style==""){$style="solid";}
		echo "style="
		border-style: ".$style.";
		border-color: #".$col.";
		border-top-width: ".$top."px;
		border-right-width: ".$right."px;
		border-bottom-width: ".$bottom."px;
		border-left-width: ".$left."px;
		"";
	}#################### EOF
?>
when I call the function from my class file, it looks something like this

Code: Select all

<?php
print"
<td ";$Lib -> border(0,0,2,0,'',$color); print">some text</td>";
?>
Notice how I have to print and then close the print and then call the function and then print again.
The reason I have to do that is because I'm echoing in the function.
I'd really like to be able to do something like:

Code: Select all

<?php
print"
<td ".$Lib -> border(0,0,2,0,'',$color).">some text</td>";
?>
Sorta like you can do with some of php's built in functions.

like:

Code: Select all

<?php
print"
<td>".mysql_num_rows($sql)."</td>";
?>
I don't know much about functions though.
Is there some way I can return that information without echoing/printing it?
And would that solve my problem or is there something else I need to do?


Thanks for any help.

--pb
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Instead of doing the echo within the function - return a value from it:

Code: Select all

function border($top, $right, $bottom, $left, $style, $col) 
   { 
      if($style==""){$style="solid";} 
      $style_info = 'style=" 
      border-style: '.$style.'; 
      border-color: #'.$col.'; 
      border-top-width: '.$top.'px; 
      border-right-width: '.$right.'px; 
      border-bottom-width: '.$bottom.'px; 
      border-left-width: '.$left.'px; 
      "';
      return $style_info; 
   }####################
Mac
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

alright!
thanks twigletmac!
thats exactly what I needed.

-pb
Post Reply