Syntax Of PHP

PHP syntax is simple to learn. PHP code is written using PHP tags. Within these tags <?php (for beginning) and ?> (for ending) is where you'll write a Php code. Every PHP statement is ended with a semicolon (;). Additionally, PHP has operators, functions, loops, arrays, conditions, constants, variables, and comments.

PHP code can be put anywhere on the page. The.php file contains PHP code.It is not possible to run PHP in an HTML file. PHP is a server-side programming language that executes on the server.

After everything has been processed, PHP shows the HTML in a web browser. A PHP script is run on the server, and the plain HTML result is sent to the browser.

PHP Basic Syntax

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

PHP Simple Statement

Here's an example of simple PHP code that works with HTML tags. PHP code begins with a PHP starting tag, then a basic PHP statement to add two numbers, a semicolon to stop the statement, and a PHP ending tag.

This is basic PHP syntax.

<!DOCTYPE html>
<html>
<body>
<h1>Our first PHP Code</h1>
<?php
$result = $num1 + $num2;
?>
</body>
</html>

PHP Variables

Variables are used to store any kind of information in PHP, just like in other programming languages. Variables in PHP are written with a $ at the beginning.


Variables Example

<!DOCTYPE html>
<html>
<body>
<h1>Our first PHP page</h1>
<?php
$name = "CodesBright";
?>
</body>
</html>


In the above piece of code, $name is a variable whose value is CodesBright, and the statement is ended with a semicolon (;). Here you can learn more about PHP variables.

PHP Function

PHP uses functions, like every other programming language, to do the same thing over and over. PHP comes with a wide variety of useful predefined functions; it also allows us to easily create our own.

Below is an example of the built-in PHP echo function. It outputs text and HTML tags. In this example, the text is "CodesBright."

<!DOCTYPE html>
<html>
<body>
<h1>Our first PHP Code</h1>
<?php
echo "CodesBright";
?>
</body>
</html>

PHP Comments

A comment is a piece of information that isn't run, but gives the person reading the code some information. Comments are very helpful, and you should always add them to your code. Comments can tell you what a piece of code is meant to do.

PHP supports many different kinds of comments, such as single-line and multi-line comments. Below is an example of a comment.

<!DOCTYPE html>
<html>
<body>
<?php
// Single-line comment
# Another single-line comment
/*
It is a multiline comment.
It can go as many line as you need
*/
?>
</body>
</html>


A block of code can be commented out to prevent it from being executed. Click here to learn more about the comment.

PHP Case Sensitivity

Because none of PHP's predefined keywords are case-sensitive, you are free to write them with either all caps or all lowercase letters, or even a combination of the two, and the program will continue to function correctly regardless of how they are written.

However, variables are case-sensitive, therefore $num and $Num are two separate variables.