PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP VariablesPHP ConstantsPHP OutputPHP Data TypesPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Basic Syntax

PHP has really simple syntax.

PHP Tags

  • <?php - Opening Tag
  • ?> - Closing Tag

When PHP parses a file, it searches for opening and closing tags. All the code inside this tags are intepreted. Everything outside this tags are ignored by the PHP parser.

PHP has short opening and closing tags. But, it is not recommended to use them. Always use above tags.

1. Pure PHP

PHP files can have pure PHP code. In this case, you can omit closing tags. This prevents accidental whitespace or new lines being added after the PHP closing tag.

Pure PHP Example

echo 'Hello World'; 

Run Example ››

2. PHP inside HTML

You can use PHP code inside HTML. Here, the closing tag is compulsory. In the below example, the PHP code is made bold.

PHP inside HTML Example

<!DOCTYPE html>
	<title>Hello World</title>
<h1>My First PHP-Enabled Page</h1>

<p><?php echo 'Hello World'; ?></p>


Run Example ››

In this way, PHP tags can be inside any text output like HTML, Javascript, JSON, etc.

PHP Statements

In above examples we used echo statement to output a string. echo is a built-in PHP function. In PHP, each statement or instruction is separated with semicolon (;)

PHP Statement Example

echo 'Hello World'; 

Run Example ››

In the above example,

  • <?php announces the opening of the PHP code.
  • echo says to output the string right after it.
  • ; says to terminate the current statement or instruction
  • ?> announces the ending of the PHP code.

PHP Comments

Good programmers write perfect code with plenty of comments.

Comments are use to understand the code while reading. They can also be used to leave parts from the code. Comments are really important when trying to understand other's or your own code after years. Comments can explain what the code does.

PHP parser ignores all the comments.

There are two ways of commenting

  • Single-Line Commenting - // your comment or # your comment
  • Multi-Line Commenting - /* your comment */

PHP Comments Example


// this is a single-line comment

# this is a single-line comment too

	This is a multi-line comment
	This line is a comment

// to remove a part of code
echo 5 /* + 2 */  + 5;

Run Example ››

PHP Case-Sensitivity

PHP is a SEMI-CASE-SENSITIVE language which means some features are case-sensitive while others are not.

Following are case-insensitive

  • All the keywords (if, else, while, for, foreach, etc.)
  • Functions
  • Statements
  • Classes

All the echo statement below are equal.

PHP Case-Insensitive Example

echo 'Hello World';
ECHO 'Hello World';
eCHo 'Hello World';
Echo 'Hello World';

Run Example ››
PHP Variables are case-sensitive.

In the below example, the second echo statement will throw an error or will output an empty string, because $Name is not defined. (If you don't understand the code, just remember that PHP variables are case-sensitive. We will be discussing more about PHP variables in later tutorials)

PHP Sensitive Example


$name = 'Hyvor Developer';

echo $name;
echo $Name;

Run Example ››