How to run PHP code in XAMPP

Last updated on May 31 2022
Aridam Das

Table of Contents

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

  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:

17

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 black Notice: Undefined variable: COLOR in D:\xampp\htdocs\program\p2.php on line 10My 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 5 and line 7.

PHP – Syntax Overview

This chapter will give you an idea of very basic syntax of PHP and very important to make your PHP foundation strong.

Escaping to PHP

The PHP parsing engine needs a way to differentiate PHP code from other elements in the page. The mechanism for doing so is known as ‘escaping to PHP’. There are four ways to do this −

Canonical PHP tags

The most universally effective PHP tag style is −

<?php...?>

If you use this style, you can be positive that your tags will always be correctly interpreted.

Short-open (SGML-style) tags

Short or short-open tags look like this −

<?...?>

Short tags are, as one might expect, the shortest option You must do one of two things to enable PHP to recognize the tags −

  • Choose the –enable-short-tags configuration option when you’re building PHP.
  • Set the short_open_tag setting in your php.ini file to on. This option must be disabled to parse XML with PHP because the same syntax is used for XML tags.

ASP-style tags

ASP-style tags mimic the tags used by Active Server Pages to delineate code blocks. ASP-style tags look like this −

<%...%>

To use ASP-style tags, you will need to set the configuration option in your php.ini file.

HTML script tags

HTML script tags look like this −

<script language = "PHP">...</script>
Commenting PHP Code

A comment is the portion of a program that exists only for the human reader and stripped out before displaying the programs result. There are two commenting formats in PHP −

Single-line comments − They are generally used for short explanations or notes relevant to the local code. Here are the examples of single line comments.

<?   
# This is a comment, and   
# This is the second line of the comment      
// This is a comment too. Each style comments only   
print "An example with single line comments";
?>

Multi-lines printing − Here are the examples to print multiple lines in a single print statement −

<?   
# First Example  
print 
<<<END   
This uses the "here document" syntax to output   
multiple lines with $variable interpolation. 
Note   that the here document terminator must appear on a   
line with just a semicolon no extra whitespace!   END;     
# Second Example   print "This spans   
multiple lines. The newlines will be   output as well";
?>

Multi-lines comments − They are generally used to provide pseudocode algorithms and more detailed explanations when necessary. The multiline style of commenting is the same as in C. Here are the example of multi lines comments.

<?   
/* This is a comment with multiline      
Author : Mohammad Mohtashim      
Purpose: Multiline Comments Demo      
Subject: PHP   */      

print "An example with multi line comments";
?>

PHP is whitespace insensitive

Whitespace is the stuff you type that is typically invisible on the screen, including spaces, tabs, and carriage returns (end-of-line characters).

PHP whitespace insensitive means that it almost never matters how many whitespace characters you have in a row.one whitespace character is the same as many such characters.

For example, each of the following PHP statements that assigns the sum of 2 + 2 to the variable $four is equivalent −

$four = 2 + 2; 
// single spaces$four <tab>=<tab2<tab>+<tab>2 ; 
// spaces and tabs$four =2+2; 
// multiple lines

PHP is case sensitive

Yeah it is true that PHP is a case sensitive language. Try out following example −

  <html>

   <body>            
<?php         
$capital = 67;         
print("Variable capital is $capital<br>");         
print("Variable CaPiTaL is $CaPiTaL<br>");      
?>         
</body>
</html>

This will produce the following result −

Variable capital is 67Variable CaPiTaL is

Statements are expressions terminated by semicolons

A statement in PHP is any expression that is followed by a semicolon (;).Any sequence of valid PHP statements that is enclosed by the PHP tags is a valid PHP program. Here is a typical statement in PHP, which in this case assigns a string of characters to a variable called $greeting −

$greeting = "Welcome to PHP!";

Expressions are combinations of tokens

The smallest building blocks of PHP are the indivisible tokens, such as numbers (3.14159), strings (.two.), variables ($two), constants (TRUE), and the special words that make up the syntax of PHP itself like if, else, while, for and so forth

Braces make blocks

