| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | --- | 
					
						
							|  |  |  | title: PHP Syntax and Comments | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | ## PHP Syntax
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | The structure of a PHP document may look something like: | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:50 +01:00
										 |  |  | ```php | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | // Your PHP code goes here. | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | **NOTE:** When creating a document with only PHP, the closing tags (see below) should be omitted. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | When placing PHP in an HTML document, a closing tag is needed, like so: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:50 +01:00
										 |  |  | ```php | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Your PHP code goes here. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ?> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | **NOTE:** Shorthand syntax is also available, but should be avoided to reduce unwanted behavior. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A PHP file may have HTML tags and / or JavaScript. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | The default file extension for PHP files is `.php`. | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## 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: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:50 +01:00
										 |  |  | ```php | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Same level of indendation. | 
					
						
							|  |  |  |     // Not indented like so. | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | ## How to make comments in PHP?
 | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | This can be seen in the example below: | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 10:59:50 +01:00
										 |  |  | ```php | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | <? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | // This is a single-lined comment. | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * This is a multi-lined comment block | 
					
						
							|  |  |  |  * that spans over multiple | 
					
						
							|  |  |  |  * lines. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | ## Additional Information
 | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-16 00:55:08 -04:00
										 |  |  | For more information, please see [PHP: Comments](http://php.net/manual/en/language.basic-syntax.comments.php). |