PHP Syntax

PHP Syntax

PHP Syntax is very simple. PHP tags are used to write PHP code. A PHP script or PHP code is written inside php starting tag <?php and php ending tag ?>. Each PHP statement ends with semicolon (;). PHP also has variables, constants, conditions, loops, arrays, operators, functions and comments.

You can place your PHP Code anywhere on the page. PHP is written in .php file. PHP cannot run in .html file. PHP is a server side language that runs on server.

PHP displays the HTML on web browser after processing everything as a result. A PHP script is executed on the server, and the plain HTML result is sent back to the browser.

Basic PHP Syntax

<?php   //php starting tag
// PHP code goes here
?>     //php ending tag

Simple PHP Statement

Below is the working example of simple PHP code with HTML Tags. PHP code starts with php starting tag, then there is simple php statement to add two numbers, then there is semicolon to execute or end the statement and then in the end there is php ending tag.

This is simple PHP syntax.

<!DOCTYPE html>
<html>
<body>
<h1>My first PHP Code</h1>
<?php
 $a=$b + $c;
?>
</body>
</html>

Variables in PHP

Like other programming languages PHP also have variables, variables are used to store any information. PHP variables are written with the dollar sign $ at the start.

example:

<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
$name="How to Code School";
?>
</body>
</html>

In the above line of code, $name is a variable, How to Code School is it's value and semicolon ( ; ) is used to end the statement. Read more about PHP Variables here.

Functions in PHP

Functions like all other programming languages are used in PHP to execute one task repeatedly. There are many built-in functions in PHP but we can also make our own functions.

Below is the simple built-in function echo, which is used to output the text or HTML tags. In this case it's simple text "HOW TO CODE SCHOOL".

<!DOCTYPE html>
<html>
<body>
<h1>My first PHP Code</h1>
<?php
echo "HOW TO CODE SCHOOL";
?>
</body>
</html>

Comments in PHP

Comment is just a piece of information that is not executed but it just provides some information to the reader of the code. Comments are very helpful and it's always suggested to write comments with your code. Comments can tell you about the purpose of a particular piece of code.

PHP supports many types of comment, single-line comment, multi-line comment, below is the example.

<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that can go 
up to many lines.
*/
?>
</body>
</html>

You can also comment out piece of code and it won't be executed. Read more about the comment here.

Case Sensitivity in PHP

In PHP all pre defined keywords are not Case Sensitive, which means that you can write them in upper case or lower case or both combined and it will still work fine.

example: echo, ECHO and eCho all three will work fine.

But variables are case sensitive which means that $name and $Name are two different variables.