Jump to content

How can I pass a CSS class style into a PHP function's parameter?


Victory
Go to solution Solved by doddsey_65,

Recommended Posts

Hi guys,

 

It's a simple question really (just getting used to php - actionscript 3.0 developer!) but I need to know how I can pass a CSS class into the arguments in a PHP function, then have it apply that style to text in html. What I have so far is:

<?php
function addText($text,$class) {


 ?>
<div class= $class >
<p><?php echo $text ?></p>
</div>
<?php
}
?>
 
But it doesn't work, the <div class = $class > should be different I think. Anybody got a solution?
Link to comment
Share on other sites

  • Solution
<div class= $class >
<p><?php echo $text ?></p>
</div>

Just as you have echoed the $text variable you will need to do so for the $class variable

<div class="<?php echo $class; ?>">
<p><?php echo $text; ?></p>
</div>

Also needs to be wrapped in double quotes ( or single if you prefer ) for valid HTML syntax

Link to comment
Share on other sites

echo is a language construct unlike return (that's a statement) and is not terminating in the way return is (return is mostly used with functions to return a value which becomes the value of the function).
Link to comment
Share on other sites

echo is a language construct unlike return (that's a statement) and is not terminating in the way return is (return is mostly used with functions to return a value which becomes the value of the function).

 

Yeah, I originally thought that echo would end the function immediately like return does. I use return all the time in AS3, but I had never encountered echo before.

 

I'm using this with Thematic in WordPress, I'm using this function to add text to the page with filter hooks - just wondering, does anybody know the best way of getting the text to format to a specific section on the page? Like I use the following:

add_filter('thematic_abovecomments',addText("Add comments here.","default-header"));

But the text appears at the top of the page instead of formatting with the comments section. I guess I'm not using the filter hooks correctly, I can't figure it out...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.