有没有办法在PHP体内使用PHP来编写样式来*移动*它

I was just wondering if there was a way to write CSS styling within the body, to pull it into the head tag using some php.?

... not necessarily inline styling

... Is there a way to run the php before the HTML head tag is parsed?

I assumed this would be possible:

  1. write some style in the body:

    <body>
      <div class="moveCSS">
        <style type="text/css">
          #frog
          {
          font-color: green;
          }
        </style>
      </div>
    <p class="frog">Frog's are stereotypically documented as being 'green' in colour, when in actual fact are often very different depending on their environmental habitat!</p>
    </body>
    
  2. In the head write something similar to this:

    <head>
      <?php
        function getElementsByClassName(\DOMDocument $DOMDocument, $ClassName)
        {
          $Elements = $DOMDocument->getElementsByTagName("moveCSS");
          $Matched = array();
    
          for($i=0;$i<$Elements->length;$i++)
          {
    
            if($Elements->item($i)->attributes->getNamedItem('class')->nodeValue == $ClassName)
            {
              $Matched[]=$Elements->item($i);
            }
          }
          return $Matched;
        }
    ?>
    </head>
    

    (Snippet of PHP code reference: Link to Page ...FYI: I'm sure variable names need to start with a lowercase alphabetical char in PHP)

If this were possible, It would allow me to write better CSS into CMS articles, avoiding the requirement to access the CSS files and to manually add custom entries. The single class name of 'moveCSS' would enforce that anything written within a 'moveCSS' div will be included in the head. Making my work more malleable.

No, instead write the css in original document and load it with JavaScript wherever required or completely write inline if css is not too complex.