PHP introduction

PHP is a server-side programming language that is used to develop web pages mainly.

PHP stands for preprocessor hypertext and developed by Rasmus Lerdorf in 1994.

The current stable release of PHP is 7.4 and the preview release is 8.0.0.

How to install server and PHP?

If you are using a window machine, you can install XAMPP that is the most popular PHP development environment, XAMPP is completely free and easy to install Apache distribution containing MariaDB, PHP, Perl.

download XAMPP from here

After successfully download and install XAMPP on your computer, make sure you have two software also.

  • Code Editor (NOTEPAD, NOTEPAD++, SUBLIME TEXT3)
  • Web browser (Google Chrome, Microsoft Edge, Mozilla Firefox, Opera with an updated version.

After all this stuff you can start to write PHP programs.

When you are writing PHP code make sure to save the file with extension .php and save file at where your XAMPP installed.

mainly it is installed at C: drive with a folder XAMPP.

You have to save files at C:\xampp\htdocs.

Follow all these instructions to create, save, and run the PHP file.

  • Open sublime text3 code editor or other which is your favorite.
  • Write PHP code one of the examples given below.
  • You can create a project folder at htdocs folder like a blog.
  • Now save PHP file(first.php) at blog folder so the path of the file will be C:\xampp\htdocs\first.php
  • Now open one of the favorite web browsers and write localhost/blog/first.php at the browser URL address.
  • You have successfully created, save, and run your first PHP program.

PHP Syntax

<?php
      echo "Hello";
?>

Where <?php is start of PHP, and ?> end of PHP

Hello program in PHP

<?php
     echo "Hello, welcome to PHP";
?>

PHP variables

$ is used in front of the variable to declare a variable.

Example:

<?php
$country="USA";  // String variable

$age=20; // integer variable
$salary=23000.78; // float variable
$areYouHungry=false; // boolean variable
?>

Print PHP statements

<?php
$country="USA"; 

$age=20; 
$salary=23000.78; 
$areYouHungry=false; 
echo "Country: $country";
echo "Age: $age";
echo "Salary: $salary";
echo "are you hungry: $areYouHungry";
?>

PHP comments

  • Comments are used to avoid code to execute.
  • and comments make code more readable.
  • lets others understand your code.

In PHP comments can be written in two ways.

Single line comments

// this is a single-line comment, python interpreter will ignore this line

Multiline comments

/*
      $x=10;
      $y=20;
      $z=$x+$y;
      echo "sum of two number is $z";
*/

Post written by: Hemant Singh
© 2020 - 2020 All rights are reserved