My head is starting to hurt just thinking about all the possabilities and impossabilities of mixing html and css with php and I'd like to understand how's this works. I did a php project for a guy one time and I came close to shooting myself in the face and I may be starting another project for my current employer, and I'd like it go to a little better this time around. When programming in php you often use php to generate a lof, if not all, of a page's html. When so much of the html is generated via php instead of being statickck in the file, can and does your css rules apply to the php generated html?

Another thing I'd like to understand is about writing static html but doing so within a php if/else block. One of the problems I had in the past was that I was php coding over a lot of pre existing html that had all the attributes double quoted (""), which of course is a pain when dealing with php. What I'd like to be able to do when that happens is to start a php if block, end the php with a ?> tag, leave the static pre-existing html code alone and at it's end start the php up again with <?php elif. Can I count on the static html only showing up if the condition is met? It would be between and if/elfi block but not in active php tags. If not then I either have to manually remove the (") quotes and change them to singel quotes (') and then wrap each and every line of the stacick html code with a php echo statement. This stuff always gets out of hand, at least when I try my hand at it. Thanks.

Recommended Answers

All 4 Replies

PHP generated HTML arrives at the client's browser the same as any other server generated HTML - that is, it presents itself as nothing but HTML. There are <?php ?> tags in what is received. So the answer to your first question is yes, CSS and javascript can be used to target classes/IDs you generate in PHP because CSS and javascript can't tell the difference.
You may find it useful to get into the style followed by most open source PHP platforms where actual processing PHP (database operations/ calculations, etc) are done in a class while outputting PHP (making HTML) is in a template file. The separation will help kep things in order. Have a look at Smarty and similar templates to get an idea.

Thanks. Any input on my second question reguarding PHP broken if/elif with html in the middle?

lewashby as I understood you are programming in PHP in one block pure in procedural way. I will not start explaining how OOP or even differentiating the view from the other parts of the app would help because this would go a way beyond what you are asking . Here is an example with if else and output just using functional PHP and none scope separation.

<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    A functional PHP example of treating if else in output
    <?php
    $conditionsMet = false;
    if($conditionsMet)
    {
        sayYes();
    }
    else
    {
        sayNo();
    }
    ?>
</body>
</html>
    <?php
    function sayYes()
    {?>
<div class="something">YES</div>
    <?php
    }

    function sayNo()
    {?>
<div class="somethingElse">No</div>
    <?php
    }
    ?>

php has multiple formats for echoing out plain html in single lines, up to book-amounts of text

heredoc or nowdoc format can do pretty much what you want

if(condition) echo <<<endofmultiquotedhtml
this is some huge block of static html
endofmultiquotedhtml;
elseif(condition) echo <<<somethingelse
<input type="doublequoted">'singlequoted'
and carriage returns
and $php_variables
any amount of html code
and any amount of more lines
all kindds of stuff
more stuff
"stuff"
'stuff'
\x41 (capital A)
somethingelse;

php strings heredoc
nowdoc continues under heredoc in the linked page

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.