Basic Syntax

PHP tags

  • When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them.
  • Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.

PHP tags(contd.)

Recommended tags

Standard tag:

<?php echo ‘hello’; ?>

PHP tags(contd.)

<?php

echo ‘This is a test’;

?>

<?php echo ‘This is a test’ ?>

<?php echo ‘We omitted the last closing tag’;

Escaping from HTML

  • Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content.
  • This allows PHP to be embedded in HTML documents, for example to create templates.

<p>This is going to be ignored by PHP and displayed by the browser.</p>

<?php echo ‘While this is going to be parsed.’; ?>

<p>This will also be ignored by PHP and displayed by the browser.</p>

PHP Comments

  • A comment is simply text that is ignored by the PHP engine.
  • The purpose of comments is to make the code more readable.
  • It may help other developer (or you in the future when you edit the source code) to understand what you were trying to do with the PHP.
  • PHP support single-line as well as multi-line comments. To write a single-line comment either start the line with either two slashes (//) or a hash symbol (#).

PHP Comments(contd.)

<!DOCTYPE html>

<html lang=”en”>

<head>

<title>PHP Comments</title>

</head>

<body>

<?php

// This is a single line comment

# This is also a single line comment echo ‘Hello World!’;

?>

</body>

</html>

PHP Comments(contd.)

  • However to write multi-line comments, start the comment with a slash followed by an asterisk (/*) and end the comment with an asterisk followed by a slash (*/), like this:

<!DOCTYPE HTML>

<html>

<head>

<title>PHP Comments</title>

</head>

<body>

<?php

/*

This is a multiple line comment block that spans across more than

one line

*/

echo “Hello, world!”;

?>

</body>

</html>

Case Sensitivity in PHP

Variable names in PHP are case-sensitive.

PHP echo vs print

  • echo has no return value while print has a return value of 1
  • echo can take multiple parameters (although such usage is rare) while print can take one argument.
  • echo is marginally faster than print.

[pdf_note link=”https://drive.google.com/file/d/17temoFk_mPk9kx1YoeTONOPdaKFgG5Gw/view”]