How to Install and Execute PHP in Localhost

In this article, we are going to explain how to install and execute PHP on localhost. And also, you can find how to make some settings for the sake of best scripting practices.

PHP is a widely used scripting language that is particularly well suited to web development. PHP is a fast, flexible, and intuitive programming language that drives anything from your blog to the world’s most popular websites.

With PHP you can collect form data, generate dynamic page content, and even more, with it yo can create desktop application (with PHP-GTK extension).

But the most prominent feature of PHP is its support for a wide range of databases. Writing a database-enabled web page is very easy using the appropriate database extension.

PHP also ability to communicate to other services using protocols such as HTTP, LDAP, POP3, IMAP, SNMP, NNTP, COM, etc.

For more details about PHP refer to the link.

How to Install PHP on a LocalHost

There are a number of available web server solution stack packages to install locally, such as: XAMPP, Laragon, Wampserver, EasyPHP, etc. After the installation of any of them, you will have installed PHP together with other components.

But sometimes it is necessary to install a new PHP version instead of the existing one. In this case, do the following:

  1. Download the PHP installation packet and unpack it.
  2. Write in the Windows Environment Variables path (e.g. C:\laragon\bin\php\php-7.4.19-Win32-vc15-x64)
  3. Install (or re-install) Composer on the PC.
  4. If there is a composer.json in the existing project, run:
composer install

Also, you can install PHP in Docker. Follow the link, which discuses how to do it.

Create Server By PHP CLI Command

PHP has a built-in web server designed to aid the development process. Hence, you can create the server with a CLI command:

php -S localhost:8008

It’s impossible to run complex PHP applications on the built-in server, but you can easily run a callback application (e.g. for ngrok tunneler) and call it as:

http://localhost:8008/src/ApiMock/callback.php?query=something

PHP Code Styling

To preserve PHP code styling best practices, install PHP-CS-Fixer:

  • Install PHP-CS-Fixer from http://get.sensiolabs.org/php-cs-fixer.phar
  • Copy file on your root project folder.
  • From this folder, run:
php php-cs-fixer.phar fix ../project-folder --rules=@Laravel
  • When run from a directory with a.php_cs file, you can just run php php-cs-fixer.phar fix
  • To install PHP-CS-Fixer, install Composer and run the following command:
$ ./composer.phar global require fabpot/php-cs-fixer

Then, make sure you have ~/.composer/vendor/bin in your PATH, and you’re good to go:

export PATH="$PATH:$HOME/.composer/vendor/bin"

Then run from the project root:

php-cs-fixer fix

An example of the file .php_cs content:

The file .php_cs

and the appropriate CLI command line to activate it:

php php-cs-fixer.phar fix ../project-folder

PHP Code Folding

The first possible code-folding way:

//region Start of the code region

//endregion

Second possible code folding way:

//<editor-fold desc="Some Code Region">

//</editor-fold>

Was this helpful?

1 / 1

Leave a Reply 0

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