Php run local php file

How to Run PHP Code on Your PC

Do you want to run PHP code on your PC? This tutorial will teach you everything.

PHP is a server-side scripting language, that’s why you can’t run it by just opening it in your browser.

You need two things to run PHP code on your PC.

  1. A Local Webserver software with Apache, MySQL, and PHP support,
  2. A Text Editor for example Visual Studio Code.

Note: You can use any text editor with PHP support.

Now, let’s see the steps to set up, install and run PHP code on your PC.

How to Run PHP Code

First of all, we’re going to teach you how to create a local web server on your computer to run PHP. Because PHP is a server-side language and you can’t run it without a server.

Fortunately, we have third-party software available on the internet like XAMPP, WAMP, MAMP, etc. These apps can make the job extremely easy for you to run PHP.

We’ll use XAMPP to create a local webserver. The reason I prefer XAMPP is that we can run XAMPP on multiple operating systems, like Windows, MAC, Linux, etc. And also, it has more features.

So, let’s begin with our first step.

Step 1: Install XAMPP, Run Apache and MySQL

XAMPP is software that you can use to create a local webserver on your PC. Using XAMPP, you can run the Apache server that is required to run and execute PHP code.

There are other web server apps available on the internet like WAMP, MAMP, etc. But we choose XAMPP because it is open-source software and available for more than one operating system. It is available for Windows, Linux, and MAC OS.

To install XAMPP, visit the apachefriends.org website. Then, download and install XAMPP on your PC.

install XAMPP to run PHP

After installing XAMPP on your PC, open it. Then run Apache and MySQL as shown in the image below. MySQL is a database server, it is not mandatory. Because in this tutorial, we’ll not talk about the MySQL database. We’ll talk about the MySQL database in our later PHP tutorials.

Читайте также:  Java деление на 0 ошибка

So you can only Start Apache if you want.

Run Apache and MySQL on XAMPP

And done! you have successfully created a local web server on your PC. But wait! How you can access it in your browser?

Every web server has an IP address and a hostname. As this server is on our local computer. So, for XAMPP by default, our hostname is localhost and our localhost’s IP address is 127.0.0.1.

Just type localhost in your browser’s search bar to access your local web server.

localhost

You will see a ‘Welcome to XAMPP’ page. This means the local web server or XAMPP is working on your PC.

Step 2: Place Your Project Folder on the Local Server

On a live web server your website files (aka your project files) are located in directories (mean folders). Same as the files and folders on your computer. Here, to run PHP code in XAMPP, your website files will store in the htdocs folder which is located in the XAMPP folder.

To do this, you have to copy-paste your PHP project folder into ‘htdocs‘ folder as shown in the image below and create a folder inside it with any name.

htdocs folder in xampp to store php files

This is where your HTML, CSS, JS, and PHP files will store. The PHP files you’re going to create in the next step will also store in this folder.

So now, you have a web server running on your PC. It’s time to create a PHP file and execute PHP code in our Browser. So, let’s see the main step.

Step 3: Create a PHP File to Run PHP Code

I am using visual studio code to create a PHP file. You can download this text editor from their website, it’s free.

Before creating a PHP file, open the htdocs folder and create a new folder in that. You can give it any name. This is the folder where your website or project files are located. As you can see in the image below, I gave it a name testwebsite .

Create a Folder in htdocs folder

You can access this folder in your browser by typing localhost/testwebsite.

Читайте также:  Html input type enabled

After that, open visual studio code and open this folder in your text editor as shown below.

Open test website folder in VS code

Then, create a PHP file and give it the name sample.php.

After that, write some code in that file. You can copy/paste the code given below.

This will look like this as shown in the image below.

It is the simple hello world program that we’re going to run on your PC.

Now, we have created the PHP file and written some code in it. Now, you can run PHP in your browser.

Step 4: Execute or Run PHP Code in Your Browser

To do this, open your browser and type in the search bar localhost/testwebsite/sample.php. And hit enter. This will open a PHP file and run a PHP script in your browser.

According to our code. The ‘Hello World’ program has been executed. See the image below.

a simple php hello world program

That’s it. Now, you know how to run PHP code on your PC. And you can create and run as many PHP files as you want.

Things to Remember Before Running PHP Code:

  • Whenever you want to run PHP code, always make sure XAMPP is running with Apache and MySQL.
  • Your project folder containing PHP files must be placed inside ‘htdocs‘ folder which is located in the XAMPP installation folder.
  • Whenever you install XAMPP on your PC, just make sure you select the default location to install XAMPP which is usually C drive. To avoid errors.

What’s next? Now, you should learn PHP variables. Variables are containers for storing data. This tutorial is will help you to understand the concepts behind variables.

If you want to learn something advanced about PHP. Then, see this trending guide on how to connect HTML Form to MySQL database using PHP.

That’s it, if you have any questions related to this guide, then feel free to leave a comment below.

Keep learning and sharing!

About The Author

Muhammad Zeeshan

Hi everyone, I’m a Full Stack Web Developer and Technical Writer. I just love to share my knowledge to help others in this community. I mostly write about HTML, CSS, JS, MySQL, and PHP.

Источник

How to execute PHP code using command line ?

PHP Installation for Windows Users: Follow the steps to install PHP on the Windows operating system.

Читайте также:  Css new line height

  • Step 1: First, we have to download PHP from it’s official website. We have to download the .zip file from the respective section depending upon on our system architecture(x86 or x64).
  • Step 2: Extract the .zip file to your preferred location. It is recommended to choose the Boot Drive(C Drive) inside a folder named php (ie. C:\php).
  • Step 3: Now we have to add the folder (C:\php) to the Environment Variable Path so that it becomes accessible from the command line. To do so, we have to right click on My Computer or This PC icon, then Choose Properties from the context menu. Then click the Advanced system settings link, and then click Environment Variables. In the section System Variables, we have to find the PATH environment variable and then select and Edit it. If the PATH environment variable does not exist, we have to click New. In the Edit System Variable (or New System Variable) window, we have to specify the value of the PATH environment variable (C:\php or the location of our extracted php files). After that, we have to click OK and close all remaining windows by clicking OK.

PHP Installation for Linux Users:

apt-get install php5-common libapache2-mod-php5 php5-cli

PHP Installation for Mac Users:

    Mac users can install php using the following command.

curl -s https://php-osx.liip.ch/install.sh | bash -s 7.3

After installation of PHP, we are ready to run PHP code through command line. You just follow the steps to run PHP program using command line.

  • Open terminal or command line window.
  • Goto the specified folder or directory where php files are present.
  • Then we can run php code using the following command:

We can also start server for testing the php code using the command line by the following command:

php -S localhost:port -t your_folder/

Note: While using the PHP built-in server, the name of the PHP file inside the root folder must be index.php, and all other PHP files can be hyperlinked through the main index page.

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

Источник

Оцените статью