PHP Day #2 - Guaranteed Web-development course in 15 Days

PHP Day #2 - Guaranteed Web-development course in 15 Days

Day 2:

Welcome to PHP in 15 Days – Guaranteed Training Course day 2. Today we are going to study about 3 very important and basic things. These things are going to lay the foundation and you know if the foundation is strong, building built on this will be really strong. So, pay deep attention and try to learn them.

First thing we will study today is Data Types, second is Constants and third one is operators.

Table of Content Day 2

·         Data Types

·         Constants

·         Operators (Assignment, Arithmetic, Comparison, increment/ decrement, Logical)

Data Types:

If you Google the internet or go to you will find there are several data-types in PHP. I am sure if you will try to accumulate all of the data types right now, you will get confused or may be run away from learning PHP altogether. So, don’t bother to understand all of those. I am going to teach you onlythose data types which are most frequently used.

Here are some data types that we will studyabout.

·         Booleans   (true, false) or (1, 0)

·         Integers / Numbers   (…-2,-1,0, 1, 2, 3 …  so on)

·         Floating Point Numbers   (1.23, 1.11908)

·         Strings   (‘This is something’)

·         Arrays   (Collection of variables)

·         Null   (Null / Nothing)


So, before defining these types, we need know about a built in function. This function will help usgetting the data type name on any value we’ll supply to it. We will study about built in or internal functions in detail later on in a coming sessions in this training course. So, gettype(); This is an internal function. Its purpose is just to tell us the exact typeof any value we put in its parenthesis. If you are unable to understand this for now, just watch me, what I do, and don’t bother to understand it right away.Just focus on data-types.

·         Boolean   (true, false) or (1, 0)


First Data Type is Boolean.

Thistype is only true and false. These true and false are same as we have in ourday to day life. As an example, if you answer to a question, either its true or false, your examiner will tell you. So these true and false let us make some decisions to do something. So, for now just keep in mind this type is true or false. So we will ask something from PHP and PHP will tell us that, is it true or false.

Let’s just put true in internal function. Let’s go to the browser and put the path to our recent file in which we are working right now.



Hit enter and no output. The reason is we did not echo it out. Let’s just echo it out and check this in browser again.



This time you can see the type, ‘Boolean’. Let’s change true to false and again you can see the output ‘Boolean’.



Now I ask this function about

Is 6 > 5 = true

Is 5 > 6 = false

get_type(5>6); Boolean false

get_type(true); Boolean true

get_type(false); Boolean false

‘true’ always returns the result 1 and false though doesn’t show up anything as a result, but remember its always 0


·         Integers / Numbers   (…-2,-1,0, 1, 2, 3 …  so on)

Another data type is integers. Any whole number in PHP is known asinteger. These are simple counting numbers you count in your normal life like 1 2 3 4 etc

Let’s check data type for 6

echogettype(4);and this is integer. Let’s just put some br tags between these results to output them line by line. You can see here it’s an integer.


¡  Floating Point Numbers   (1.23, 1.11908)

Let’s put a fraction value in the function



You can see its double. So, any values having point within it or fraction are called floats or doubles

·         Strings   (‘This is something’)

Now let’s put some text within the gettype.

      echogettype('Hello World!');


This is an example of string. So anything like, sentence or words or characters are strings. You must need to enclose strings within quotes either single or double.

A single character, the Combination of characters or a single space between characters is a string.Also, if you don’t put anything within single quotes, that is also a string.

·         Arrays   (Collection of data)

Array can be defined as a collection of data sets at one place or in one single variable.You can see in a variable we have only one value. Like

$a = 12;

So, this variable can have only one value.

But within an array we can have many values. Let’s create an array

array(12,14, 17, 20);

So, this array is another data type.


·         Null   (Null / Nothing)

This is nothing. When you set anything to this, null, it means you have undefined it if it wasdefined before.

Let’s have a look.

Define a variable


And set it to some value

$a = 6;

