Uiot
Uiot

Reputation: 83

Is it ok to use <style> tags inside php?

If I have certain styles that need to take place upon some php condition, is it valid and good practice to use the style tag inside php scripts ?

if(condition){
 echo "<style> .div{display:block} </style>";
} else {
 echo "<style> .div{display:none} </style>";
}

I am thinking that since the tag is not place in the head of the document, it could be a problem ? Or is this a usual way of doing this sort of thing ?

Thank you.

Upvotes: 0

Views: 160

Answers (1)

Enrico Dias
Enrico Dias

Reputation: 1487

The way you are doing certainly works, but it's not a good practice to mix PHP with html/css this way. The best way is to use a template class to manipulate the html.

As a beginner you may be overwhelmed with the features of most advanced template classes, so I recommend start with something simple to get the basic concept.

Upvotes: 1

Related Questions