PHP Tutorial: Basic syntax

Hello everyone, this is a continuation to the first PHP article that introduces you to PHP. In this article, we will be going through the basic syntax of PHP.


Recap

In the previous article, you’ve been exposed to how PHP works, PHP handlers, PHP interpreter and the simple Hello World code.


Variable Types

There are several variables type in PHP and these variables are present in most programming languages too. This article will briefly explain the variable types. Some might be very simple and familar while others might be quite new to you.

Integers

An integer is a number that is the range of …, -2, -1, 0, 1, 2, …. They are usually declared as decimals but can also be declared as negative, octal, hexadecimal or binary as well.

Booleans

A very simple variable type, it can either be TRUE or FALSE.

Strings

A string is series of characters. For PHP, the supported set is the 256-character set and the maximum size of a string variable is 2GB. There are 4 different type of strings – single quoted, double quoted, heredoc syntax and nowdoc syntax.

Floats

Floats, also known as doubles/real numbers/floating point numbers, are platform dependent variables that has a precision of roughly 14 decimal digits. Due to its limited precision, it should never be trusted for complicated calculations and exact rounding.

Arrays

An array in PHP is an ordered map, which associates values to keys. Due to nature of this, it can be treated as an array, list, hash table, dictionary, collection and many more. Arrays in PHP makes it stand out against other programming languages as it makes multidimensional arrays possible and really easy/simple to use.

Objects

Objects are created from instantiating a class through the “new” statement.

NULL

NULL means a variable with no value. NULL is the only possible value of type null.

Resources

Resources are special variable type that holds a reference to another external resource.

Callbacks / Callables

We won’t be touching on this for now.

Pseudo-types

We won’t be touching on this for now.

Juggling (Default)

This will be touched on in the later part – Initializing Variables.


Operators

Operators are used in programming languages to carry out arithmetical calculations and bitwise operations. It is the typical plus + and minus -.

So here are some of the basic operators:

  • + – Addition
  • − – Subtraction
  • % – Modulus
  • * – Multiply
  • / – Divide
  • ^ – To the power of
  • = – Set
  • == – Is equal to comparison (loose variable type comparison)
  • === – Is equal to comparison (strict variable type comparison)
  • += – Add to
  • -= – Remove from
  • ++ – Increase by 1
  • −− – Decrease by 1
  • && – AND operator
  • || – OR operator

Initializing Variables

Variables are used in programming to store information so that it can be re-used again later. It is fairly easy to initialize a variable in PHP. Variables start with a dollar sign $ and the default declaration of variables makes the variables not limited to types. This means that when declared using a dollar sign $ without explicit type declaration, it will be a “Type Juggling” variable.

Example #1

<?php
$x = 1;
echo $x; //this should output "1" on the web page
$x = "abc d e"; //as it isn't limited to variable types, it can be changed from a integer variable to string variable without any conversion or creation of new variable
echo $x; //this should output "abc d e" on the web page
?>

Result:

1abc d e

That was easy, wasn’t it? Now we’re going to take this one step further.

Example #2

<?php
$x = 1;
echo $x;
$x++;
$b = "abc d e";
$b += $x;
echo $b;
?>

Result:

1abc d e2

If Else?

If and else conditional statements are used in programming to see if the statement is true before executing what is in-between its curly braces {}.

Example

<?php
$intVariable = 5; //declare the variable

if($intVariable == 5) {
	echo "lol";
} elseif($intVariable == 6) {
	echo "rofl";
} else {
	echo "lmao";
}
?>

Result:

lol

Loops

Loops are used in programming to execute repetitive/continuous actions or functions within its scope as long as the statement in the loop is “true” or “1”. There are 3 main loops in PHP, the while loop, for loop and foreach loop.

Example

<?php
for($i = 0; $i < 10; $i++) {
	echo $i; //echo the value of $i
}
?>

Result:

0123456789

Common Practices

Camel Case

Camel casing is a common practice in most programming languages. It is not mandatory to put variables in camel case format but it should be done. Variables need to be in a camel case format - the first letter of the first word is in lowercase while the first letter of the other words are in uppercase.

Example

<?php
$veryLongVariableNameThatWillShowThisCamelCaseFormatting = true;
$intVariable = 12;
$floatVariable = 3.14;
?>

Identation

Identations are not required in PHP but help keeps the code neat and readable. Depending on personal preferences, indentations can be either spaces or tabs. However, I recommend tabs indentations because on most text-editors and IDEs, it is easier to delete tab indentations than space indentations. Indentations should be made when your code enters one layer down or into another scope.

Example

<?php
$intVariable = 12; //declare the variable
$intVariable2 = 5; //declare the variable

if($intVariable == 5) {
	echo "lol";
} elseif($intVariable == 12) {
	if($intVariable2 == 5) {
		echo "toplel";
	} else {
		echo "rofl";
	}
} else {
	echo "lmao";
}
?>

Result:

toplel

Conclusion

And that's it for now. We've gone through operators, variables, conditional statements, loops and common practices. Thanks for being a great reader and I've hope you have enjoyed learning the basic syntax of PHP. If you have any questions, feel free to leave a comment below and I'll get back to you as soon as possible.

Next Tutorial: PHP Requests

Tutorial Overview: Fast Track Learning

Author: Woo Huiren

Howdy, I'm a IT student who is currently serving my full time national service. I do lots of things related to IT but I have an exceptionally great love for the web and opensource, so expect me to blog a lot about it! :)

Leave a Reply