# PHP a general purpose programming language

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

1. Domain specific like SQL
    
2. 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 ?

![](https://www.phptutorial.net/wp-content/uploads/2021/03/What-is-PHP-How-PHP-works.png align="center")

---

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](https://www.apachefriends.org/).

XAMPP is an easy install Apache distribution that contains PHP, MariaDB, and Apache webserver.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">MariaDB is a fork of the most popular relational database management system, MySQL.</div>
</div>

### Installation steps for linux

1. Download XAMPP with link below
    
    [XAMPP installer](https://www.apachefriends.org/index.html?source=post_page-----56f8d639d627--------------------------------)
    
2. Change the permissions to the installer
    
    ```bash
    chmod 755 xampp-linux-*-installer.run        
    ```
    
3. Run the installer
    
    ```bash
    sudo ./xampp-linux-*-installer.run
    ```
    
4. Then you must set the path variables to access xampp bundle globally
    
    ```bash
    vim ~/.bashrc
    ```
    
5. Add this in the file
    
    ```bash
    export PATH="$PATH:/opt/lampp/bin"
    ```
    
6. Symlink by ruuning this command to access PHP globally
    
    ```bash
    sudo ln -s /opt/lampp/bin/php /usr/local/bin/php
    ```
    
7. You can start and stop XAMPP on linux by following commands
    
    ```bash
    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

```bash
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
<?php
    echo "Hello world";
?>
```

There is 2 ways by which you can run PHP code

* Serve on the web
    
    ```php
    php -S localhost:8000
    ```
    
    now go to the browser and type localhost:8000 in address bar
    
* On the Command line
    
    ```php
    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.
