PHP a general purpose programming language
Choosing a programming language often depends on the task at hand, the platform you are targeting, and your personal preferences.
Programming languages are formal systems designed for expressing computations. They are used to instruct computers and create software applications. There are numerous programming languages, each with its own syntax, semantics, and purposes.
Types of programming languages
Domain specific like SQL
General purpose like C++, Java, Python, PHP.
The domain-specific languages are used within specific application domains. For example, SQL is a domain-specific language. It’s used mainly for querying data from relational databases. And SQL cannot be used for other purposes.
On the other hand, PHP is a general-purpose language because PHP can develop various applications but it is mainly used in web development.
What can PHP do ?
PHP has two main applications :
Server side scripting - developing dynamic websites
Command line scripting - like python, we can run PHP script from command line to send mails or some admin tasks.
How PHP works ?
To Work with PHP we need to have following software installed
PHP
A web server that supports PHP . e.g - Apache web server
A Database server . e.g - MySQL
Typically, we won’t install all this software separately because connecting them is tricky and not intended for beginners.
Therefore, it’s easier to find an all-in-one software package that includes PHP, a web server, and a database server. One of the most popular PHP development environments is XAMPP.
XAMPP is an easy install Apache distribution that contains PHP, MariaDB, and Apache webserver.
Installation steps for linux
Download XAMPP with link below
Change the permissions to the installer
chmod 755 xampp-linux-*-installer.run
Run the installer
sudo ./xampp-linux-*-installer.run
Then you must set the path variables to access xampp bundle globally
vim ~/.bashrc
Add this in the file
export PATH="$PATH:/opt/lampp/bin"
Symlink by ruuning this command to access PHP globally
sudo ln -s /opt/lampp/bin/php /usr/local/bin/php
You can start and stop XAMPP on linux by following commands
sudo /opt/lampp/lampp start sudo /opt/lampp/lampp stop sudo /opt/lampp/lampp restart
Now if you run below command you will get following output
govind@penguin:~$ php -v
PHP 8.2.4 (cli) (built: Apr 6 2023 08:21:45) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
Running PHP code
First of all make a file named index.php and write some code in it as given below
<?php
echo "Hello world";
?>
There is 2 ways by which you can run PHP code
Serve on the web
php -S localhost:8000
now go to the browser and type localhost:8000 in address bar
On the Command line
php index.php
You can also copy paste your index.php file in xampp/htdocs folder and access it via localhost in browser but its a very tedious work to do. i mostly prefer it serving via php.