More on Variables and Error Reporting in PHP – Part IV

In the previous part of the course, we had learned a little bit about variables, how to declare them and make use of them in PHP. But that’s not all about it as there are lots of important things that you need to learn about variables.

There are some really important and fundamental concepts on PHP that you need to learn just at the time of your beginning. If you are in a hurry and keep on skipping a lots of important things, then later you are surely going to face a lot of troubles developing larger and more complex applications in PHP. Learning a programming language is not a one-time process as you need to continue learning new things each and every day you work with it, so it’s very important to get the basics and fundamentals perfectly clear.

In this part of the course, we will be learning some concrete concepts about PHP in-depth like strongly-typed and loosely-typed language, variable scope and variable concatenation. We will also learn a bit about error reporting in PHP.

PHP is a Loosely-Typed Language

PHP is a loosely-typed language which simply means that you do not need to mention the types of the variables while declaring them. All the variable are kept loose and you do not need to worry about their types while declaring them.

For example, you can declare a variable in this way,

[php]<?php

$string = ‘Hello’;

echo $string;

?> [/php]

In this example, the variable $string will hold a string type value and can later be changed into a variable which will hold an integer type value. PHP itself doesn’t put any restriction on this type of action.

[php] <?php

$string = ‘Hello!’;

echo $string;

$string = 10;

echo $string;

?>[/php]

But in a strongly-typed languages, we have a strict data type attached with each and every single variable and if we need to change the type of the variables then we need to “type cast” them. This process of converting a variable into another type of variable is known as “Type Casting”.

But in PHP we do not have this constraint which adds a lot of flexibility and dynamic capabilities to PHP, but can also lead to a lot of problems if not managed properly. Many PHP developers do not properly manage their code due to PHPs loosely-types nature which leads to a very poorly written code.

If you are coming from a strongly-typed programming language background, then it’s very unlikely for you to make these kind of mistakes, but if you are starting out with a loosely-typed language then there is a huge possibility for you to jump into errors right from the beginning.

You surely do not want your application to break just because of your boolean value of “true” getting converted into a string value of “true” and a comparison not working at all. All you need to do is remain consistent with the variables and their types throughout your application and NEVER EVER CHANGE THE TYPES if not extremely necessary and if you are not aware of what you are actually doing.

Variables Scope in PHP

In PHP, we can divide or modularize a large program into small parts which are known as functions. This part of the course is surely not about functions, so we are not going to go deep into it but you need to have at least a bit about functions before you start learning about variables scope.

In PHP, we can declare variables in any part of our script as we want but cannot use or reference it from every part of the program as we wish as there are certain restrictions. Thus, the scope of a variable is the part of the script from where a variable can be used or referenced.

There are three types of variable scopes in PHP,

i) Local Scope

ii) Global Scope

iii) Static Scope

Variables which are declared outside functions are known as Global variables and the variables which are declared inside functions are known as local variables. Global variables can only be accessed outside the functions and local variables can only be accessed inside the functions.

Let us illustrate this with the help of an example,

[php] <?php

$a = "Global";

function varTest(){

$b = "Local";

echo ‘The Variable is ‘.$a.'</br>’;

echo ‘The Variable is ‘.$b.'</br>’;

}

varTest();

echo ‘The Variable is ‘.$a.'</br>’;

echo ‘The Variable is ‘.$b.'</br>’;

?> [/php]

scope_of_variables

From this example, we see that $a is a global variable and $b is a local variable.

When we output the values we see that inside the function we only get the value of $b (as it is the local variable and accessible only inside the function) and not $a (as it is global and only accessible outside functions).

We can make a simple change in the previous code and can access the global variable inside functions also by using a special keyword known as “global”.

[php] <?php

$a = "Global";

function varTest(){

global $a;

$b = "Local";

echo ‘The Variable is ‘.$a.'</br>’;

echo ‘The Variable is ‘.$b.'</br>’;

}

varTest();

echo ‘The Variable is ‘.$a.'</br>’;

echo ‘The Variable is ‘.$b.'</br>’;

