Thursday, December 26, 2024
Google search engine
HomeLanguagesPHP | Basic Syntax

PHP | Basic Syntax

The structure which defines PHP computer language is called PHP syntax.

The PHP script is executed on the server and the HTML result is sent to the browser. It can normally have HTML and PHP tags. PHP or Hypertext Preprocessor is a widely used open-source general-purpose scripting language and can be embedded with HTML. PHP files are saved with the “.php” extension. PHP scripts can be written anywhere in the document within PHP tags along with normal HTML. 
 

Escaping To PHP:

Writing the PHP code inside <?php ….?> is called Escaping to PHP.

The mechanism of separating a normal HTML from PHP code is called the mechanism of Escaping To PHP. There are various ways in which this can be done. Few methods are already set by default but in order to use few others like Short-open or ASP-style tags, we need to change the configuration of the php.ini file. These tags are also used for embedding PHP within HTML. There are 4 such tags available for this purpose.

Canonical PHP Tags: 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 (;). Let’s look at the hello world program in PHP. 
 

PHP




<?php
# Here echo command is used to print
echo "Hello, world!";
?>


Output: 

Hello, world!

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!

HTML Script Tags: These are implemented using script tags. This syntax is removed in PHP 7.0.0.  So its no more used.  

Example: 
 

PHP




<script language="php">
echo "hello world!";
</script>


 Output:

hello world!

ASP Style Tags: To use this we need to set the configuration of the php.ini file. These are used by Active Server Pages to describe code blocks. These tags start with <% and end with %>

Example: 
 

PHP




<%
# Can only be written if setting is turned on
# to allow %
echo "hello world";
%>


Output:

hello world

Constants:

Constants can be defined using the const keyword or define() function.

There is some difference between constants and variables.

  • Constants do not have $ in front of them like variables have.
  • Constants can be accessed from anywhere without regard to variable scoping rules.

Comments in PHP:

Comments help in reminding the developer about the code if it’s re-visited after a period of time.

A comment is something that is ignored and not read or executed by the PHP engine or the language as part of a program and is written to make the code more readable and understandable. These are used to help other users and developers to describe the code and what it is trying to do. It can also be used in documenting a set of codes or parts of a program. You must have noticed this in the above sample programs. 
PHP supports two types of comment: 
 

  • 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 (#).

Example: 
 

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!!!
  • Multi-line or Multiple line Comment: These are 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!

Case Sensitivity in PHP:

  • PHP is insensitive to whitespace. This includes all types of spaces that are invisible on the screen including tabs, spaces, and carriage returns. Even one space is equal to any number of spaces or carriage returns. This means that PHP will ignore all the spaces or tabs in a single row or carriage return in multiple rows. Unless a semi-colon is encountered, PHP treats multiple lines as a single command.

Example: 
 

PHP




<?php
// PHP code illustrate the whitespace insensitivity
$var1         =     15;
$var2 =
30;
$sum = $var1
+
$var2;
 
// "\n" for new line
echo $sum, "\n";
 
$sum1 = $var1 + $var2;
echo $sum1;
?>


Output:

45
45

Both of them show the same results without any errors.

  • PHP is case-sensitive. All the keywords, functions, and class names in PHP (while, if, echo, else, etc) are NOT case-sensitive except variables. Only variables with different cases are treated differently. Let’s look at this example: 
     

PHP




<?php
// Here we can see that all echo
// statements are executed in the same manner
  
$variable = 25;
echo $variable;
ECHO $variable;
EcHo $variable;
 
// but this line will show RUNTIME ERROR as
// "Undefined Variable"
echo $VARIABLE
?>


Output: 
 

25
25
25

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

This article is contributed by Chinmoy Lenka. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments