PHP Syntax

Last Updated : May 3, 2021

In this tutorial, we will learn about how to write a program using PHP syntax. A typical PHP program starts with <?php and ends with ?> expression, PHP parser checks the code enclosed between these two tags and then compiles/executes, returns the output as HTML to the browser.

Different PHP Syntax Examples

1.The below program example contains typical and most common PHP syntax.

<?php
echo "Welcome to PHP Tutorial";
?>

2. In the below example program, we have used a short tag <? ?>, which is not recommended in general for security reasons; however, the below program will run without any issues. To enable/disable short tag in the PHP.ini settings then please check this tutorial for more information.

<?
echo "Welcome to PHP Tutorial";
?>

3. In the below example program, we have used HTML-based syntax to mention specific code block is belongs to the PHP program.

<script language="PHP">
echo "Welcome to PHP Tutorial";
</script>

4. The below program starts with PHP open tag <?php and ends without it. When you write a pure PHP program, then you don’t have to use PHP closing tag ?>. The EOF will automatically terminate the execution of the PHP program.

<?
echo "Welcome to PHP Tutorial";
echo "Again Welcome to PHP Tutorial";

<? echo "Welcome to PHP Tutorial" ?>

Each PHP code statement ends with a semicolon ;, when PHP parser comes to this ; then it’s recognized this is the end of the statement of the current line.

Include PHP inside HTML

PHP language program can be integrated with HTML code block seamlessly wherever needed, and the server executes as it is. Please find below the sample program, which contains HTML and PHP code blocks.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body>  
    <?php  
        echo "Welcome to PHP Tutorial";
    ?>  
  </body>  
</html>

When the above code executed in the server, the HTML output you can see in the browser will be like below.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body>  
    Welcome to PHP Tutorial
  </body>  
</html>

PHP Case-Sensitivity

Is PHP case sensitive? Not fully, yet it is partial case sensitivity. For example, PHP variable names are case-sensitive, but built-in functions and user-defined functions are not. You can learn more about Variables and Functions later in this tutorial series. Please find below some examples to understand the scenarios.

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

// Constants
define('FRUIT', 'Apple');
echo FRUIT .'<br>';
echo Fruit .'<br>';
echo fruit .'<br>';

// Variables
$COLOR = 'Blue';
$Color = 'Red';
$color = 'Green';
echo $COLOR .'='. $Color .'='. $color .'<br>';

// built-in functions
$a = 'sample';
echo strlen($a) .'<br>';
echo STRLEN($a) .'<br>';
?>

Indentation and Coding Standards

PHP is a loosely coupled programming language regarding Indentation, which means you can follow specific indentation in the code block or can do otherwise. Even if you don’t follow the indentation, the PHP program will execute without giving any errors, unlike the Python programming language. There are different coding standards for PHP based on the Frameworks; you will learn more about Coding Standards in the later chapter of this tutorial series.

<?php
$fruit = 'apple';

// with indentation
if(fruit == 'apple') {
    echo "Yes, its Apple <br>";
} else {
    echo "No, it's not <br>";
}

// without indentation
if(fruit == 'apple'){echo "Yes, its Apple <br>";}else{
echo "No, it's not <br>";}
?>

In the above example, with indentation code block and without that will give you the same output. However, we recommend following the Coding Standards in all your PHP programs for better readability, consistency, and understandability.

Please run the above code block examples using our online code editor/compiler and change them to get more ideas. Here we have used echo and comments inside code examples, we will jump deep into those concepts in the upcoming chapter of this tutorial series.