?> [/php]

variables_scope_global_keyword

There is also another way for you to access the global variables from inside the functions as PHP stores all its local variables inside an array (we would discuss about it later in detail) known as $GLOBALS[index], where index is the name of the global variable. We can make use of this global array inside functions to access and modify global variables with ease.

[php] <?php

$a = "Global";

function varTest(){

$b = "Local";

echo ‘The Variable is ‘.$GLOBALS[‘a’].'</br>’;

echo ‘The Variable is ‘.$b.'</br>’;

}

varTest();

echo ‘The Variable is ‘.$a.'</br>’;

echo ‘The Variable is ‘.$b.'</br>’;

?>

&nbsp;

Now, let us discuss a few words on the “static” scope. When a function is “called” in PHP, its local variables are created and when the function have completed its execution then all the local variables are deleted. But there are some situations, when you will not want your local variables to get deleted. This is where you can use the “static” keyword.

For example, we can create a counter like this using the static keyword,

[php] <?php

function staticTest(){

static $counter = 0;

echo $counter.'</br>’;

$counter++;

}

staticTest();

staticTest();

staticTest();

staticTest();

staticTest();

?> [/php]

static_variable

Just remove the “static” keyword from the $counter variable and see the difference that it makes.

Concatenation in PHP

Concatenation is just joining of strings together and in PHP this is as easy as using a “.” operator. This operator is mainly used to concatenate strings, but can also be used to concatenate variables which we are going to discuss in this section.

[php] <?php

echo ‘Concatenation operation in ‘.’PHP ‘.’is super easy.’;

?> [/php]

We can also perform this concatenation operation with variables. Let us illustrate this example with a simple and intuitive example,

[php] <?php

$day = 1;

$month = January;

$year = 2014;

echo ‘Date is: ‘.$day.’, ‘.$month.’, ‘.$year.'</br>’;

?> [/php]

Here we have used three variables and have used the concatenation operation to display them in a single echo statement. We can get a bit more creative by using html inside it too.

[php] <?php

$day = 1;

$month = January;

$year = 2014;

echo ‘Date is: <strong>’.$day.’, ‘.$month.’, ‘.$year.'</strong></br>’;

?> [/php]

We can also concatenate variables without using the “.”operator by simply using the double-quotation marks instead of the single quotation marks. Like,

[php] <?php

$day = 1;

$month = January;

$year = 2014;

echo "Date is: <strong>$day, $month, $year</strong></br>";

?> [/php]

Error Reporting in PHP

When you are writing PHP scripts, you will surely run into one or more errors. Errors are nothing more than abnormal situations in your application which you never want to let happen. You should always try to minimize the no. of errors your code produces and make it perfect with absolutely zero errors.

Generally, we can categorize errors into two types –

1)    Syntax Errors – Syntax is the structure or the grammar of your programming language. There is a set of rules in every language, even in languages like English that we humans use.  If you make a mistake in maintaining the proper rules of the language in which you are coding, then you are surely going to run into some sort of errors and your application would probably break.

For example –

[php] <?php

Echo ‘Hello’

echo ‘Welcome to corePHP’;

?> [/php]

We have deliberately missed the semi-colon and this should produce an error.

2)    Semantic Errors – These are more critical errors which your compiler or interpreter will probably never complain about, but your co-developers or clients surely will. Semantics is all about the logic or the meaning of your code and what it actually does.

For example,

[php] <?php

$balance = 1000;

if(balance <0)

echo ‘You have full balance in your account.’;

?> [/php]

Just have a look at the logic of this code, which outputs “You have full balance in your account” when the balance goes negative. Is that really possible in reality?

The Last Words

There are lots of interesting things to be discussed about errors and error reporting in PHP. We will surely deal a bit more with it in the very next part of our course which is going to be very essential for you continue learning PHP.

So the next article is going to be even more interesting when we are also going to learn about some decision making statements in PHP too. Stay tuned and see you in the next episode. Till then happy coding.

STAY UP TO DATE

Sign up today to stay informed with industry news & trends