We are going to learn basic syntax to write simple program in PHP and how to execute it. We will also learn some more basics of PHP scripting language in this PHP BASICS tutorial.
PHP scripts are executed on server and the output is sent in the form of plain text to browser.
- Some basic Concepts:
- PHP programs are written in simple text editors like notepad++.
- These programs contain HTML tags and some PHP code in it or they can contain only PHP logic.
- When PHP code is embedded in HTML file, it can be written in head section as well as body section using the PHP script tag
- If the PHP code is written in a separate file, then that PHP code can be used into the HTML file by linking them to each other.
- PHP code can be written anywhere in the program using the PHP script tag. i.e.
- The PHP script tags starts with
and ends with
. The ending tag (?>) is optional.
- The PHP code is written in between these start and end tags.
- PHP programs are saved with .php extension.
- Each line of code in the program is known as a statement.
- Every statement in PHP code is terminated with semicolon (;).
- The ending PHP script tag (?>) also implies a semicolon, so it doesn’t matter if you don’t terminate the last statement of PHP code with a semicolon (Remember the last statement which is before the ending PHP script tag (?>).
- Writing a simple PHP program:
- Let us write a simple PHP program that welcomes us to learn PHP.
- Open your notepad++ document and save it with a name index.php on desktop. While saving select the Save as type as PHP Hypertext Preprocessor file.
- Write the following code in this index.php file:
Some basic concepts useful in writing PHP programs are as follows:
<!DOCTYPE html> <html> <head> <title> Simple PHP Program </title> </head> <body> <h1>PHP</h1> <?php echo "WELCOME TO PHP COURSE."; ?> </body> </html>
echo "WELCOME TO PHP COURSE.";
echo “some text”;
echo ‘some text’;
- We all remember that our program index.php is saved on the desktop.
- Let us see if it prints WELCOME TO PHP COURSE.
- Run the file index.php with Google Chrome as shown below:
- Go to Run menu and click on Launch in Chrome option in the dropdown menu or else you can simply press Ctrl+Alt+Shift+R shortcut key.
- The index.php page will open in the browser as shown below:
- Here, the output shows only the text PHP which was enclosed in the h1 heading tag.
- The code enclosed inside PHP script tag is not at all executed. We can conclude this because the statement WELCOME TO PHP COURSE is not displayed in the above output.
- We had learned to install xampp server in the last lesson and we had successfully installed it.
- Copy the index.php file from desktop.
- Now in our C drive we have a folder named xampp. Open that folder.
- Search for htdocs folder in the xampp folder and open it.
- Create a new folder named Simple_PHP in htdocs folder and paste the copied index.php file inside it.
- Now open your browser and type localhost/Simple_PHP in the address bar of the browser.
- Remember the file having name index always gets executed automatically as soon as the folder name containing it is given in the address bar of the browser. In this example we have provided the folder name Simple_PHP which automatically calls index.php.
- You will see the following output:
- Here, you got the PHP code output.
- This indicates that PHP code files require a server for execution.
- Write the program.
- Save it with .php extension.
- Create a new folder in htdocs folder which is inside xampp folder in C drive.
- Copy the program file to be executed and paste it inside the newly created folder inside htdocs folder.
- Open the browser and write localhost/newly_created_folder_name in its address bar.
- Comments in any language are the statements that are not read or not executed.
- These are just for information of the programmer.
- Comments can be used by the programmer in a program to understand his program after a long period of time. I mean if the programmer had written a program and again wants to review it after 6 months or a year. He can understand the program if the purpose of the code is written in the form of comments.
- A programmer can also write comments to remember the work he wants to do further in a project.
- Comments can also be used to test the project. I mean, a programmer can test some part of code by commenting the other.
- There are two types of comments in PHP.
- Single line comment
- Multiple line comment
<!DOCTYPE html> <html> <head> <title> Simple PHP Program </title> </head> <body> <h1>PHP</h1> <?php echo "WELCOME TO PHP COURSE. <br><br>"; echo ‘This is my first PHP program. <br><br>’; echo "PHP is a server side scripting language.<br>"; echo 'Every statement in PHP should terminate with a semicolon.'; ?> </body> </html>
<?php //echo "WELCOME TO PHP COURSE.<br><br>"; echo 'This is my first PHP program. <br><br>';#echo is used to print a statement. /* echo "PHP is a server side scripting language.<br>"; echo 'Every statement in PHP should terminate with a semicolon.'; */ ?>
Rest of the basic things in PHP are its variables, data types, keywords, loops, functions etc. We will study each of them in detail in further sections.
Thus we studied how to write and execute a PHP program in this PHP Basics tutorial.