Different ways to write a PHP code - GeeksforGeeks (2024)

Improve

Improve

Like Article

Like

Save

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.

Different ways to write a PHP code - GeeksforGeeks (1)

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

Share your thoughts in the comments

Please Login to comment...

Different ways to write a PHP code - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 6282

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.