How does php work?

In order to write web application using php you need to have following things on your computer installed:

  • A web server like apache or nginx
  • A database to store data
  • PHP parser support

Whenever we refer to local environment setup I mean to say installing above components in your computer. Once we have local environment setup we will be able to write php applications.

In the begining we do not need to install web server because we will learn some basics of php and run these scripts using terminal.

You wont need to have database at this moment until we move on to advance level. At this moment all we need is to have php parser support on our computer.

To install php7 on different operating system to begin with follow tutorials below regarding different opearting system.

How to install php7 on ubuntu (linux)?

To install php7 on linux open your linux terminal window and run following commands:

# install python dependencies
sudo apt-get install python-software-properties

# add php repo
sudo add-apt-repository ppa:ondrej/php-7.0

# update your package
sudo apt-get update

# install php7 
sudo apt-get install php7.0

# install other useful php extensions
sudo apt-get install php7.0-curl php7.0-zip php7.0-mysql php7.0-fpm

# verify if php installed
php -v

How to install php7 on mac?

To install php7 on mac machine open your mac terminal and run following commands:

To install php7 you first need to have brew installed on your mac machine. To install homebew on your mac machine open your terminal window and run following commands:

# install home brew
/usr/bin/ruby -e "$(curl -fsSL"

# verify if brew installed
brew -v

Once you have brew installed on your mac machine you can now install php7 using brew open your terminal window again and run following commands:

# update/upgrade your brew
brew update && brew upgrade

# tap on php versions
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php

# remove old php if any
brew unlink php56 

# install php7
brew install php70

# verify if php installed
php -v

How to install php on windows?

To install php7 on windows machine follow the tutorial below:

Install php7 on windows

Note: make sure that you can access php from anywhere in your computer terminal if you can not do it make sure to add php executable path to environment variables.

How to create a php file?

php scripts are saved with .php extensions. You can easily create a new file and named using .php at the end when you save the file.

Once you know how to create php files you can test them easily using command line. Let's take a look on how we can create a sample test file using php and test.

Open your mac or linux terminal window and run following commands:

# go to your home directory
cd ~/

# let's create a test file
touch test.php

# above command will create an empty file
# let's add some sample php code to test
# open your newly created file and paste following contents
nano test.php

# paste following contents and save file
# using ctrl + x + y + enter
<?php echo "Hello World"; ?>

# now, test this new file
php test.php

At this moment all we needed to learn how to save and create php files. Don't worry about php syntax. We will slowly learn php as we move on.