Now just

echo $a;

now set it equals to null

$a = null;

Now try to print $a

echo $a;

and there we go it’s saying error of call to undefined variable.

So, null means nothing, it’s really straight forward.


·         Constant is like a variable as we assign a name and value to it.

·         Constant value remains same every time and it’s not changeable

·         Procedure of assigning or defining a constant is a bit different than of a variable.

·         define(‘constant-name’, ‘constant-value’);

·         Calling the constant is just by constant-name without a $ sign.

Now let’s define a constant. First we need to put the keyword ‘define’. ‘define’ is a special keyword which is used to define a constant and then parenthesis. Between parentheses we need to supply two arguments or parameters. First one is the name of our constant, we want to define and it must be a string. Let’s put ‘MyContant’. You can name the constant keeping in mind the naming standards that we used to define a variable name. Second parameter is the value we want to assign to our constant. Let’s just put my facebook page link here. Value can be of any data type, no restriction.

      define('MyConstan', ‘PHP in 15 Days’);


Now we can easily call our constant by just echo the name and don’t need to write the name with in quotes or we don’t need a $ sign to call the constant.


Check this out in the browser. You can see the value of our constant i.e. ‘PHP in 15 Days’. Remember that the name of Constant is case sensitive, so if you will change the case PHP will not be able to recognize it. If you want to make case sensitive free, just provide a third parameter ‘true’ and then you can call it by using any letter case.

Now let’s change our Constant value to an integer to prove that after defining the constant we cannot make a change in its value.

      define('MyConstan', 12);


Now, let’s try to add 1 in our constant. This double plus sign means add 1 in the following value. Now, let’s have a look in the browser.



You can see we have an error. This is because we are adding in constant value which is nothing but an error. So, constant always remains constant after being defined.

Assignment Operator – Operators

·         Only assignment operator is = (equal sign).

·         Example: $a = 6;

·         Don’t get confused. This doesn’t mean $a is equal to 6.

·         It means, assign value of 6 to variable $a.

·         Whenever we’ll call $a will show value of 6 till its changed or removed.

·         So, = is an assignment operator which assigns right side value to the left side variable.

There is only one assignment operator and that is = sign.

Lets make a variable

$a = 6;

This doesn’t means that $a is equal to 6, but this statement is saying that assign the right hand value to the left side variable. So keep in mind whenever you use = sign it means in PHPto assign right hand side value to left side. So, that’s why when you echo $a it gives you 6

echo $a;

Because we have assigned 6 to $a. So simply, = is an assignment operator.

Arithmetic Operators – Operators

I am sure, every one of you already know all of these Arithmetic Operators and their functionality. So, just to recall we will go through these and have a quick look.

·         -$a Negation. This is Opposite of $a.

- sign is used to get the negative value of the same value or variable. Let’s try it

Let’s first define two variables first $a = 6; and $b = 4;

Now just put the minus sign before variable a and just echo it out. Let’s check the result in the browser. You can see this is just the opposite value that we assigned to variable a.



·         $a + $b Addition. It’s Sum of $a and$b.

Most of the arithmetic operators have the same meaning and functionality which actual arithmetic defines. So when +(plus sign) comes between two values it means addition.

Now, Let’s just try to add our variables $a  and $b by putting plus sign between these;

      echo$a + $b;

next one is;

·         $a - $b Subtraction. The Difference of $a and$b.

echo$a - $b;


Just like addition its arithmetic’s subtraction


·         $a * $b Multiplication. It’s the Product of $a and$b.

·         $a / $b Division. The Quotient of $a and$b.

And same for multiplication and division, startic for multiplication and a slash for division.

·         $a % $b Modulus. This returns Remainder of $a divided by $b.

This %(percent) sign is not for percentage its modulus. This will divide first value with second one and will return the remainder. In this case we’ll get 2


·         $a ** $b Exponentiation. The Result of raising $a to the $b'th power. Example: 52 $a will work like 5 and $b will work like 2 in the example. This is Introduced in PHP 5.6.

