Open In App
Related Articles

Different ways to write a PHP code

Improve Article
Save Article
Like Article

The full form of PHP is a Hypertext preprocessor. It was developed by Rasmus Lerdorf. It is a programming language for developing dynamic web applications and interactive websites. It is a server-side scripting language. It is used for establishing a connection between the front-end and database i.e., back-end.

Ways to run a PHP program:

  • Generally, we need a web browser to run a PHP program.
  • Write code in notepad.
  • Save the file with any name followed by a .php extension.
  • For example, Geeksforgeeks.php.
  • Save the file in the XAMPP location of the file structure.
  • If the XAMPP is installed in local disk C, then we have to save the file in C:\xampp\htdocs.

Like this we have to save the PHP file 

After saving your code file into the htdocs folder follow the below steps.

  • Open XAMPP control panel.
  • Start Apache and MySQL servers
  • Go to any browser and type localhost/filename.php in the search box.
  • If you save your PHP code in a subfolder in htdocs, then type localhost/subfolder_name/filename.php

A PHP code can be written in 3 ways

  1. Without any HTML markups
  2. Embedding HTML markups in PHP code
  3. Embedding PHP code in HTML.

1. Without any HTML markups:


    echo("The addition of a and b is ". $c);

Output :  

The addition of a and b is 20

2. Embedding HTML markups in PHP code:


    echo "<html>";
    echo "<h1> welcome </h1>" ;
    echo "</html>" ;



3. Embedding PHP code in HTML:


    echo "Your first PHP code"


 Your first PHP code 

The most proficient way:

  • All three are most proficient.
  • They may be implemented based on the required work.
  • For example, if we want to develop only front-end pages then we can directly use HTML instead of embedding PHP with HTML.
  • If we want to develop dynamic web pages and which are using back-end databases then we can embed HTML with PHP.
  • Even though if we write PHP code in HTML, we are going to save the file with the .php extension only.
  • Because the web pages should interact with the database which is present in XAMPP.
  • To run a PHP page we need XAMPP. If we use XAMPP as a server we need to save our code with filename followed by .php extension only.
  • All methods are proficient only. We can use any method based on our present work.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 05 Mar, 2021
Like Article
Save Article
Similar Reads
Related Tutorials