Different Ways to Write PHP Code
PHP, which stands for Hypertext Preprocessor, is a widely-used server-side scripting language designed for web development. Created by Rasmus Lerdorf, PHP is instrumental in building dynamic web applications and interactive websites. It plays a crucial role in bridging the front-end and back-end by connecting to databases.
Typically, to run a PHP program, you need a web browser. Here’s a step-by-step guide:
Write Code in Notepad:
- Write your PHP code in a simple text editor like Notepad.
- Save the file with a
.php
extension. For instance,thefullstackcide.php
.
Save the File in XAMPP Location:
- If XAMPP is installed on your local disk (C:), save the file in
C:\xampp\htdocs
.
- If XAMPP is installed on your local disk (C:), save the file in
Start XAMPP and Run Your Code:
- Open the XAMPP control panel.
- Start the Apache and MySQL servers.
- Open any web browser and type
localhost/filename.php
in the address bar. - If your PHP file is in a subfolder within
htdocs
, uselocalhost/subfolder_name/filename.php
.
Methods to Write PHP Code
PHP code can be written in three primary ways:
1. Without Any HTML Markups :
This method involves writing pure PHP code without embedding it in HTML.
$a = 15;
$b = 5;
$c = $a * $b;
echo "The product of a and b is " . $c;
Output :
The product of a and b is 75
2. Embedding HTML Markups in PHP Code :
In this method, HTML elements are generated using PHP.
";
echo "PHP Example ";
echo "";
echo "Welcome to PHP Programming
";
echo "This is a paragraph generated by PHP.
";
echo "";
echo "