The Wayback Machine - https://web.archive.org/web/20240619184538/https://www.geeksforgeeks.org/different-ways-to-write-a-php-code/
Open In App

Different ways to write a PHP code

Last Updated : 05 Mar, 2021
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.


Previous Article
Next Article

Similar Reads

What are the different ways to include CSS Code in HTML ?
In HTML, styles can be incorporated through external linking, where a separate CSS file is connected to the HTML document using the &lt;link&gt; tag in the &lt;head&gt; section, ensuring consistency across multiple pages. Alternatively, internal styling can be employed by placing CSS rules within a &lt;style&gt; tag in the &lt;head&gt; section, sui
1 min read
Write a code to upload a file in PHP ?
In this article, we will understand how to upload a file using PHP. Let us understand some basic configurations. In your "php.ini" file, search for file_uploads and set it to "On". file_uploads = On Approach: First, we create an HTML file in which we make a form so that we can upload the file using the POST method.The $target_dir variable in "fileu
2 min read
Different Ways to Delete an Item From an Array in PHP
Given an array containing some elements, the task is to Delete an item from an array using PHP. Below are the approaches to delete an item from an Array in PHP: Table of Content Using unset() FunctionUsing array_splice() FunctionUsing array_diff() FunctionUsing array_filter() FunctionUsing array_search() and unset() FunctionsUsing a Loop to Remove
3 min read
How to write a form in different line using Bootstrap ?
Bootstrap is an open-source front-end framework for developing standard &amp; decorative websites or web applications that are fast and user-friendly. Bootstrap includes templates based on HTML and CSS for tables, forms, buttons, etc. Bootstrap also uses JavaScript and helps us to create responsive designs. Forms are a crucial building block of eve
2 min read
How to write ReactJS Code in Codepen.IO ?
Now everything is online, some people use VScode to write react.js code and face most of the difficulty. The VScode requires setting for writing React.js code and Many beginners faced difficulty to use VScode so, for them, it is good and easy to use codepen. The codepen provide you with an online platform to create react, HTML, CSS, and JavaScript
2 min read
How to write code using module.exports in Node.js ?
module is a discrete program, contained in a single file in Node.js. They are tied to files with one module per file. module.exports is an object that the current module returns when it is "required" in another program or module. We are going to see a simple code like the calculator to learn how to use module.exports in Node.js. I will walk you thr
3 min read
Redux Toolkit Better way to write Redux code in ReactJS
Redux Toolkit is used for writing redux code but in a more concise way. Redux Toolkit (RTK) solves three bigger problems that most of the developer's face who used redux in a react application. Too much code to configure the store.Writing too much boilerplate code to dispatch actions and store the data in the reducer.Extra packages like Redux-Thunk
4 min read
How to write a simple code of Memoization function in JavaScript ?
Memoization is a programming technique that we used to speed up functions and it can be used to do whenever we have an expensive function ( takes a long time to execute). It relies on the idea of cache {}. A cache is just a plain object. It reduces redundant function expression calls. Let's understand how to write a simpler code of the Memoization
3 min read
How to re-write the code sample without try/catch block ?
Errors almost certainly occur when executing JavaScript code. These problems can arise due to a programming error, incorrect input, or an issue with the program's logic. However, all mistakes may be solved, and to do so, we have different statements but the most commonly used is "try and catch". However, if a method contains code that may produce a
4 min read
How to write a browser-specific code using jQuery ?
In this article, we will see how to write browser-specific code using jQuery. To write the browser-specific code, we will use the Navigator userAgent property and jQuery indexof() method. The Navigator userAgent property is used to get the user-agent header’s value sent to the server by the browser. It returns a string that represents values such a
2 min read