Learn PHP: Class One, Echos and Variables

So now that we have done the Prep Class its time to move on to the actual learning!


echo "text here";


echo 'text here';

Simple as that, thats how you get PHP to output some text. Whichever you use is really up to you, there is pros and cons to what you can do with each, you can read about them here.

Comments are really important within any programming language, really to tidy up your code and so you remember what each bit does later on. PHP supports two different ways of commenting, C++ Style and Shell style. I will just stick with C++ Style for today

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

echo 'Hi'; // Output: Hi
echo 'Hi'; /* Hi */

// Comments out the rest of the line. While /* */ lets you comment as far as you want, just remember to close the comment! If you don’t get me I’ll do a quick example:
This will stop PHP echo’ing “Hi”

echo "Hi";

This however will not.

echo "Hi";

But this would

//echo “Hi”;
/* style are obviously much easier for commenting out blocks of code. But for just adding a quick comment // style is alot better!


$variablename = "Variable One";
$variablename = 'Variable One';
$variablename = 1;

Variables are vital within your programming language, you will use them over and over again! Again, you can use single or double quotes, both with their pros and cons, you can also use numbers, or other variables. I’ll give you a quick example of adding within variables.

$add = 2 + 1;

That variable would add 2 + 1. But how to get what the Variable says? Well you just use echo again!

$add = 2 + 1;
echo $add;

The output of that would be:


Nothing more, nothing less.
You can also add variables within variables. As complicated as that sounds its pretty easy. I’ll give you a quick example.

$one = 1;
$two = 2;
$add = $one + $two;
echo $add;

So lets go over what we just learnt with a quick script;

$name = 'Sean'; // My Name
$age = 16 +1; // Seventeen!
$nextage = $age +1; //Age I will be next year.
echo 'Hi my name is ';
echo $name;
echo ' and I am';
echo $age;
echo 'but I will be'
echo $nextage;
echo 'next year!'

Of course that is probably the longest, but easiest way you could do it! The faster way to do it would be:

$name = 'Sean'; // My Name
$age = 16 +1; // Seventeen!
$nextage = $age +1; //Age I will be next year.
echo "Hi my name is $name and I am $age but I will be $nextage next year!”"

That’s defiantly the handiest way to do it! Don’t worry I’ll go a bit more into that in the next lesson! Until then, practice!

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked

This site uses Akismet to reduce spam. Learn how your comment data is processed.