November 13, 2024 • 15 min read

How to Test Your PHP Locally with Examples

THE SHORT ANSWER

To test PHP locally, install XAMPP, move your PHP files into its htdocs folder, start the Apache server, then load the file in your browser using localhost and the correct port. Online PHP testers are a lighter alternative that skip local installation entirely.

What is PHP?

PHP is a free, open-source, server-side scripting language that web servers use to build web pages. Rasmus Lerdorf created PHP in 1994 and released it publicly in 1995. PHP is a general-purpose language and a popular choice for web development because you can embed it directly in your HTML. As of October 2026, W3Techs reports that PHP is used by 69.7 percent of websites whose server-side language it can detect. PHP can be used for the dynamic generation of your web page content.

A good example is a blog website. PHP scripts can be written for retrieving blog posts from your database prior to display. Facebook was originally written in PHP. You can also test PHP code locally for:

  • Restricting access for specific website pages
  • Saving and processing your user input through form data
  • Working with and setting website cookies

The execution of your PHP code is through a web server as opposed to using a local computer. Good examples are when you click on a website link with the pages written using PHP and completing and submitting a website form. No PHP is running on your computer in either instance. Your request for a web page or form is delivered to a web server prior to processing using the PHP scripts.

The processed HTML is then sent back to your computer by the webserver with your results displayed. This is the reason you will be unable to view a website’s PHP. All you see are the PHP scripts and HTML being produced. PHP is classified as an interpreted language. When changes are made to your source code, they can be tested immediately. Your source code does not need to be compiled into binary form which makes the process much faster.

How do you test PHP code on localhost?

To test PHP on localhost, install a local server such as XAMPP, put your PHP file in its htdocs folder, start Apache, and open the file through localhost in your browser.

When you test PHP code locally, you can check your PHP scripts for both functionality and errors. Your best option is using XAMPP through your web browser to run your PHP scripts. If you prefer, there are online services available to find errors within your PHP code. If you choose to use XAMPP, follow the steps below.

Step One

Make certain XAMPP is installed. This is one of the most frequently used PHP testing environments for Mac and Windows computers. XAMPP can be downloaded and installed for free. For instance:

How to Test Your PHP Locally with Examples

(Image Credit: Learn Coding Fast)

Step Two

If XAMPP is running, close it. Your htdocs folder can now be updated with no interference from the existing processes. If you are using a MAC, skip this step.

Step Three

Put your PHP files into your htdocs folder. If you are using Windows, open the folder labeled My PC. Double-click on the name of your hard drive, then your xampp folder and finally your htdocs folder. You need to move all required PHP files into your folder. If you are using a Mac, go to your XAMPP control panel and click the Volumes tab. Now click Mount, then Explorer and double click on your htdocs folder. Once again, move all required PHP files. For example:

How to Test Your PHP Locally with Examples

(Image Credit: wikiHow)

Step Four

Double click on your icon for XAMPP or open your XAMPP. Look for an orange background with a white X.

Step Five

Just to the right of your Apache heading, you will see the Apache web server. Click on Start. There is an indicator to your right that should turn green. For example:

How to Test Your PHP Locally with Examples

(Image Credit: Edureka)

Step Six

To the right of your first Apache port is another port number. If you edited the file titled httpd.conf, you need to label your second port 8080. For instance:

How to Test Your PHP Locally with Examples

(Image Credit: wikiHow)

Step Seven

Open your web browser to test your PHP scripting. You can use your choice of web browsers including Chrome.

Step Eight

Go to the address bar above your browser and click. If you see any text delete it before moving to the next step. For example:

How to Test Your PHP Locally with Examples

Step Nine

Type in the PHP script address you need to test. Enter localhost followed by the second port number for your Apache server. Now enter a slash and the PHP document name you want to be tested. Your address must have .PHP at the end.

Step Ten

You can load your PHP script into your browser by pressing ↵ Enter. Your page will load correctly if your script is working. If you have errors in your script, you will receive errors or your page will not load properly.

How do you test PHP code online?

To test PHP online, paste your code into a browser-based PHP tester, choose a PHP version, and run it to see the output and any errors.

Step One

Your PHP document can be opened with the PHP editing software on your computer. If you are using Windows, this is Notepad++ and BBEdit for Mac. For Windows, right-click on your document. For Mac, you need to click on your document first, then click on File. Now click Open With and click the name of your PHP editing program. Here is what the Windows option will look like:

How to Test Your PHP Locally with Examples

Step Two

Select the contents of your documents. Click anywhere within your document, then enter Ctrl+A for Windows to select the complete document or Command+A for Mac.

Step Three

For Windows enter Ctrl+C to copy your contents and Command+C for Mac.

Step Four

Go to your web browser and open the site for your online PHP functions. For instance:

How to Test Your PHP Locally with Examples

Step Five

Paste in your code, then choose your code for the Your Script window. Now replace the old code with your copied code by entering Ctrl+V for Windows or Command+V for Mac.

Step Six

Choose your PHP version, Look under the Your Script widow for a drop-down-box. Select Run on PHP Version.

