Updated syntax, corrected and added information. (#19433)
Updated syntax to adhere to PSR, corrected outdated information, and added additional information regarding tags and indentation.
This commit is contained in:
committed by
Quincy Larson
parent
c614c91f8a
commit
cedb5befd9
@ -2,42 +2,65 @@
|
||||
title: PHP Syntax and Comments
|
||||
---
|
||||
|
||||
### PHP Syntax
|
||||
## PHP Syntax
|
||||
|
||||
The structure of a PHP syntax somewhat looks like:
|
||||
The structure of a PHP document may look something like:
|
||||
|
||||
```shell
|
||||
<?php
|
||||
|
||||
// Your PHP code goes here.
|
||||
|
||||
?>
|
||||
```
|
||||
This PHP script can be placed anywhere in the document.
|
||||
A PHP file generally have HTML tags, and some scripting code .
|
||||
The default file extension for PHP files is `.php`.
|
||||
|
||||
### How to make comments in PHP?
|
||||
|
||||
A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code.
|
||||
<?php
|
||||
|
||||
// Your PHP code goes here.
|
||||
```
|
||||
|
||||
In PHP, comments can be make by two ways either single lined or multi-lined.
|
||||
The code snippet given below explains it:
|
||||
**NOTE:** When creating a document with only PHP, the closing tags (see below) should be omitted.
|
||||
|
||||
```shell
|
||||
<?
|
||||
// This is a single-line comment
|
||||
When placing PHP in an HTML document, a closing tag is needed, like so:
|
||||
|
||||
# This is also a single-line comment
|
||||
```
|
||||
<?php
|
||||
|
||||
// Your PHP code goes here.
|
||||
|
||||
/*
|
||||
This is a multiple-lines comment block
|
||||
that spans over multiple
|
||||
lines
|
||||
*/
|
||||
?>
|
||||
```
|
||||
|
||||
### More information
|
||||
**NOTE:** Shorthand syntax is also available, but should be avoided to reduce unwanted behavior.
|
||||
|
||||
For more resources you can visit: https://www.w3schools.com/php/php_syntax.asp
|
||||
A PHP file may have HTML tags and / or JavaScript.
|
||||
|
||||
The default file extension for PHP files is `.php`.
|
||||
|
||||
## Indentation
|
||||
|
||||
While this is mostly personal preference, it is most common to see the lines within the <?php / ?> tags at the same level, like so:
|
||||
|
||||
```
|
||||
<?php
|
||||
|
||||
// Same level of indendation.
|
||||
// Not indented like so.
|
||||
```
|
||||
|
||||
## How to make comments in PHP?
|
||||
|
||||
A comment in PHP code is a line that is not executed as part of the program. Its only purpose is to be read by someone who is looking at the code.
|
||||
|
||||
In PHP, comments can be make by two ways — either single-lined or multi-lined.
|
||||
|
||||
This can be seen in the example below:
|
||||
|
||||
```
|
||||
<?
|
||||
|
||||
// This is a single-lined comment.
|
||||
|
||||
/**
|
||||
* This is a multi-lined comment block
|
||||
* that spans over multiple
|
||||
* lines.
|
||||
*/
|
||||
```
|
||||
|
||||
## Additional Information
|
||||
|
||||
For more information, please see [PHP: Comments](http://php.net/manual/en/language.basic-syntax.comments.php).
|
||||
|
Reference in New Issue
Block a user