PHP

PHP Menu

The PHP script can be embedded within HTML web pages. The script starts with <?php and ends with ?>.

Note that when dealing with pure PHP files, the closing tag (?>) is not required. It is only required when the PHP script is embedded in an HTML document.

<?php

// Your PHP code here

?>
<!DOCTYPE html>
<html>
<body> 
...

Below is a very typical example of a simple code written in PHP. The script uses the echo function, which simply displays text on a web page:

<!DOCTYPE html>
<html>
<body>            
    <h1>My first PHP page</h1><br>
    <?php echo "Hello World!"; ?>
</body>
</html>

Note that PHP statements end with a semicolon (;).

PHP Comments

A comment is just text that the PHP engine ignores. The objective of comments is to make the code easier to read. It may aid another developer (or you in the future when you modify the source code) in understanding what you were attempting to do using 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
// This is a single line comment # This is also a single line comment echo "Hello, world!";

For multi-line comments, start the comment with a slash followed by an asterisk (/*) and end the comment with an asterisk followed by a slash (*/):

<?php
/* This is a multiple line comment block that spans across more than one line */ echo "Hello, world!";

Case Sensitivity

Variable names in PHP are case-sensitive. As a result the variables $color, $Color and $COLOR are treated as three different variables.

<?php
// Assign value to variable $color = "yellow";
// Try to print variable value echo "The color of my car is " . $color . "<br>"; echo "The color of my car is " . $Color . "<br>"; echo "The color of my car is " . $COLOR . "<br>";

However, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.

<?php
ECHO "Hello World! <br>"; echo "Hello World! <br>"; EcHo "Hello World! <br>";

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods