Generally, a PHP file contains HTML tags and some PHP scripting code. It is very easy to create a simple PHP example. To do so, create a file and write HTML tags + PHP code and save this file with .php extension.
Note: PHP statements ends with semicolon (;).
All PHP code goes between the php tag. It starts with <?php and ends with ?>. The syntax of PHP tag is given below:
1 2 3 4 5 6 7 8 |
<!DOCTYPE> <html> <body> <?php echo "<h2>Hello World</h2>"; ?> </body> </html> |
How to run PHP programs in XAMPP
How to run PHP programs in XAMPP PHP is a popular backend programming language. PHP programs can be written on any editor, such as – Notepad, Notepad++, Vs Code, etc. These programs save with .php extension, i.e., filename.php inside the htdocs folder.
For example – index.php.
As I’m using window, and my XAMPP server is installed in C drive. So, the path for the htdocs directory will be “C:\xampp\htdocs”.
PHP program runs on a web browser such as – Chrome, Internet Explorer, Firefox, etc. Below some steps are given to run the PHP programs.
Step 1: Create a simple PHP program like hello world.
1 2 3 |
<?php echo "Hello World!"; ?> |
Step 2: Save the file with index.php name in the htdocs folder, which resides inside the xampp folder.
PHP Case Sensitivity
In PHP, keyword (e.g., echo, if, else, while), functions, user-defined functions, classes are not case-sensitive. However, all variable names are case-sensitive.
In the below example, you can see that all three echo statements are equal and valid:
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE> <html> <body> <?php echo "Hello world using echo </br>"; ECHO "Hello world using ECHO </br>"; EcHo "Hello world using EcHo </br>"; ?> </body> </html> |
Output:
Hello world using echo
Hello world using ECHO
Hello world using EcHo
Look at the below example that the variable names are case sensitive. You can see the example below that only the second statement will display the value of the $color variable. Because it treats $color, $ColoR, and $COLOR as three different variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Color</title> </head> <body> <?php $color = "black"; echo "My car is ". $ColoR ."</br>"; echo "My dog is ". $color ."</br>"; echo "My Phone is ". $COLOR ."</br>"; ?> </body> </html> |
Output:
Notice: Undefined variable: ColoR in C:\xampp\htdocs\dkyadav\index.php on line 9
My car is
My dog is blackNotice: Undefined variable: COLOR in C:\xampp\htdocs\dkyadav\index.php on line 11
My Phone is
Only $color variable has printed its value, and other variables $ColoR and $COLOR are declared as undefined variables. An error has occurred in line 9 and line 11.