Starting With PHP - Installation, Syntax, Variables, Constant

In this article, we are going to see this article from a beginners’ point of view.
 

WHAT?

 
PHP is the abbreviation of “Hypertext Processor”. This is an open-source scripting language which is completely free and very popular for executing server-based applications. By using this scripting language, we can make dynamic as well as interactive web pages.
 
Facebook is the best example of PHP. So, in brief, we can say that PHP is the Server Side Programming Language.
 

WHY?

 
PHP can run on different platforms like Windows, Linux, Unix, Mac OSx etc. PHP is suitable with the modern servers, such as Apache, IIS and many more. Not only these but PHP supports huge databases too.

SET UP PHP ON YOUR PC

 
Download from the official PHP resource: www.php.net
 
However, if your server does not support PHP, go through this.
  • Install a web server
  • Install PHP
  • Install a database, MySQL
The official PHP website (www.php.net) contains the instructions to install PHP - http://php.net/manual/en/install.php
 

HOW TO WRITE PHP SCRIPT

 
NOTE The programmer should have basic knowledge of HTML, CSS, and JavaScript.
 
SYNTAX 1
 
We will move on with the latest version of PHP 7 which is quite popular and compatible among the end-users.
 
We will start scripting in PHP like <?php and ends with ?>
  1. <?php  
  2.    // Code over here  
  3. ?>   
Let’s have an example.
  1. <html>  
  2. <body>  
  3.   
  4.    <?php  
  5.       echo "My first Blog!";  
  6.    ?>  
  7.   
  8.    //Output is My first Blog!   
  9.   
  10. </body>  
  11. </html>  

echo

 
This is the inbuilt function of the PHP script, which is used to print the output text. We can say that it is a language construct.  
 
Remember that a PHP statement ends with a semicolon (;).
 
This is standard type of scripting in PHP.
 
SYNTAX 2
 
With using HTML <script> tag.
 
Let’s have a quick look with examples.
  1. <html>  
  2. <body>  
  3.   
  4.    <script language=”php”>  
  5.       echo "My first Blog!";  
  6.    </script>  
  7.    //Output is My first Blog!  
  8.   
  9. </body>  
  10. </html>  
NOTE
In PHP 7, this is not applicable.
 
SYNTAX 3
 
Shorthand PHP tag <? ?>,
  1. <?  
  2.    echo “My first Blog!”;  
  3. ?>  
  4. //Output is My first Blog!  

Comments

 
Adding comments to our script helps to understand our logic and processes. It is a good practice to keep comments in scripts.
 
Single Line - //
 
Example
  1. //This is single line comment  
  2. Multi Line: /*      */  
Example
  1. /* This is  
  2.                    multi line  
  3.                    comment (block)*/  
You can write comments anywhere in PHP Scripting code.
 
Components of PHP File
  • PHP files can includes text, HTML, CSS, JavaScript, and PHP code
  • PHP codes are executed on the server, and the output is returned to the browser as plain HTML
  • PHP files have the extension ".php"
Case sensitive
 
PHP is not case-sensitive. Only variable names are case sensitive in PHP.
 

How do variables play their role in PHP Scripting?

 
Variables are like ‘containers’ which contain information. A PHP variable always starts with the $ sign.
 
Example
  1. $var_name=value;  
Remember that a variable name starts with a letter or _underscore only, not with a number. Only alpha-numeric characters and underscores (A-z,0-9,_) are included in the variable name. If you are using $var_name and $Var_Name, both are considered as different variables. There is a great advantage, which is that we don’t need to declare data type, like other programming languages, it automatically converts the variable according to assigned value.
 
Example
  1. <?php  
  2.   $name=’dipa’;  
  3.   $age=’28’;  
  4.   echo $name;   
  5. ?>  
  6. //Output is dipa  

CONSTANTS

 
Constant is a value which cannot be modified or changed once it is declared. It plays a role, the same as variables.
 
To create a constant in PHP scripting we have to use a function called define(). 
 
Syntax
 
define(name,value,case-sensitive)
 
There are major three parameters.
  • name - Declaration of constant name.
  • value - Specify the value of constant.
  • Case-sensitive - It generates Boolean values: TRUE or FALSE. Default is False.
Example
  1. <?php  
  2.     define(“Message”, ”Hi Dipa Here”, true);  
  3.    echo message;  
  4.    //Output is Hi Dipa Here  
  5. ?>  


Similar Articles