Example of PHP

PHP example

Note: In this site, I am using XAMPP server and in windows operating system and installed XAMPP in C drive, so make sure you install XAMPP server.


Run PHP program in XAMPP server

Step by step run PHP program is given below

  1. Go into HTDOCS folder and create your folder name like "first_program", and into folder create a file give a name "welcome.php"
  2. "welcome.php" file open in any editor you like.
  3. Start the XAMPP server and start Apache and MySQL.
  4. Below code write into "welcome.php".
  5. PHP script that uses a built-in PHP function "echo" to output.
  6. <!DOCTYPE html>
    <html>
    <body>
    
    <h1>My first PHP page</h1>
    
    <?php
    echo "Hello World! ";
    ?>
    
    </body>
    </html>
    
  7. See output in web browser like Chrome, Microsoft Edge, Internet Explorer, Firefox, etc.
  8. seacrch localhost/your_folder_name = http://localhost/first_program/welcome.php.


PHP Case Sensitivity
  • In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.
  • In the example below, all three echo statements below are equal
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    ECHO "Hello World!<br>";
    echo "Hello World!<br>";
    EcHo "Hello World!<br>";
    ?>
    
    </body>
    </html>
    
    

    Note: In PHP, all variable names are case-sensitive!


  • Look at the example below; only the first statement will display the value of the $color variable! This is because $color, $COLOR, and $coLOR are treated as three different variables:
<!DOCTYPE html>
<html>
<body>

<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My bike is " . $coLOR . "<br>";
?>

</body>
</html>

Output:

My car color is red

Notice: Undefined variable: COLOR in C:\xampp\htdocs\example\p6.php on line 8
My house color is

Notice: Undefined variable: coLOR in C:\xampp\htdocs\example\p6.php on line 9
My bike color is

Note: In above example,my house is and my bike is name was not show because "Undefined variable : $COLOR, $coLOR"