Open In App

Different ways to write a PHP code

Improve
Improve
Like Article
Like
Save
Share
Report

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:

PHP




<?php
    $a=10;
    $b=10;
    $c=$a+$b;
    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:

PHP




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


Output:  

welcome

3. Embedding PHP code in HTML:

PHP




<html> 
<body> 
<?php 
    echo "Your first PHP code"
?> 
</body>
</html>


Output:

 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.


Last Updated : 05 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads