In this part, we will learn about finding root directory in Xampp and writing the first program in PHP by embedding code within HTML. 

If you don’t have Sublime Text installed on your system then click the link below.

       PHP for beginners part 3, installing text editor

 The root directory contains all the files of our project. The files will be stored here and the user can access and execute these files by using his browser.

The process of finding root directory in Xampp is very simple.

Go to C drive of your system.

Finding root directory

Click on xampp directory

Finding root directory2

You will find the root directory, that’s htdocs.

You can also create folders within htdocs to manage the of your project appropriately.

Now click htdocs.

Finding root directory3

Inside htdocs you will find some files, here your can store your own files. You can also create your own folder and store data in it.

In our case, we will create a file with name “test” within htdocs. After saving that file with the .php extension, we will access it from the browser.

For accessing the file we have to specify a proper path. The path follows by me to access the file inside root directory is highlighted with a red line in the screenshot below. You can also see the 

Finding root directory4

 

Note: It is necessary to write http://localhost to access the root document. 

 

Embedding PHP code on a page

 

We will write our code using Sublime Text. If you don’t have Sublime Text installed then click the link given above.

PHP code can be embedded within other programming languages i.e. HTML. 

The PHP has a particular syntax as other programming languages. We have to write the PHP code following that particular syntax.

The basic syntax of  PHP is:

<?php

echo “Hello World”;

?> 

Code written within these starting and closing PHP tags will be considered the part of PHP.

By opening PHP tag, we are saying to apache server here is our PHP code started. The ending tag will inform the apache server about the ending of PHP code.

As I have mentioned above, we can embed PHP code within HTML. It is necessary to inform apache server which part of our code contains PHP. So, we have to put starting and ending tag around PHP code.

Embedding PHP code within HTML

<!DOCTYPE html>
<html>
     <head>
         <title>first</title>
   </head>
<body>
<?php

echo “Hello World”;

?>

</body>
</html>

Output of this code will be same as in above program i.e. Hello World. 

Another important thing then one must keep in mind while saving a PHP file is to end the PHP file with .php extension. When we run our code, apache server recognizes a file on the basis of its extension. The extension tells the apache server for looking at the PHP tag within the file. Thus apache picks the PHP code from the file. If we end the file with .html or some other extension then apache server will not able to read the PHP code. It will just consider the file as an HTML file and output the data only related to HTML.

LEAVE A REPLY

Please enter your comment!
Please enter your name here