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.
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.
<?phpecho "hello world";
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 symbolscan 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";*/
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;
There are a number of operators you can use within PHP.
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'
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