The Wayback Machine - https://web.archive.org/web/20250306125355/https://www.geeksforgeeks.org/php-basic-syntax/
Open In App

PHP Syntax

Last Updated : 05 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

PHP, a powerful server-side scripting language used in web development. It’s simplicity and ease of use makes it an ideal choice for beginners and experienced developers. This article provides an overview of PHP syntax. PHP scripts can be written anywhere in the document within PHP tags along with normal HTML. 

Basic PHP Syntax

PHP code is executed between PHP tags, allowing the integration of PHP code within HTML. The most common PHP tag is <?php … ?>, which is used to enclose PHP code. The <?php ….?> is called Escaping to PHP.

PHP
<?php
    // code
?>

The script starts with <?php and ends with ?>. These tags are also called ‘Canonical PHP tags’. Everything outside of a pair of opening and closing tags is ignored by the PHP parser. The open and closing tags are called delimiters. Every PHP command ends with a semi-colon (;).

Basic Example of PHP

PHP
<?php

// Here echo command is used to print
echo "Hello, world!";

?>

Output
Hello, world!

Embedding PHP in HTML

PHP code can be embedded within HTML using the standard PHP tags. In this example, the <?php echo “Hello, PHP!”; ?> statement dynamically inserts a heading into the HTML document.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP Syntax Example</title>
</head>
<body>
    <h1><?php echo "Hello, PHP!"; ?></h1>
</body>
</html>

SGML or Short HTML Tags

These are the shortest option to initialize a PHP code. The script starts with <? and ends with ?>. This will only work by setting the short_open_tag setting in the php.ini file to ‘on’.

Example: 

PHP
<?
  
// Here echo command will only work if 
// setting is done as said before
echo "Hello, world!";

?>


Output

Hello, world!

Case Sensitivity

PHP is partially case-sensitive-

  • Keywords (like if, else, while, echo) are not case-sensitive.
  • Variable names are case-sensitive.

Example:

PHP
<?php

$Var = "Hello Geeks";

// Outputs: Hello Geeks
echo $Var; 

// Error: Undefined variable $variable
echo $var;

?>

Comments in PHP

Comments are used to make code more readable by explaining the purpose of specific code blocks. Comments are ignored by the PHP interpreter.

1. Single Line Comment

As the name suggests, these are single line or short relevant explanations that one can add to their code. To add this, we need to begin the line with (//) or (#). 

PHP
<?php

// This is a single line comment
// These cannot be extended to more lines

echo "Hello World!";

# This is also a single line comment

?>

Output
Hello World!


Output: 
 

hello world!!!

2. Multi-Line or Multiple Line Comment

It is used to accommodate multiple lines with a single tag and can be extended to many lines as required by the user. To add this, we need to begin and end the line with (/*…*/
 

PHP
<?php

/* This is a multi line comment
   In PHP variables are written
   by adding a $ sign at the 
   beginning.*/

$geek = "Hello World!";
echo $geek;

?>

Output
Hello World!

Variables and Data Types

Variables are used to store data that can be manipulated within PHP. They are declared using the $ symbol followed by the variable name.

Declaring Variables

Variables are created by assigning a value to them using the assignment operator (=).

PHP
$name = "XYZ";  // String
$age = 30;       // Integer
$isEmployed = true; // Boolean

Data Types

PHP supports several data types, including:

  • String: A sequence of characters.
  • Integer: Whole numbers.
  • Float (Double): Numbers with a decimal point.
  • Boolean: Represents true or false.
  • Array: A collection of values.
  • Object: An instance of a class.
  • NULL: A special type representing a variable with no value.
  • Resource: A special type that holds a reference to external resources (like database connections).

Blocks in PHP

In PHP, multiple statements can be executed simultaneously (under a single condition or loop) by using curly-braces ({}). This forms a block of statements that gets executed simultaneously. 

PHP
<?php

$var = 50;

if ($var>0){
    echo ("Positive as \n");
    echo ("greater than 0");
}

?>

Output
Positive as 
greater than 0

Become a Full-Stack Developer and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Next Article

Similar Reads

Explain the benefits of spread syntax & how it is different from rest syntax in ES6 ?
Spread Operator: Spread operator or Spread Syntax allow us to expand the arrays and objects into elements in the case of an array and key-value pairs in the case of an object. The spread syntax is represented by three dots (...) in JavaScript. Syntax: var my_var = [...array]; Benefits of using Spread syntax: 1. It allows us to include all elements
4 min read
How to do HTML syntax highlighting inside PHP strings ?
Syntax highlighting is the work of code editors such as Sublime Text, Visual Studio, Dev CPP, etc, which highlights all the different parts of the source code depending on their syntax by color, modified fonts, or through graphical changes. Since color highlighting these days is integrated into all common editors and development areas. Highlighting
3 min read
How to do syntax checking using PHP ?
Syntax checking is one of the most important tasks in programming. Our compiler checks our code and shows relevant errors if there is any in the code i.e. compile time, run time, syntax, etc. We can do the same thing i.e. syntax checking in PHP. In this article, we are going to learn how we can do syntax checking in PHP. The syntax is basically a s
2 min read
jQuery Syntax
The jQuery syntax is essential for leveraging its full potential in your web projects. It is used to select elements in HTML and perform actions on those elements.jQuery Syntax$(selector).action()Where - $ - It the the shorthand for jQuery function.(selector) - It defines the HTML element that you want to selectaction() - It is the jQuery method us
2 min read
XML | Syntax
Prerequisite: XML | Basics In this article, we are going to discuss XML syntax rule which is used while writing an XML document or an XML application. It is a very simple and straight forward to learn and code. Below is a complete XML document to discuss each component in detail. XML <?xml version="1.0" encoding="UTF-8"?>
3 min read
Explain the arrow function syntax in TypeScript
Arrow functions in TypeScript are implemented similarly to JavaScript (ES6). The main addition in TypeScript is the inclusion of data types or return types in the function syntax, along with the types for the arguments passed into the function.What is arrow function syntax in TypeScript?Arrow functions in TypeScript offer a concise syntax for defin
3 min read
Provide the syntax for optional parameters in TypeScript
In TypeScript, optional parameters allow you to specify that a function parameter may be omitted when calling the function. You denote optional parameters by adding a question mark (?) after the parameter name in the function declaration. Syntax:function functionName(param1: type, param2?: type, param3?: type) { // Function body } Parameters:param1
2 min read
Less.js Extend Syntax & Inside Ruleset
LESS.js is one of the most popular CSS preprocessor languages because of its many features like mixins, imports, variables, and, so on, which help to reduce the complexity of CSS code. One such important and useful feature of LESS is the @extend directive. In this article, we will see the basic usage of the extend feature in LESS.js, along with kn
2 min read
JavaScript Spread Syntax (...)
The spread syntax is used for expanding an iterable in places where many arguments or elements are expected. It also allows us the privilege to obtain a list of parameters from an array. The spread syntax was introduced in ES6 JavaScript. The spread syntax lists the properties of an object in an object literal and adds the key-value pairs to the ne
4 min read
How to Hide the VueJS Syntax While the Page is Loading ?
Vue.js is a JavaScript framework used in building powerful and elegant user interfaces. In this article, we will learn how to hide the VueJS syntax while the page is loading. This approach ensures that users don't see the uncompiled Vue.js syntax during the initial page load, providing a smoother user experience. The following approaches can be use
3 min read