Vaidikalaya

PHP Syntax


PHP is a server-side scripting language so it is executed on the server, and the plain HTML result is sent back to the browser.


Where to put PHP files :

If you have XAMPP or WAMPP server then you have to put your file in xampp/htdocs folder of the drive in which Xampp is installed.
I have installed the Xampp server in c drive so I will put my files in c:/xampp/htdocs.


Extension of PHP File :

The default file extension for PHP files is ".php".


Syntax of writing PHP code :

A PHP script always starts with an open PHP tag (<?php) and ends with an end tag (?>)This is the most universally effective PHP tag that starts with <?php and ends with ?>.

<?php
 
//Write php code here
?>


Text Editor needs for PHP: 

PHP programs can be written on any editor, such as - Notepad, Notepad++, Visual Studio Code and Sublime, etc.


How to write PHP code:

Step 1: Open the text editor which you want to use for PHP code. I am using notepad for writing this first code.

Step 2: Write code for printing "Hello Vaidikalaya".

<?php
    echo "Hello Vaidikalaya";
?>



Step 3: Now save this file in htdocs folder with a .php extension.




Step 4: Now run this code:

For running PHP files on the browser, firstly run your Xampp server and then open your browser and type localhost/filename.php which you want to run.

So for running this file, open the browser and type: localhost/first_code.php



Run PHP code from a separate folder within htdocs folder:

For good practice and better code structure, I recommend to you that you can make a separate folder within htdocs folder and put your practice files here. And for running these files you simply type localhost/folder_name/file_name.php

Example: 

I created one folder (php-tutorials) in htdocs directory and after that, I created one file program1.php within php-tutorials folder and write some code in this file.





Now run this file on the browser: localhost/folder name/file name

Ex: localhost/php-tutorials/program1.php



NOTE: 
When you type localhost on the browser it points to the htdocs folder and then whatever file or folder name you have given then it runs those files or folders.