The Basic PHP Syntax is a set of rules and guidelines to write PHP programs. PHP is a server side backend language used for web development. Building dynamic web pages becomes easy with the use of PHP codes which can be integrated very easily into HTML documents.
PHP tags are used to enclose php codes and help in integration of Php programs within HTML. In this article, let us familiarize ourselves with the basic PHP syntax mentioned below.
Why is PHP Language Used?
PHP or HyperText PreProcessor is used for server side scripting for web applications. This language is used to help developers in building dynamic web applications. PHP is a popular language used in over 75% of web applications.
Basic PHP Syntax: Key Takeaways
- PHP is a server side scripting language used for creating dynamic web pages and applications.
- PHP was created in 1994 and was initially used by developers to create web pages and websites.
- A PHP Script can be placed in the HTML document starting with <?php> and ending with ?>
- PHP-coded statements end with semicolons (;) in the end.
- The default file extension for PHP files is .php
Basic PHP Syntax
The PHP tags are very similar to HTML tags enclosed within special tags. A PHP coded file contains HTML tags as well as PHP Scripting Code. You can also embed PHP using shorthand tags.
<?php
// PHP code goes here ?> |
Shorthand Basic PHP Syntax
The shorthand basic php syntax can also be used to embed PHP codes into HTML documents.
<?= “Hello, World!”; ?> |
History of PHP Language
PHP language was developed in 1994 by Rasmus Lerdorf used as a common gateway interface written in C programming languageIn 1995, Rasmus released the first public version of PHP, called PHP/FI (Personal Home Page/Form Interpreter). This version introduced the ability to process web forms and work with databases like MySQL, marking its initial functionality for dynamic web content generation.
However, PHP/FI had many limitations, including scalability issues and basic syntax, which made it suitable only for small-scale projects.
The turning point came in 1997, when Zeev Suraski and Andi Gutmans, two developers from Israel, rewrote the Basic PHP syntax core to improve its performance and capabilities.
They created what became known as PHP 3.0, officially released in 1998, which introduced a new parser and turned PHP into a robust and extensible scripting language. At this stage, the name PHP was redefined as “PHP: Hypertext Preprocessor”, a recursive acronym. This version gained significant popularity due to its simplicity, integration with databases, and support for various web servers.
In 2000, PHP 4 was released, powered by the new Zend Engine, developed by Zeev Suraski and Andi Gutmans. PHP 4 focused on improving performance, security, and scalability, making it a preferred choice for web developers worldwide.
It supported features like session handling and output buffering, which were essential for building dynamic websites. The release of PHP 5 in 2004 introduced major enhancements, including support for object-oriented programming (OOP), the improved Zend Engine 2, and new extensions like SimpleXML and PDO (PHP Data Objects) for better database handling. This version solidified PHP’s position as a leading web development language.
PHP 7 was released in 2015, brought revolutionary improvements in performance, memory usage, and error handling. With the introduction of the Zend Engine 3, PHP 7 was up to twice as fast as its predecessor. It also added new features like scalar type declarations, return type declarations, and null coalescing operators, making it more modern and developer-friendly. The latest major release, PHP 8, launched in 2020, introduced features like JIT (Just-In-Time) Compilation, named arguments, and enhanced error reporting.
Over the decades, PHP has evolved from a simple tool for personal websites to a powerful language that powers over 75% of websites globally, including platforms like WordPress, Facebook (in its early days), and Wikipedia. Its open-source nature, large community, and constant improvements have made PHP a cornerstone of web development.
Features of Basics of PHP
Below, let us know some of the major features of the PHP language.
- PHP is an open source language available for free. You can run PHP on any operating system like Windows, MacOS, Linux, and more.
- All operations in PHP are carried on the server side sending HTML content to browsers online. Web pages are made dynamic and interactive using PHP command tags.
- PHP is an interpreted language as it executes line by line.
- Basic PHP syntax supports integration with databases easily using PostgreSQL, Oracle, MySQL, MongoDB, etc.
- PHP supports built in functions with data manipulation, time handling, string manipulation, etc.
- It provides session management and security features.
Basic PHP Syntax for Hello World!
We can use simple Basic php syntax to write “Hello World!” on the output screen below.
<!DOCTYPE html>
<html> <body> <h1>First PHP Page </h1> <?php echo “Hello World!”; ?> </body> </html> |
Variables Basic PHP Syntax
The variables in PHP files start with a dollar ($) symbol. The variable name must start with a letter or an underscore. PHP is loosely typed and we do not need to declare data types explicitly.
<?php
$name = “John”; $age = 25; $is_logged_in = true; echo “Name: $name, Age: $age”; ?> |
Comments Used In Basic PHP Syntax
Comments in PHP programs can be added with a simple hashtag symbol. However, multi-line comments in PHP syntax are embedded with “/* .. …. … .. /*”. Let us understand with an example given below.
<?php
// Single-line comment echo “Hello World”; #This will be executed # Another single-line comment # The statements starting with hashtags wont be executed. /* Multi-line comment */ ?> |
Strings In Basic PHP Syntax
Like all other programming languages, PHP also represents strings within double quotes. Single quotes parse the variable inside the quotes as plain text while double quotes take them as strings.
<?php
$name = “Ankit”; echo “Hello, $name!”; // Outputs: Hello, Ankit! echo ‘Hello, $name!’; // Outputs: Hello, $name! ?> |
Basic PHP Syntax: PHP Math functions
There are many PHP math functions that can be used to perform mathematical tasks on numbers. PHP provides pi function, min, max, abs, sqrt, round, and many other math functions in PHP. Let us take an example of basic PHP syntax.
echo (abs (-7.8)); #Absolute Functions
echo (min(0, 20, 30, -90, -8, -200); # minimum function echo (max(0, 20, 30, -90, -8, -200); # max function echo (sqrt(64)); # square root function |
How to Embed PHP Into an HTML Document?
PHP programs can be embedded in an HTML document using simple basic PHP tags. Check the example below, how we are simply embedding PHP into HTML documents with the help of PHP tags.
<!DOCTYPE html>
<html> <body> <h1>First PHP Page </h1> <h2><?php echo “Hello World!”; ?> </h2> </body> </html> |
Basic PHP Syntax: Operators
PHP provides all basic operators used to perform operations on variables. Some of the popular PHP operators are
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Increment/Decrement Operators
- String Operators
- Conditional Assignment Operators
Arithmetic Operators
The Arithmetic operators in PHP are used to perform basic mathematical operations such as addition, subtraction, multiplication, modulus, etc.
Operator | Description | Example | Output |
+ | Addition | 3 + 2 | 5 |
– | Subtraction | 5 – 3 | 2 |
* | Multiplication | 4 * 2 | 8 |
/ | Division | 10 / 2 | 5 |
% | Modulus (remainder) | 7 % 3 | 1 |
Assignment Operators
This operator is used to assign values and perform operations simultaneously.
Operator | Description | Example | Output |
= | Assign value | $a = 10 | $a = 10 |
+= | Add and assign | $a += 5 | $a = 15 |
-= | Subtract and assign | $a -= 3 | $a = 7 |
*= | Multiply and assign | $a *= 2 | $a = 20 |
/= | Divide and assign | $a /= 2 | $a = 5 |
%= | Modulus and assign | $a %= 3 | $a = 1 |
Comparison Operators
This operator in basic PHP syntax is used to compare between two values.
Operator | Description | Example | Output |
== | Equal to | $a == $b | true/false |
=== | Identical (equal and same type) | $a === $b | true/false |
!= | Not equal to | $a != $b | true/false |
!== | Not identical | $a !== $b | true/false |
< | Less than | $a < $b | true/false |
> | Greater than | $a > $b | true/false |
<= | Less than or equal to | $a <= $b | true/false |
>= | Greater than or equal to | $a >= $b | true/false |
<=> | Spaceship operator | $a <=> $b | -1, 0, 1 |
Logical Operator
This operator is used to combine multiple conditions using different operators, such as and, or, xor, etc.
Operator | Description | Example | Output |
&& | Logical AND | $a && $b | true/false |
` | ` | Logical OR | |
! | Logical NOT | !$a | true/false |
and | Logical AND (lower precedence) | $a and $b | true/false |
or | Logical OR (lower precedence) | $a or $b | true/false |
xor | Logical XOR | $a xor $b | true/false |
Learn Web Development with PW Skills
Become an expert web developer with the PW Skills Full Stack Web Development Program. Learn fundamentals of web development, advanced tools, and frameworks in this 6 months live training session.
The complete course consists of in-depth tutorials, practice exercises, module level assignments, real world projects, and more. Learn with the guidance of our dedicated faculty only on pwskills.com
Basic PHP Syntax FAQs
Q1. How is PHP code integrated into an HTML document?
Ans: PHP can easily be integrated with special tags tags. Everything between these tags is treated as PHP code.
Q2. What are the rules for naming variables in PHP?
Ans: Variables in PHP start with the $ symbol.
Variable names must begin with a letter or an underscore (_).
Variable names can contain letters, numbers, and underscores but cannot have spaces or special characters.
Variable names are case-sensitive ($Name and $name are different).
Q3. How can I add comments in the PHP code?
Ans: You can easily add comments using “//…… . … .. .//” for a single-line comment. Add multiple lines of comment using “/*....../*” in PHP code.
Q4. What are the different data types in PHP?
Ans: The data types in PHP are string, integer, float, boolean, array, object, and Null.