Step 2 – Write a PHP program
Before you begin, you need to understand the concept of ‘Document Root’. The ‘Document Root’ is the folder where you keep files for any web-based application developed using a server side scripting language like PHP, for XAMPP on Windows ‘Document Root’ location is C:\xampp\htdocs. It is different in case of a Linux server or an IIS server but those discussions are out of scope for this article.
You’ll also require a Text / HTML / PHP Editor or IDE to write PHP code that supports syntax highlighting. PSPad and Notepad++ are two good Editors available for free download and install, they are easy and simple to use for beginners as well as pros.
Create a new folder under your ‘Document Root’; name it as Bittu (replace with your own name or any name you wish to keep).
Now create your first PHP program file within Bittu folder and name it as test1.php. You can simply create one test1.txt file and rename it as test1.php or you can open a new PHP document within your PSPad or Notepad++ Editor and save it under Bittu folder as test1.php.
<?php // Set display_errors to on for debugging purpose ini_set('display_errors', '1'); echo 'My first PHP Program to subtract one number from the other…'.'<br/>'; $x = 50; $y = 20; if ($x > $y) $sub = $x - $y; else $sub = $y - $x; echo 'Result: '. $sub; ?>
Save the file test1.php and run on browser, type http://localhost/Bittu/test1.php in the address bar and press ENTER. You should see an output like…
My first PHP Program to subtract one number from the other…
<?php and ?> are open and close tags for a PHP file
Any PHP variable starts with a Dollar ($) sign
Each PHP line of code terminates with a semicolon (;)
// or # is used to comment out a line in PHP
PHP is case sensitive
echo is used to print the output
<br/> is a HTML tag for line break
Dot (.) is used for concatenation in PHP
You can learn all PHP syntaxes from PHP Manual.
Go to next page for important definitions.