Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

styling css with php from constructor

I'm very beginner to php so I'm trying to understand the following:

class Style{

    public $fontsize;
    public $fontcolor;
    public $bgcolor;
    function __construct($fontsize,$fontcolor,$bgcolor){
        $this->fontsize="font-size: $fontsize";
        $this->fontcolor="color: $fontcolor";
        $this->bgcolor="background-color: $bgcolor";
    }
}
$setStyle = new Style("12px","red","blue");

$output = <<<EOT

$setStyle->fontcolor; <!-- this prints ->   color: red;      --->

<div style="<?php $setStyle->fontcolor; ?>">This is test for php</div><!---but why not here, it's not working---->

EOT;
echo $output;

The question is clearly defined in the code above with comments why css style is not working.

Upvotes: 1

Views: 99

Answers (2)

Krish R
Krish R

Reputation: 22711

Can you try this, replaced

style="<?php $setStyle->fontcolor; ?>" 

into

style="$setStyle->fontcolor"

Code:

$output = <<<EOT

  $setStyle->fontcolor <!-- this prints ->   color: red;      --->

  <div style="$setStyle->fontcolor">This is test for php</div><!---but why not here, it's not working---->

EOT;

echo $output;

Upvotes: 0

koralarts
koralarts

Reputation: 2112

Change your div to this:

<div style="{$setStyle->fontcolor}">This is test for php</div>

Since your code is already in a <?php ?> block, you don't need to add it again unless you're going to put it outside a <?php ?> block, and; if you do put it outside, do it like this:

<div style="<?php echo $setStyle->fontcolor; ?>">This is a test for php</div>

Upvotes: 1

Related Questions