Although statements cannot be combined like expressions, you can always put a sequence of statements anywhere a statement can go by enclosing them in a set of curly braces.

Here both statements are equivalent −

if (3 == 2 + 1)   
print("Good - I haven't totally lost my mind.<br>");   
if (3 == 2 + 1) {   print("Good - I haven't totally");   
print("lost my mind.<br>");
}

Running PHP Script from Command Prompt

Yes you can run your PHP script on your command prompt. Assuming you have following content in test.php file

 <?php

   echo "Hello PHP!!!!!";?>
Now run this script as command prompt as follows −

$ php test.php

It will produce the following result −

Hello PHP!!!!!

Hope now you have basic knowledge of PHP Syntax.

So, this brings us to the end of blog. This Tecklearn ‘How to run PHP code in XAMPP’ blog helps you with commonly asked questions if you are looking out for a job in PHP Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/java-and-jee-training/

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

  • Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
  • Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
  • Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

  • Java Fundamentals
  • Introduction to Java Basics
  • Features of Java
  • Various components of Java language
  • Benefits of Java over other programming languages
  • Key Benefits of Java

Installation and IDE’s for Java Programming Language

  • Installation of Java
  • Setting up of Eclipse IDE
  • Components of Java Program
  • Editors and IDEs used for Java Programming
  • Writing a Simple Java Program

Data Handling and Functions

  • Data types, Operations, Compilation process, Class files, Loops, Conditions
  • Using Loop Constructs
  • Arrays- Single Dimensional and Multi-Dimensional
  • Functions
  • Functions with Arguments

OOPS in Java: Concept of Object Orientation

  • Object Oriented Programming in Java
  • Implement classes and objects in Java
  • Create Class Constructors
  • Overload Constructors
  • Inheritance
  • Inherit Classes and create sub-classes
  • Implement abstract classes and methods
  • Use static keyword
  • Implement Interfaces and use it

Polymorphism, Packages and String Handling

  • Concept of Static and Run time Polymorphism
  • Function Overloading
  • String Handling –String Class
  • Java Packages

Exception Handling and Multi-Threading

  • Exception handling
  • Various Types of Exception Handling
  • Introduction to multi-threading in Java
  • Extending the thread class
  • Synchronizing the thread

File Handling in Java

  • Input Output Streams
  • io Package
  • File Handling in Java

Java Collections

  • Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
  • Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
  • Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

  • Introduction to SQL: Connect, Insert, Update, Delete, Select
  • Introduction to JDBC and Architecture of JDBC
  • Insert/Update/Delete/Select Operations using JDBC
  • Batch Processing Transaction
  • Management: Commit and Rollback

Java Enterprise Edition – Servlets

  • Introduction to J2EE
  • Client Server architecture
  • URL, Port Number, Request, Response
  • Need for servlets
  • Servlet fundamentals
  • Setting up a web project in Eclipse
  • Configuring and running the web app with servlets
  • GET and POST request in web application with demo
  • Servlet lifecycle
  • Servlets Continued
  • Session tracking and filter
  • Forward and include Servlet request dispatchers

Java Server Pages (JSP)

  • Fundamentals of Java Server Page
  • Writing a code using JSP
  • The architecture of JSP
  • JSP Continued
  • JSP elements: Scriptlets, expressions, declaration
  • JSP standard actions
  • JSP directives
  • Introduction to JavaBeans
  • ServletConfig and ServletContext
  • Servlet Chaining
  • Cookies Management
  • Session Management

Hibernate

  • Introduction to Hibernate
  • Introduction to ORM
  • ORM features
  • Hibernate as an ORM framework
  • Hibernate features
  • Setting up a project with Hibernate framework
  • Basic APIs needed to do CRUD operations with Hibernate
  • Hibernate Architecture

POJO (Plain Old Java Object)

  • POJO (Plain Old Java Object)
  • Persistent Objects
  • Lifecycle of Persistent Object

Spring

  • Introduction to Spring
  • Spring Fundamentals
  • Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "How to run PHP code in XAMPP"

Leave a Message

Your email address will not be published. Required fields are marked *