As we all know that PHP is a server-side scripting language that’s why it needs a server to execute a program. In the previous tutorial, we learned how to set up XAMPP and install PHP in the local system because we don’t spend money to learn or develop a program. XAMPP is free of cost that helps to execute the PHP program. Let’s see how to run a PHP program?
How to run a PHP program or how to write first PHP program?
In this tutorial, first, we will learn how to write a PHP program and then save a program and execute it on browser to see the output.
When we are beginners in PHP or any GUI based programming language then we are little bits confused that set up any software to create custom software or application will be hard but it is very easy if you are interested otherwise it will hard for everyone who doesn’t have interest in developing custom software.
Some important points you must know before writing a PHP program:
- Keywords, function, and class names are not case sensitive but variable names are case sensitive. Case sensitive means differentiating between capital and lower letters.
- PHP file extension must be (.php). You can run PHP code in another extension file but you have to configure it. Here, I am not configuring any other setup because we will execute the PHP code in the .php extension.
- PHP statement always terminated by a semicolon (;).
- You have to write a PHP code between PHP starting and ending syntax. PHP starting syntax is <?php and ending syntax is ?>
- You don’t require to write ending syntax in the pure PHP file. Pure PHP files are those files which have only PHP codes.
- To execute PHP code on the server, you need PHP parser. XAMPP provides PHP parser.
First Pure PHP program
You can write a PHP program in any text editor like notepad, Notepad++, Sublime, and many more.
<?php echo "Hello World!"; ?>
This is our first PHP program from where we are starting our PHP learning process. In the above code, I have written starting PHP syntax (<?PHP) and then written a “Hello World” string with echo keyword. Echo keyword prints a string on the output screen. And lastly, I have written closing PHP syntax but here, we didn’t require closing syntax (?>) because it is a pure PHP file.
Where should I save the PHP program and how to execute it?
Now, the time to find the location to save a PHP program. In this tutorial series, we have installed XAMPP in Windows operating system. Therefore, I am going to save our PHP program inside C:\xampp\htdocs drive.
Steps to save a PHP program:
- First, create a folder inside C:\xampp\htdocs Here I am creating a folder named with php_example so that I can keep all files related to a project in one folder.
- Now, write your PHP code in a text editor and click on save option according to your text editor or by Ctrl + s keyboard shortcut.
- Now, select your destination folder in htdocs and save your file with any name but file name extension must be .php.
- Here, I am going to save my file with index.php name.
Steps to run PHP program in browser
To run PHP programs in local system, you must have Apache or any other server which supports PHP. In XAMPP, there is an Apache server. Let’s see steps to run a PHP program in browser:
- First, start the XAMPP control panel by clicking on the start button and then find the XAMPP control panel.
- Next, start the Apache server by clicking on the start button in-front of Apache. If the Apache server get successfully started then the start button will be automatically replaced with stop button.
- Next, open any web browser. Here, I am going to open a chrome web browser.
- Type http://localhost/project_folder_name/filename.php replace project_folder_name with your project folder name if any and filename.php should be replaced with your file name which you want to access.
- Here, I am going to type http://localhost/php_example/index.php. You can access the index.php file without typing the file name. It will also access through http://localhost/php_example/
- Now, you will be able to see the output of your code in a web browser.
How to write HTML with PHP?
Now, I am going to share how to write HTML in the PHP file. I am writing code for displaying some text on a web browser via PHP and HTML.
<!DOCTYPE html> <html> <head> <title>PHP with HTML</title> </head> <body> <!-- This is simple HTML --> <h1>Hi, Welcome to PHPTreePoint</h1> <!-- This is heading tag printing with PHP --> <?php echo '<h1>Hi All, How are you?</h1>'; ?> <!-- This is heading tag printing with HTML and PHP --> <h2><?php echo 'Hi Friends, Are you beginners in PHP?'; ?></h2> </body> </html>
In the above code, I have a simple H1 heading tag without any PHP code. In the second line of heading, I am printing HTML heading with PHP and in the third heading, I am printing text by PHP in an HTML heading.