your image

PHP Example - javatpoint

javatpoint
Related Topic
:- PHP

How to run PHP code in XAMPP

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. <?php   
  2. //your code here  
  3. ?>  

Let's see a simple PHP example where we are writing some text using PHP echo command.

File: first.php

 

 

3. PHP Inheritance | Build a CMS using OOP PHP tutorial MVC [2020]

  1. <!DOCTYPE>  
  2. <html>  
  3. <body>  
  4. <?php  
  5. echo "<h2>Hello First PHP</h2>";  
  6. ?>  
  7. </body>  
  8. </html>  

Output:

 

Hello First PHP

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++, Dreamweaver, etc. These programs save with .php extension, i.e., filename.php inside the htdocs folder.

For example - p1.php.

As I'm using window, and my XAMPP server is installed in D drive. So, the path for the htdocs directory will be "D:\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. <?php      
  2.     echo "Hello World!";  
  3. ?>  

Step 2: Save the file with hello.php name in the htdocs folder, which resides inside the xampp folder.

Note: PHP program must be saved in the htdocs folder, which resides inside the xampp folder, where you installed the XAMPP. Otherwise it will generate an error - Object not found.

Step 3: Run the XAMPP server and start the Apache and MySQL.

 

Step 4: Now, open the web browser and type localhost http://localhost/hello.php on your browser window.

Step 5: The output for the above hello.php program will be shown as the screenshot below:

Most of the time, PHP programs run as a web server module. However, PHP can also be run on CLI (Command Line Interface).

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. <!DOCTYPE>  
  2. <html>  
  3.     <body>  
  4.         <?php  
  5.             echo "Hello world using echo </br>";  
  6.             ECHO "Hello world using ECHO </br>";  
  7.             EcHo "Hello world using EcHo </br>";  
  8.         ?>  
  9.     </body>  
  10. </html>  

Output:

Hello world using echoHello world using ECHOHello 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. <html>  
  2.     <body>  
  3.         <?php  
  4.             $color = "black";  
  5.             echo "My car is ". $ColoR ."</br>";  
  6.             echo "My dog is ". $color ."</br>";  
  7.             echo "My Phone is ". $COLOR ."</br>";  
  8.         ?>  
  9.     </body>  
  10. </html>  

Output:

 

Notice: Undefined variable: ColoR in D:\xampp\htdocs\program\p2.php on line 8My car isMy dog is blackNotice: Undefined variable: COLOR in D:\xampp\htdocs\program\p2.php on line 10My Phone is

Comments