Learn PHP: Output & Operators

This series was severely outdated, as it was originally written in 2009, I have updated it with more modern options as of 12th July 2020

Continuing on from Learn PHP: Setup, we can start with a few of the basic principles / building blocks of the language.

Output

The first thing you want to be able to do is output data to your console, we can do this by using the echo statement.

<?php
echo "hello world";

Comments

Comments are multipurpose they allow you to temporarily cause code not to run as well as make notes describing code functionality.

PHP supports two different ways of commenting, C/C++ style and Unix shell style (Perl Style). We will focus on C/C++ style here.

Example’s of echo via C++ Style are as follows:

<?php
// Comments starting with this are single line.
echo "hello";
/* Any comments within this combination of symbols
can span multiple lines
*/
echo "world";

You can also use comments to temporarily cause a block of code to no longer run. In the example below the console won’t output anything.

<?php
/**
echo "hello world";
*/

Variables

You will use variables for a variety of things when programming. Essentially they let you store some data to either use or modify later on.

Here are two examples of variables, one containing a string, the other an integer.

<?php
$first_variable = "Variable One";
$second_variable= 1;

Operators

There are a number of operators you can use within PHP.

Arithmetic Operators

There are plenty of arithmetic operators for operating on numbers.

Here are an example of a few of the basic ones you’ll use frequently.

<?php
$add = 2 + 1;
echo $add; // Will output '3'
$subtract = $add - 1;
echo $subtract; // Will output '2'
$multiply = $add * 4;
echo $multiply; // Will output '8'
$divide = $multiply / 2;
echo $divide; // Will output '4'

String Operators

There are two string operators available in PHP. You’ll use these for joining strings together like below:

<?php
$variable = "Hello";
echo $variable . " world"; // Will output 'Hello world'
$variable .= " world";
echo $variable; // Will now output 'Hello world'

You can now move onto the next part Learn PHP: Ifs And Else’s


Posted on August 20, 2009

Learn PHP: Setup

This series was severely outdated, as it was originally written in 2009, I have updated it with more modern options as of 12th July 2020

Cloud based options

There are plenty of free cloud based editors that will allow you to get up and running without having to install anything on your computer. If you are just looking to try out PHP and aren’t sure you will continue using it, this is the best option.

Windows options

Wamp

Wamp is a GUI installer that will guide you through installing a web server Apache a database mySQL, the PHP programming language and PhpMyAdmin which allows you to easily manage your databases.

If you are looking for an option for more long term use, this is the option I would suggest.

Xampp

Xampp is another GUI installer, made by Apache. It will install a web server Apache, a database MariaDB and two programming languages PHP and Perl.

PHP Built-in web server

As of PHP 5.4 there is now a built-in web server available in PHP.

This means that you can now just go here and download the latest “Non Thread Safe” archive. You should then extract the archive to somewhere (eg C:\php) and then add that directory to your path as explained here.

This Stack Overflow answer contains answers that explain in detail the differences between “Thread Safe” vs “Non Thread Safe”.

OSX options

For Mac OSX the only easy option I’m aware of is installing via Homebrew, if you don’t already have Homebrew, it is a package manager that if you are getting into development you will want to have installed.

Once Homebrew is installed you can just run the following to install php.

brew install php

Linux options

If you are using Linux, you are going to need to utilise a search engine in order to get setup. Digital Ocean have good walkthroughs like this one for Ubuntu 18.04.

You can now move onto the next part Learn PHP: Output & Operators


Posted on July 07, 2009