PHP Tutorial 2 – Basic syntax

Hello this is tutorial #2!

This tutorial will teach you basic PHP syntax. Any PHP scripting block always starts with <?php and ends with ?>, although some server configurations allow <? for a starting tag, I recommend always using <?php as a starting tag for maximum compatibility. So we always start and end like this:

<?php
//some code here...
?>

In my code fragment above you might have noticed ‘//some code above‘ The two forward slashes mean a comment is following. This kind of comment is called a single-line comment and is terminated when you insert a newline character.
For multi-line-comment use this construction:

<?php
/* Multi Line Comment started...
You can continue on this line...
and even on this line...
But after the multi line comment stop, things you type will be interpreted as code again
*/
echo "This is a little bit of code that will be executed";
// echo "This line wont be executed because it's commented!";
// So after the */ you can type code again
?>

So now that you know basic php syntax for starting and ending a php scripting block, I will finally introduce you to using variables! Variables are basically used to store data in it. An example:

<?php
$myFirstName = 'Dino';    //myFirstname using single quotes
$myLastName = "Hensen";    //myLastName using double quotes, just to show that it is possible too
echo 'My name is: ' . $myFirstName . ' ' . $myLastName;    //notice the ' ' (space) between myfirstName and myLastName
?>

The code above first assigns ‘Dino’ to the variable names myFirstName. All variables in php start with a dollar sign…I guess the php creators must have been thinking a lot about money when they created it! hah! The second instruction assigns “Hensen” to myLastName. Finally I uses the echo to output text to the screen. As you can see I concatenated 4 pieces of strings. In PHP you can concatenate strings by using a point between every two strings. I had 4 pieces concatenated, so I used 3 points.

You have also seen me use single-quotes versus double-quotes. The difference between those two is that single-quotes doesn’t check the string for variables while double quotes does! I will give a quick example:

<?php
$foo = 'bar';
echo "Some people love eating a chocolate $foo";
//output: Some people love eating a chocolate bar
echo 'Some people love eating a chocolate $foo';
//output: Some people love eating a chocolate $foo
?>

As you can see in the example above, in the second echo the single quoted string will not be searched for variables and is interpreted as a string. The first line however has its variable replaced by the value. So in the first variable example, I could have done this:

<?php
$myFirstName = 'Dino';
$myLastName = "Hensen";
echo "My name is: $myFirstName $myLastName";
//Now everything goes automatically
?>

Or I could do this:

<?php
$myFirstName = 'Dino';
$myName = "$myFirstName Hensen";
echo "My name is: $myName";
?>

In the last example the myName variable is retrieving the value assigned to myFirstName. This produces the value “Dino Hensen” and is stored in myName. Now I can echo it using just one variable! Try messing around with the code a little bit to understand how it works and get comfortable with it!

Leave a Reply

Your email address will not be published. Required fields are marked *