Step Seven

Look under your drop-down-box and click on Execute Code. Your PHP code will now run. Your results box is located under the button for Execute code. This is where you can view your results. For example:

How to Test Your PHP Locally with Examples

Step Eight

Your code should appear in your result box. If your code has any errors, they will be displayed between tags. Every error reported is for a specific line. Look on the left of the Your Script window to view each line by number. To the left of these lines, you will find the errors displayed as white and red X icons.

Why use a PHP framework for local PHP projects?

A PHP framework gives you ready-made structure and tested code, so you write less from scratch and follow common best practices from the start.

The majority of web developers find it more practical to use frameworks as opposed to writing code for the entire website. The framework offers you numerous benefits because most of the nuances have already been handled when you create a new project. The majority of frameworks also adhere to the best practices for your language. Since most use the model-view-controller or MVC pattern, presentation layers are separated from logic. The most popular frameworks include:

  • Laminas (formerly Zend Framework)
  • Laravel
  • FuelPHP
  • Yii 2
  • CodeIgniter
  • CakePHP
  • Symfony
  • Slim

You can place PHP scripts anywhere in your document. They always begin and finish using the same symbols. Your PHP statements must always conclude with a semicolon. A good localhost example test PHP outputting text with the built-in echo function is “Examples Of The Best PHP “. None of your PHP functions, classes and keywords are case sensitive. You need to be aware that all variable names will be case sensitive. If you are not careful with statements including variable names, they will be invalid.

How do PHP variables work?

A PHP variable stores a value under a name that starts with a dollar sign, and you give it a value with the = operator, as in $name = ‘Susan’;.

The main way information is stored in your PHP programs is variables. Every PHP variable must begin with a dollar sign. An example is $php_name. You use = operator for assigning variables. The variable name must be on the left with the expression evaluated placed on the right. You need to follow the rules for your PHP variables. Variable declarations must begin with the $. The variable name then follows.

You must begin your variable names with an underscore, a lowercase letter or an uppercase letter. You can only use numbers, letters or underscores for your variable names including A-Z, _ and zero-nine. If you use any special characters including ( ), %, + and -, your variable name becomes invalid. Always remember case-sensitivity applies to variable names. A few good examples include:

  • $our_variable
  • $theSecondVariable
  • $oneMoreVariable

Predefined variables require special keywords. Although these are valid when used as variable names, they are unable to be used for variables. This is because these variables have already been defined by the language. They can only be used for specific purposes. Some of the best examples of predefined variables are listed below.

  • $_POST
  • $this
  • $_FILES
  • $_GET
  • $_SERVER

You also need to be aware of the different types of PHP data. These can be stored using variables including:

  • Boolean
  • Integer
  • NULL
  • Object
  • “Hello” or a string
  • Array
  • Float or double
  • Resource

Strings are sequences of different characters. You can use any single or double text inside quotes. Integers are whole numbers without a decimal point. Their range depends on your system: about two billion either side of zero on 32-bit systems, and about 9E18 on 64-bit systems. You need to follow the rules set for integers including:

  • No integer can have a decimal point
  • The must be a minimum of one digit for all integers
  • Integers can be both negative or positive

A floating point number or float is a number including a decimal point. A Boolean can be one of two states, true or false. Booleans are used frequently for conditional testing. True = $x and false = $y. You can use an array for storing multiple values in a single variable. NULL is a special type of data with the value null. You can declare variables with no value simply by using NULL to set the value.

If you create a variable and do not assign a value, NULL will be assigned automatically. A class is a type of data structure helpful for modeling in the real world. This often includes certain methods and properties. Objects are considered instances within a class. This is a convenient method for packaging functions and values to a specific class.

A PHP resource is classified as a special variable due to the reference for external resources. You use and create resources for special functions. To see the type of resource, a good option is get_resource_type(). A series of characters comprise a string. You can use strings for storing textual information within your application. You have several different options for creating PHP strings and for HTTP localhost test test PHP.

You can use single quotes to create a simple string. A good example is $name = ‘Susan’;. When including a single quote in your string, you can escape it with a backslash. You can also use double quotes to create strings. You can use escape sequences in double-quoted strings. This is a special code placing characters in the string. The characters are a representation of generally invisible characters.

PHP variables can be embedded in double-quoted strings. The benefit is the values are then added to your string. You can locate the length of your string or find out how many words are contained in your string by using str_word_count(). You can also reverse one of your strings, replace your text using a string, search for text in the string or reverse the function.

What is a PHP constant?

A PHP constant is a named value that cannot change once it is set. You can create one with the define() function, which takes the constant’s name and its value. Older versions of PHP also accepted a third argument to make the name case-insensitive, but PHP 7.3 deprecated that option and PHP 8.0 no longer supports it. Once you have set the value of your constants, you will be unable to make any changes.

Constants are useful for values that rarely change, such as an API key or database password. You need to know that constants are different from variables because they always include a global scope. You can access them using any script function. There are rules for using constants within the Human class. These are referred to as self::constant_name. When used outside of the class, they are referred to as human::constant_name.