Let’s try this

echo‘Exponential: ’  $a ** $b; 

it will obviously give us result by raising 6 to its 4th power.

Now let’s check all of the results in our browser

10 is resultant variable a + b

2 is the result of variable a - b

24 is the result of variable a multiplied by b

1.5 is the result of variable a divided by b

2 is the remainder after dividing variable a with b

1296 is the result exponent that is 6 raise to power 4. This expression works like $a*$a*$a*$a. Just in case you don’t know what it is? This is all for arithmetic operators, as I already have told you learning PHP is just like the arithmetic of 5th Standard or below.

Comparison Operators – Operators

As from the name Comparison Operators these operators used to compare different variables and values. Result of this comparison is always a true or false data type, and we do or donot do something in response. Let’s study these. 

·         $a == $b is Equals to.  TRUE if $a is equal to $b.

Double equal sign figures out,if left hand side equals in value to right hand side. So, if they are equal result will be true otherwise false. So here we are asking, if variable a is equals to the value of variable b.

      echo($a == $b);

Let’s check the output in the browser, refresh the page and we did not get anything, it means it’s false. The reason is variable a is not equals to the value of variable b as we defined these before. Let’s redefine these again.

$a = 4;
$b = 4;


      echo($a == $b);

Let’s check this out again in the browser and you can see now we get 1. It means it’s true now.

·         $a === $b Identical.  TRUE if $a is equal to $b.

=== sign checks if the value and type as well equals to each other.

      echo($a ===$b);

Let’s check it out in browser and here we get 1, that is true. As 4 is equals to 4 in value as well as type as both variables a and b have integers with same value. Now, if I change type of the value of variable b to string by just putting single quotes around 4. Now this ‘4’ has string data type. Now if we check

$a = 4;
$b = ‘4’;


echo($a ===$b);

And now you can see we don’t get anything, so it’s false. The reason is 4 is equals to 4 as a value but the type of $a is integer and the type of $b is string.

Now let’s check for all other comparioson operators.

·         $a != $b Not equal.  TRUE if $a is not equal to $b.

echo($a != $b);

Here we are asking if variabl a is not equals to ariable b then provide us true. Let’s check this out. And we don’t find anything, it’s false. Because both the variables equals to each other as far as the values is concerned. If I change the value of variable b to 3 or something other than 4 this will result us true. Let’s check this in browser and you can see it’s true.

·         $a <> $b Not equal.  TRUE if $a is not equal to $b.

Just keep previous comparison operators too and put br tags between them. So, we can check all of these once. Not equals can also be written or defined in another way, i.e. less than sign and greater than sign together.

echo($a <>$b);

if we compare using double equals sign with sign of exclaimation, this will check the type too. If the type is not equal or the value is not equal it will give us true. So, this operator will check for two things at once.

Next is

·         $a !== $b Not identical.  TRUE if $a is not equal to $b, or they are not of the same type.

echo($a !==$b);

Let’s put some text with all of these and concatenate with the result.

Not Identical, Not equal, not equal and equal

Now if we check you can see all the results. Equal is true and rest of all are false. If we change the type, let’s change type of variable b to string and the last one not identical will become true. Here we go, you can see it’s true.

Now let’s just put most often used comparison operators and see their result in the browser for our variables.

·         $a < $b Less than.  TRUE if $a is strictly less than $b.

echo($a <$b);

If variable a is less then variable b only then this will give us true.

·         $a > $b Greater than.  TRUE if $a is strictly greater than $b.

echo($a >$b);

If variable a is greater than variable b this will give us true

·         $a <= $b Less than or equal to.  TRUE if $a is less than or equal to $b.

echo($a <=$b);

This operator will check for two things either if variable a is less then b it will produce true or if variable a is equals to variable b it will return true. So, this will return false only if the variable a is greater than b.

·         $a >= $b Greater than or equal to. TRUE if $a is greater than or equal to $b.

echo($a >=$b);

This operator will return true only either a is greater or equals to b and will return false if the variable a is less than b.

Let’s put text along with these, so that we can identify every single result in the browser.

‘Less than: ’, ‘Greater than: ’, ‘Less than or equals to’, ‘Greater than or equals to: ’ OK. Now let’s go to the browser and you can see the results accordingly as expected. Now just change variable b to integer 3.

You can see ‘not equal’, ‘not identical’ and greater than true. Also, here, greater than or equals to also true as variable a is greater than b.


Incrementing / Decrementing Operators – Operators

These are really straight forward operators and no hassle while understanding them.

These are only two types of operators increment (++) and decrement (--)

Let’s start with increment operator. From the name of this operator you might have guessed that it’s used to increase the value of an integer or variable having integer value. This operator has two types. Pre-increment and post-increment.

·         Pre-Increment         ++$a

·         Post-Increment       $a++

Let’s create a variable a and assign it value 6. For pre-increment we just place two plus signs together before calling this variable. So, this will be incremented by 1.



Now let’s just check this out in the browser and you can see it’s 7, so, it’s been incremented by 1. This is pre-increment. Now if we remover these pre-plus signs and add post plus signs here and echo this same calling right away. This will result us 6.



Here we go, it’s 6. Because we are calling our variable before incrementing. That’s why it gives us value and then it’s incremented.

Now if we echo our variable a it will show us 7 as it was post incremented. Let’s echo variable a.

      echo'After Post increment variable a : '. $a;


Check this out in browser and you can see its 7 now. You can recall prefix and suffix you must have read while you learn the English grammar. So, same is the case with pre and post.

Now let’s change the post increment back to pre-increment again and see what happens. You can see the difference that at the time of increment it’s displayed as 7 and after that it stills remains 7, but post increment at the time of increment remains the same it was defined before and after that process it is incremented.

Now same is the case with decrement. It will decrease the value or integer by 1. Pre and Post processes are same. We use double minus sign for decrement.

·         Pre-Decrement        --$a

·         Post-Decrement$a--

Let’s define a variable b and give it an integer value 4.

      $b = 4;

Now let’s decrement it and put the double minus before calling the variable. So, we are doing the pre-decrement.


Let’s just put some br tags to separate these. Now, let’s check this in browser. Refresh and you can see we get 3 here. I hope you understand why is this so. Because we are pre-decrementing the variable before calling, so first, 1 is subtracted from the value and then it’s displayed. Let’s just check it after being decremented.

      echo'After pre-decrement b: '. $b;


Let’s go to the browser and you can see both the times it’s 3. Now if I place decrement sign after calling the variable it will make it post decrement. Let’s change it.


Now it has become post decrement. Let’s check this out in browser. You can see at time of decrementing it’s 4 because variable was called prior to decrementing it’s value by 1. So, it’s 4 here and right after that it became 3. This is all for increment and decrement operators.

Logical Operators – Operators

This type of operators is the last one of the operators. These operators are mostcommonly used operators and most of the logic in PHP we create using these operators combiningthese with other operators.

So let’s dive into it.There are only three logical operators in total. AND, OR and XOR operator.

AND (&&) Logical Operator:

·         $a AND $b   : TRUE if both $a and$b are TRUE.

·         $a && $b      : “And” TRUE if both $a and$b are true.

Let’s create three variables.

$a = 2;
$b = 9;
$c = 21;


Now, let’s make a statement

      echo($a <$b);


You know this will produce true as a result, as variable a is less than b. Now, what if we need to check two or more conditions like this one simultaneously and based on all of those we want to get true and false. Here comes our logical operator in action. Let’s put AND after this and put another comparison.

      echo($a <$b) AND ($b <$c);


