Tag Archives: PHP Course

Looping Statements and Arrays in PHP – Part VII

PHP-Banner

Till now, we have learned quite a bit of PHP and are capable of writing some decent applications too. Now in this part of the course, we will learn about two very important things in almost any programming language – loops and arrays.

Firstly, you might be thinking what “loops” are and why do we need to learn it? Looping is nothing but a way to save yourself from the tedious job of doing many repetitive tasks. Quite often we need to perform a particular action (or a set of actions) repeatedly for a large no. of times and it is sometimes absolutely impossible to do that manually. Looping is just the way to automate the job of performing a task repeatedly for any no. of times with ease. Continue reading

Almost Everything about Operators in PHP – Part VI

PHP-Banner

In the previous part of the course, we have learned about error reporting and various decision making statements available in PHP. But that is obviously not enough for us to create useful and dynamic applications as we need to learn about various operators available in PHP and how to make proper use of them to bring various actions into play.

You should be a bit familiar with the word “operator” as I have already used it while discussing about concatenation.

In this part of the course, we will learn almost everything we can do with operators in PHP. But before moving on to the various types of operators, let us learn what “operators” actually mean. Operators are language constructs (composed of special characters or combination of characters) which behaves typically like functions but differ syntactically and semantically from ordinary functions.

They can be used to perform various nifty “operations” quickly and easily, and that’s why they are known as “Operators”. Continue reading

Error Reporting & Decision Making in PHP – Part V

PHP-Banner

In the previous part of the course, we have learnt a bit about errors in PHP. In this part of the course, we will dig deeper into various types of errors and error reporting in PHP. We will also learn about decision making statements in PHP and how we can use them to dynamically perform various actions depending upon various test conditions. Continue reading

More on Variables and Error Reporting in PHP – Part IV

PHP-Banner

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

$string = ‘Hello’;

echo $string;

?> 

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

$string = 'Hello!';

echo $string;

$string = 10;

echo $string;

?>

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

$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>';

?> 

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

$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>';

?> 

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

$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,

1 <?php

function staticTest(){

static $counter = 0;

echo $counter.'</br>';

$counter++;

}

staticTest();

staticTest();

staticTest();

staticTest();

staticTest();

?> 

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

echo 'Concatenation operation in '.'PHP '.'is super easy.';

?> 

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

 <?php

$day = 1;

$month = January;

$year = 2014;

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

?> 

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

$day = 1;

$month = January;

$year = 2014;

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

?> 

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

 <?php

$day = 1;

$month = January;

$year = 2014;

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

?> 

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

Echo ‘Hello’

echo ‘Welcome to corePHP’;

?> 

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

$balance = 1000;

if(balance <0)

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

?> 

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.

Diving Deeper into PHP – Part III

php-banner-3

Till now, we have learned quite a bit about PHP and have also created out first PHP file. Now, it’s time for us to dive deeper into PHP and learn some more interesting stuffs. In this part of the course, we will be learning about php.ini configuration file, phpinfo() function, outputting HTML using PHP and also about using variables in PHP.

You will possibly never lose the interest reading this article, as there are a variety of things for you to learn this time. So sit back, relax and just give your full attention to this article.

The Importance of php.ini File and phpinfo() Function

php.ini – The PHP Configuration File Continue reading

Creating Your First PHP File – Part II

PHP-Banner-2

In the previous part of the course, we have learned about the basics of PHP and how to set up a local web server using XAMPP. Now, we are officially ready to create our first PHP file and run it in our web server.

In this part of the course, we will learn in-depth about creating a PHP file, printing data in it and testing it in the web server. It is very important to give your full attention right now, because if the basic foundation becomes weak then everything you try to do later will become much more difficult. We will also learn a bit about the importance of maintaining proper coding standards through commenting and indentation. Continue reading

What is PHP & How to Set-up a PHP Web Server? – Part – I

PHP-Banner-Part-1

Welcome to the first part of the “PHP for Beginners” course. In this course, my aim is to make you understand almost everything you need to learn to get started with PHP. It is mainly targeted for the absolute beginners who are just going to start learning PHP and also for the intermediate learners who have a little knowledge of development with PHP.

I would try to explain everything in-depth and also in a very simple and lucid language, so that anyone can easily understand what’s really going on and enhance their learning curve. I won’t go into any complex aspects of PHP like OOP (Object Oriented Programming) approach with PHP as they are absolutely beyond the scope of this “beginners” course but will surely be covered in future courses I do.

Before starting this course, I would like to throw some light on what PHP actually is and how it works? Without a proper understanding of this, you will never be able to feel the real essence of PHP. I would also discuss the step-by-step process of setting up your local server which you will need to develop PHP applications faster. Continue reading