How to add html markup and css style in php?

by raphael_tillman , in category: PHP General , a year ago

How to add html markup and css style in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , a year ago

@raphael_tillman 

To add HTML markup and CSS style in PHP, you can use the following steps:


Step 1: Create a new PHP file with the .php extension. For example, "index.php".


Step 2: Start the PHP code block by opening <?php tag.


Step 3: Within the PHP code, use the echo statement to print the HTML markup and CSS style.


For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php
  echo '<html>';
  echo '<head>';
  echo '<style>';
  echo 'body { background-color: #f2f2f2; }';
  echo '</style>';
  echo '</head>';
  echo '<body>';
  echo '<h1>Welcome to my PHP page!</h1>';
  echo '</body>';
  echo '</html>';
?>


In the above example, the PHP code is used to generate HTML markup and CSS style by concatenating the necessary strings using the echo statement.


Step 4: Save the PHP file and access it through a web browser to see the rendered HTML markup and CSS style.


Note: It is generally recommended to separate the PHP code from the HTML markup and CSS style by using separate files or using a templating engine like PHP's heredoc or alternative syntax. This can help improve code readability and maintainability.