This is the combination of logical and comparison operators. So the first statement true as a is less than b and the second statmen is also true as variable b is less than c. So, both are true. This AND logical operator checks both of the conditions and if both are true it will produce true.  Let’s check this out in browser. It should give us 1 as both are true.And rightly so, it gives us 1. Now, let’s go back and make one of the statement false. Let’s put greater than sign. Variable a is not greater than b so, it’s false. Let’s now check this out in browser and here we have nothing means AND logical operator is resulting in false.

AND can be defined as double ampersand sign and works same like AND logical operator. So, only notation difference is there and functionality is same. Let’s just make a false statement here.

      echo($a >$b) &&($b <$c);


Check this out in browser and you can see only one true here. For this statement we get true

      echo($a <$b) AND ($b <$c);

and for this

      echo($a >$b) &&($b <$c);


we get false.

Now let’s just make the second logical statement true as well. Just put not equals sign for comparison and we know these are not equal to each other. So now these both comparison statements are true, so the logical operator should give us true as well.

echo($a <>$b) &&($b !=$c);

Let’s check this in the browser one last time, and you can see two ones for echo true logical statements.

So, finally just remember if everything is true while using AND we will get true, if any of the statements is false while using AND, resultant will be false.

OR (||) Logical Operator:

·         $a OR $b      : TRUE if either $a or $b is TRUE.

·         $a || $b        : “OR” TRUE if either $a or$b is TRUE.

This logical operator is slightly different from(&&)ANDoperator. Let’s use previously defined variables a, b and c for demonstration. Let’s write a true statement variable a is less than b and then OR and then another true statement variable b is less than c.

      echo($a <$b) OR ($b <$c);


So,logical operatorplay the role to checkeither any of the statement is true. If it finds one, it will return true, no matter the other is true or false. So, in this scenario only one statement needs to be true to make logic true.

Let’s check this out within the browser. Hit refresh, and we get 1, it means it’s true. Although, both of the comparison statements are true.

Now let’s make our first comparison statement false, variable a is greater than b, which is not.

      echo($a >$b) OR ($b <$c);


Let’s now check the result in the browser and you can see we still get 1.It means the logical operator OR is declaring true if any one of the statements is true.

Now let’s make the second statement false as well. Variable b is greater than c, which is not.

      echo($a >$b) OR ($b >$c);


Now both the statements are false. Now it should give the result false. Let’s find it and rightly so. We don’t get 1 here. So, we must have one statement true within our all statements when we use OR logical operator to get true.

We can write OR as double pipe sign. There is no difference at all as far as the functionality is concerned. Let’s just try it once. Copy paste the above statement and change it a bit. Variable b not equals to variable c, which is true and the first statement variable a is greater than b which is false. Just replace the OR with double pipe sign.

echo($a >$b) ||($b <>$c);


and we get 1 here as expected. Let’s comment these all out.

XOR Logical Operator:

Now let’s move to our final logical operator that is XOR, which is quite interesting one.

·         $a XOR $b    : TRUE if either $a or$b is TRUE, but not both.

This is a logical operator will return true if one and only one condition is true and rest of all are false. If both of the comparison statements are true or false it will return false.

Let’s again useour previously defined variables a, b and c.

Let’s define the comparison statement, variable a is not equals to b, XOR, variable b not equals to variable c. Both of these statements are true.

        echo($a != $b) XOR ($b !=$c);

This should return false and rightly so, we get nothing here. It means logical operator returned us false. Now let’s make false statement by just variable b equals to variable c, which is false.

      echo($a != $b) XOR ($b == $c);

Now we have one true and one false statement, now it must return true. Let’s check this out in browser and here we go, we get 1 here.

And if we make both of the statements false, XOR will return us false. Let’s make our first statement false as well.

      echo($a == $b) XOR ($b == $c);

Get back to the browser, refresh and you can see it’s false.

This is it for logical operators as well as for today’s session. I hope you learnt these thing, don’t forget to practice these. Will see you in day 3, take care, bye.

Practice files Day #2


Related Posts:

Leave a reply

Required fields are marked *

Login to Post Comment