Open In App

PHP Syntax

Last Updated : 05 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
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


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: