PHP Introduction | Values, Variables, Arrays, and Syntax | Syntax

Escaping from HTML

In an HTML document, the parts marked as PHP code are interpreted by PHP. There are several ways to mark PHP code.

Method 1

PHP interprets the content between <?php and ?>.

<?php
  // PHP Code
?>

This is the most common method and can be used in any situation.

Method 2

You can also use a script tag.

<script language="php">
  // PHP Code
</script>

Other Forms

If configured in php.ini, the following formats can also be used. They are not recommended.

<?
  // PHP Code
?>
<%
  // PHP Code
%>

Separating Statements

When writing multiple statements, separate them with semicolons (;).

<?php
  echo 'Lorem';
  echo 'Ipsum';
?>

The final statement does not have to end with a semicolon.

<?php
  echo 'Lorem';
  echo 'Ipsum'
?>

If the PHP code is at the end of the document, ?> can be omitted.

<?php
  echo 'Lorem';
  echo 'Ipsum';

Comments

Single-line Comments

Single-line comments are written with // or #. The content after // or # becomes a comment. The comment effect ends at the line break.

// comment

Multi-line Comments

Multi-line comments are written with /* and */. The content between /* and */ becomes a comment.

/* comment
   comment */

Example

<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Comment</title>
  </head>
  <body>
    <?php
      echo '<h1>Lorem</h1>'; // single-line comment
      echo '<h1>Ipsum</h1>'; # single-line comment
      /* multi-line comment
         multi-line comment */
      echo '<h1>Dolor</h1>';
    ?>
  </body>
</html>