2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: PHP Syntax
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# Basic PHP Syntax
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Start
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								All PHP files are saved by the extension ` .php ` . PHP scripts can be added anywhere in the document. A PHP script starts with ` <?php `  and ends with ` ?> ` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								` <?php   //PHP code goes here  ?> `  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Print
  
						 
					
						
							
								
									
										
										
										
											2018-11-19 22:35:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To print any statement in PHP we use `echo`  command.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Code sample
  
						 
					
						
							
								
									
										
										
										
											2018-10-19 10:59:50 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```php
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								<!DOCTYPE html>  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< html >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< body >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< h1 > My first PHP page< / h1 >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< ?php 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								echo "Hello World!";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								?>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / body >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / html >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								##### NOTE: PHP statements ends with semicolon `;`
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Declaring Variables
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								We declare variables in PHP by adding dollar `$`  sign before them.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-19 10:59:50 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```php
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< ?php 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$x = 5;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								echo $x;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								?>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Comments in PHP
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To write a single line comment in PHP we put hashtag `#`  or by putting `//`  before the comment.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-19 10:59:50 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```php
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< ?php 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# This is a single line comment
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// This is also a single line comment
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								?>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-11-19 22:35:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To write a multiple line comment we start the comment with `/*`  and end with `*/` .
							 
						 
					
						
							
								
									
										
										
										
											2018-10-19 10:59:50 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```php
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< ?php 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* This is a
							 
						 
					
						
							
								
									
										
										
										
											2018-11-19 22:35:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Multiple line comment. */
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								?>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								We can also comment out some parts of the code line.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Code Sample
  
						 
					
						
							
								
									
										
										
										
											2018-10-19 10:59:50 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```php
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								<!DOCTYPE html>  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< html >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< body >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< ?php 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// You can also use comments to leave out parts of a code line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$x = 5 /* + 15 */ + 5;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								echo $x;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								?>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / body >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / html >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can see more about this on [PHP Manual ](http://php.net/manual/en/ )