This tutorial introduces you to the basics of PHP and includes introduction to PHP, syntax used in PHP and a basic program using PHP.
PHP (Hypertext PreProcessor) is a scripting language used to develop web pages. PHP has been named according to the old version called Personal Page Home. It was developed by Rasmus Lerdoff in 1995. The web pages that end with .php are created using PHP code. It can be embedded in HTML codes for creating advance web pages without using external files.
PHP requires a web server (Apache or Internet Information Service), a database ( MySQL, Oracle, Generic ODBC etc) and the PHP module. PHP code is executed by the web server and the PHP process module, that results in creation of a web page. WordPress, Joomla, Drupal, MediaWiki are some of the softwares that use PHP. It is available free of charge at www.php.net.
A PHP file has an extension as .php that contains html and non-html (PHP) code. A PHP script can be added anywhere in html code.
<?php is an open tag and ?> is a closing tag in every PHP script. There are so many tags used in PHP like that used in html.
Echo or print keyword is used for displaying and works the same way as “cout” in C++.
Eg: echo “ Hello”; will display Hello in the web page.
Similar to C++, // is used for a single line comment and /* */ is used for multiple lines comment.
Eg: // This is a one line comment
/* This is a first line
This ia a second line */
Like any other programming language, PHP also uses variables to store values. PHP variables are case-sensitive and start with a $ sign. They should not contain spaces and can start with a letter or underscore. There is a difference in creating variables here. Like C++, variables are not required to be declared first and used then; in PHP, variables can be declared anywhere in the program whenever the need arises. Moreover, no need to assign data type to the variables, these are automatically understood by PHP according to the value assigned.
For writing a PHP code, you need a text editor. Use notepad for this purpose, write the code and save the file in the webserver directory with an extension .php eg, file.php After saving the file, open the web browser and write http://localhost/file.php, you will get the result.
A simple PHP code is as shown below:
<?php $x = 10; // declares and assigns value to x echo $x; // displays the value of x ?>
This PHP code can be added in html code as follows:
<html> <body> <?php $x = 10; // declares and assigns value to x echo $x; // displays the value of x ?> </body> </html>
Output would be:
Let’s write another program for adding two values;
<?php $x=10; $y=8; $z=$x+$y; echo $z; ?>
Save it as add.php. Type http://localhost/add.php and the output will be:
The advantage of using PHP is that the script can be used in other programs, so no need to write it again. Scripts can be edited for making changes required for some other program. The PHP scripts can be used for creating advance web pages like creating forums, username, password, pictures, surveys etc.
Now that you know the basics of PHP, continue with the next PHP tutorial where you will learn how to use IF-ELSE statements in PHP. Also, if you have any queries, please leave a comment below.