Which operators does PHP use?

PHP uses the usual arithmetic, assignment, comparison and logical operators found in most programming languages. The assignment operator is a single =. For comparison, == checks that two values are equal, and === checks that they are equal and the same type. You can also use the standard comparison symbols, or add and assign a value at the same time with +=.

Two of the most notable are “.” for joining (concatenating) strings and “.=” for adding one string to the end of another. The spaceship operator (<=>), added in PHP 7.0, returns -1, 0 or 1 when $a is less than, equal to or greater than $b.

What does the switch statement do in PHP?

A switch statement compares one value against a list of cases and runs the code for the case that matches.

The switch statement in PHP is a lot like the switch statement for JavaScript. If you want to contrast and compare, the JavaScript Switch Guide is extremely useful. You gain the ability to perform case testing quickly under numerous different conditions. The code is also much easier to read. For example:

How to Test Your PHP Locally with Examples

(Image Credit: AppDividend)

When should you use a loop in PHP?

If you require a task to be repeated numerous times, your best option is using a loop as opposed to continuing to use the same code continuously. You can stop the execution of the loop just by including a break in your loop.

What does the break statement do in PHP?

The break statement ends the current loop or switch right away, and PHP carries on with the code that follows it. If you decide not to use a break statement, you might run multiple statements and cases. If this is what you want, do not use a break statement.

What is a PHP array?

A PHP array is a single variable that holds many values in an ordered list, such as a list of student names. If you are working with multiple variables with a relation to one another, this can be very useful. A good example is a listing of capital cities or student names. There are two basic types of arrays, associative arrays and indexed arrays. Both have important uses so understanding how to create them is important.

Associative Array

An associative array is defined as a listing of values. You can access them using a key as opposed to index numbers. You can use any value for your key provided it is unique to your array.

Indexed Array

An indexed array contains a listing of ordered values. A separate index number is assigned to each individual value in your array. Indexes used for arrays always begin with zero for your first value. As you create more arrays, your values will increase by one.

Multidimensional Array

A multidimensional array is defined as any array containing other arrays. This gives you the opportunity of creating complex data structures. You can model your structures after complex groupings of data.

diib: Test Your PHP on LocalHost Today!

Whether you choose to test your PHP code yourself or find an outside testing source, you’ll want to ensure things are running how you want them to. diib compiles information about your code health and offers objectives for optimization. Here are some of the features that set us apart from our competitors:

  • Keyword, backlink, and indexing monitoring and tracking tools
  • User experience and mobile speed optimization
  • Site load speed analysis and repair
  • Social media integration and performance
  • Broken pages where you have backlinks (404 checker)
  • Technical SEO monitoring, including PHP code

Click here for your free scan or simply call 800-303-3510 to speak to one of our growth experts.

FAQ

Frequently asked questions

Can I run PHP without installing a web server?

Yes. PHP ships with a built-in development server that you start from a terminal in your project folder, then open the local address it shows in your browser. It is ideal for quick testing on your own computer, but it is not meant for live websites, where you should use Apache or Nginx instead.

What is XAMPP and do I need it to test PHP?

XAMPP is a free package that installs Apache, MariaDB and PHP together, so you can run a full website on your own computer. You do not strictly need it, since PHP's built-in server works for simple scripts, but XAMPP is easier when your project also needs a database.

Why does my PHP file download instead of running in the browser?

That usually means you opened the file directly from your computer, or the web server is not processing PHP. Make sure Apache or the built-in server is running, place the file in the served folder, such as htdocs in XAMPP, and open it through your localhost address instead of double-clicking it.

Can I test PHP code online?

Yes. Online PHP sandboxes let you paste code and see the output instantly without installing anything. They are handy for checking a function or syntax, but they cannot test your full site, database connections or file uploads. For those, use a local setup such as XAMPP or PHP's built-in server.

FREE WEBSITE SCAN

How can diib® help me improve my website today?

diib connects to your Google Analytics account in about 60 seconds (optional to start) and shows you exactly what to fix to grow your website. More than 800k businesses use diib, starting with a free website scan. Pro memberships start at $29 a month.

Because we serve so many members, we can also offer wholesale pricing on services including:

  • Professional Google Analytics setup
  • Website speed analysis and optimization
  • Keyword research
  • Article writing and publishing (500 to 5,000+ words)

Enter your website below to create your free account and see everything we offer our members.

Daniel Urmann
AUTHOR

Daniel Urmann

Daniel Urmann is the co-founder of Diib.com. Over the past 17 years Daniel has helped thousands of business grow online through SEO, social media, and paid advertising. Today, Diib helps over 150,000 business globally grow online with their SaaS offerings. Daniel’s interest include SMB analytics, big data, predictive analytics, enterprise and SMB search engine optimization (SEO), CRO optimization, social media advertising, A/B testing, programatic and geo-targeting, PPC, and e-commerce. He holds a Master of Business Administration (MBA) focused in Finance and E-commerce from Cornell University – S.C. Johnson Graduate School of Management.

LinkedIn

Facebook LinkedIn