In order to actually run any code, we have to install the PHP interpreter. There are several ways to go about this, depending on what you want to achieve - for now, lets stick to the most common ways for beginners.
TLDR: To use PHP on Windows, Mac or Linux, a web server package like xampp will quickly get you started. On Windows, make sure to add the xampp/php folder to the PATH environment variable.
Windows: Installing PHP along with a Web Server and MySQL
If your primary goal is developing database driven websites, a good way to get going is to download and install the popular xampp package. It comes with PHP, Apache web server and a MySQL compatible database system as well as some additional tools and a handy launcher.
The default installation location is C:\xampp
with C:\xampp\htdocs
beeing
the folder for your PHP / website files. You should delete any
files that are pre-installed in the htdocs
folder - they are just placeholders.
Since there's a beginner friendly instructional video on the xampp website, I'm not going to go into details here =). Once installed you are pretty much ready to go.
Windows: Installing PHP only / standalone
Choose this option if you simply want a clean, portable copy of PHP to learn the language and execute code snippets. Actually, PHP comes with a built-in web server nowadays, so you can even preview simple dynamic websites as well.
Head over to the windows downloads page on the PHP website https://windows.php.net/download/ and grab a version of your choice. I'll be using PHP 7.3 (7.3.4) VC15 x64 Thread Safe, which is the most recent PHP 7 release as I'm writing. If you want to use the same version, here's the direct download link php-7.3.4-Win32-VC15-x64.zip.
When extracting the archive, you should choose a location with
no whitespaces or special characters in the path name.
Your windows user directory would be just fine e.g. C:\Users\Bob\php
.
It does not matter really since we're going to make PHP available
globally in the next step. Once extracted there should be a few
folders and several dozen files in the php folder. Excellent.
Now we need to add this path to the environment variables to let
Windows know where our php.exe resides.
Open up My Computer -> Properties -> Advanced System Settings -> Environment Variables.
There should be a variable named PATH, to which you simply add
the folder name to PHP C:\Users\Bob\php
.
To double check if PHP is now properly installed and globally available,
type php -v
in a new command prompt. The output should be something like this:
PHP 7.3.4 (cli) (built: Apr 2 2019 21:56:52) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.4, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.4, Copyright (c) 1999-2018, by Zend Technologies
If you get an error message, saying the command was not found, double check your PATH environment variable again and/or close the command prompt and open it up again.
If a Windows error pops up, you probably need the install the Microsoft VC Runtime. If you installed the same version as I did, go to the Microsoft website https://www.microsoft.com/download/details.aspx?id=48145 and download/install the vcredist_x64.exe. Now you should be fine and we're ready to go =).
Installing & enabling extensions like Xdebug on Windows
Extension provide a ton of additional functionality. Many of them are already installed, but disabled by default. Hence we need to enable them.
Xdebug on the other hand is not pre-installed, so we have to download it first.
The official download page can be found here https://xdebug.org/download.php.
From there we choose and download the one corresponding to our PHP version.
If you installed the PHP version I mentioned in the chapter above,
you'll want to download the latest Xdebug version with the label
PHP 7.3 VC15 TS (64 bit)
.
When downloading the extension, there's a chance that the browser
issues a security warning. This is the regular behavior when trying to download
files with the extension dll
. In this case though, we are confident
that we can trust the Xdebug website.
After downloading an extension, we have to move the file to the php
extension folder e.g. C:\Users\Bob\php\ext
. There should be several
dozens of other dll files inside already. Also for simplicity of future
updates we rename the extension from whatever complicated name it has
to simply php_xdebug.dll
.
Btw. Tons of other PHP extensions can be found here https://windows.php.net/downloads/pecl/releases/
Now - to enable the xdebug extension, we have to navigate to PHPs root install
folder C:\Users\Bob\php\
and look for a file named php.ini
. If there is no
such file yet, we simply create a copy of the file php.ini-development
and
rename the copy to just php.ini
. After opening the file, we need to scroll
all the way down to the very bottom and add the following lines.
[phpextensions]
extension_dir = "ext"
zend_extension=php_xdebug.dll
xdebug.remote_enable=1
xdebug.remote_port=9001
xdebug.remote_autostart=1
To double check, if we did everything properly we take a look at the
output of php -v
at the console again. This time it should show an
extra line with the xdebug version number.
PHP 7.3.4 (cli) (built: Apr 2 2019 21:56:52) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.4, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
with Zend OPcache v7.3.4, Copyright (c) 1999-2018, by Zend Technologies
Alight, now we're finally ready to go and start to develop something.
Ubuntu / Debian: Installing PHP
For Ubuntu/Debian users it's as easy as opening a shell and simply typing
sudo apt-get install php
After that, you should be ready to go. Type php -v
in a console window to double check.
PHP 7.2.18-1+0~20190503103213.21+stretch~1.gbp101320 (cli) (built: May 3 2019 10:32:13) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.18-1+0~20190503103213.21+stretch~1.gbp101320, Copyright (c) 1999-2018, by Zend Technologies
Installing & enabling extensions like Xdebug on Ubuntu / Debian
To install an extension like e.g. Xdebug, just use apt
again and install the
desired extension. It's really that simple.
sudo apt-get install php-xdebug
PHP comes with several built-in extension, but most of them are disabled by default.
To enable and disable extensions, there are two commands. Use phpenmod
to enable
an extension and phpdismod
to disable it.
sudo phpenmod xdebug
sudo phpdismod xdebug
next : : Choosing A Php Editor Or Ide