1. Patterns and Variables

Basics of PHP – Module 2

Data Types available in PHP

PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:

  1. Scalar Types (predefined): It holds only single value. There are 4 scalar data types in PHP.
    1. boolean
    2. integer
    3. float
    4. string
  2. Compound Types (user-defined): It can hold multiple values. There are 2 compound data types in PHP.
  3. array
  4. object

Special Types: There are 2 special data types in PHP.

  1. resource
  2. NULL

Note: Resources are not the exact data type in PHP. Basically, these are used to store some function calls or references to external PHP resources. For example - a database call. It is an external resource.

Null is a special data type that has only one value: NULL.

Variables in PHP

Variables are the identifier of the memory location, which used to save data temporarily for later use in the program. The purpose of variables is to store data in memory for later use.

We can make any variable types, like numbers, text strings, and arrays. All variables in PHP start with a $ dollar sign, and the value can be assigned by using the = assignment operator.

PHP Variables are case sensitive, variables always defined with a $, and they must start with an underscore or a letter (no number).

In PHP, unlike other languages, variables do not have to be declared before assigning a value.

You also don't require to declare data types, because PHP automatically converts variable to data types depends upon its value.

Constants in PHP

Constants are like a variable, except that their value never changes during execution once defined.

The constant value is immutable.

Constant within a script is accessible to any area; however, they can be scalar values.

Constant names are case-sensitive, and they also follow the same naming requirements like variables, except the leading $.

It is considered best practice to define constants using only upper-case names.

PHP define() function defines a constant.

Types of Operators in PHP

PHP offers various types of operators having different functioning capabilities.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Concatenation Operators

Comments in PHP

Single Line Comments

// (Single line comment)

Multi Line Comments

/* (Multi line comment) */

1. Patterns and Variables

The branch of mathematics in which we studied numbers is arithmetic.    
In Arithmetic, numerals 1, 2, 3, 4 , -------- etc. and four fundamental operations : addition , subtraction, multiplication and division are used to deal with various problems. In Algebra, in addition to numerals, we use letters such as x, y, z in various situations to solve the problems.

Matchstick Patterns
Ram and Sarita are making patterns with matchsticks. They decide to make simple patterns of the letters of the English alphabet. Ram takes two matchsticks and forms the letter L as shown in Fig (1). Then Sarita also picks two sticks, forms another letter L and puts it next to the one made by Ram Fig (2). Then Ram adds one more L and this goes on as shown by the dots in Fig (3).

Number of matchsticks required = 2 × number of Ls. For convenience, let us write the letter n for the number of Ls. Thus, n can be any natural number 1, 2,  3, 4, 5, .... We then write, Number of matchsticks required = 2 × n. Instead of writing 2 × n, we write 2n. Note that 2n is same as 2 × n.

1. Patterns and Variables

Chapter 11


Patterns and Variables

Introduction – Algebra
Algebra is the branch of Maths that uses alphabetical letters to find unknown numbers. These letters are also called variables. The values, which are known in the given expression similar as numbers, are called constants.

Algebra involves the simple operation of mathematics like addition, subtraction, addition, and division involving both constant as well as variables. For case, x +10 = 0.

Number patterns
Still, again, its successor is (n +1); if a natural, number is denoted by n.
Case Successor of n = 10 is n +1 = 11.
Still, also 2n is an indeed number and (2n +1) is an odd number, If a natural number is denoted by n.
If n = 10, further 2n = 20 is an even number and 2n +1 = 21 is an odd number.

Patterns using matchsticks/Ice candy sticks,
Matchstick Patterns
 No. of matchsticks used to make 1st square = 4
 No. of matchsticks used to make 2nd square = 7
 No. of matchsticks used to make 3rd square = 10
 Therefore, the pattern that we observe now is 3n +1
With this pattern, we can freely find the number of matchsticks needed in any number of squares.

How numerous matchsticks will be used in the 50th figure?

 3n+ 1
 3 × 50 1
 = 151 matchsticks

More about variables,

A variable is an unknown volume that is prone to change with the environment of a situation.
Example In the expression 2x+5, x is the variable.
Examples on variables
Examples of Variables are:
3 + x = 2
X = 3
(a+b) ² = a² + b² + 2ab

2. Expressions and Equations

In the above example, we found a rule to give the number of matchsticks required to make a pattern of Ls. The rule was: Number of matchsticks required = 2n Here, n is the number of Ls in the pattern, and n takes values 1, 2, 3, 4,.... 
Let us look at Table 1 once again. In the table, the value of n goes on changing (increasing). As a result, the number of matchsticks required also goes on changing (increasing). 
n is an example of a variable. Its value is not fixed; it can take any value 1, 2, 3, 4, ... . We wrote the rule for the number of matchsticks required using the variable n.
The word ‘variable’ means something that can vary, i.e. change. The value of a variable is not fixed. 
It can take different values.
Examples of variables : We have used the letter n to show a variable. There is nothing special 
about n, any letter can be used. One may use any letter as m, l, p, x, y, z etc. to show a variable. Remember, a variable is a number which does not have a fixed value. For example, the number 5 or the number 100 or any other given number is not a variable. They have fixed values. Similarly, the number of angles of a triangle has a fixed value i.e. 3. It is not a variable. The number of corners of a quadrilateral (4) is fixed; it is also not a variable. But n in the examples we have looked is a variable. It takes on various values 1, 2, 3, 4, ... .    

Let us take other example of variable : Raju and Balu are brothers. Balu is younger than Raju by 3 years. When Raju is 12 years old, Balu is 9 years old. When Raju is 15 years old, Balu is 12 years old. We do not know Raju’s age exactly. It may have any value. Let x denote Raju’s age in years, x is a variable. If Raju’s age in years is x, then Balu’s age in years is (x – 3). The expression (x – 3) is read as x minus three. As you would expect, when x is 12, (x – 3) is 9 and when x is 15, (x – 3) is 12.

Expressions with Variables
To form expressions we use all the four number operations of addition, subtraction, multiplication and division. For example, to form (2 × 10) + 3, we have multiplied 2 by 10 and then added 3 to the product. Expressions can be formed from variables too. In fact, we already have seen expressions with variables, for example: 2n, 5m, x + 10, x – 3 etc. These expressions with variables are obtained by operations of addition, subtraction, multiplication and division on variables. For example, the expression 2n is formed by multiplying the variable n by 2; the expression (x + 10) is formed by adding 10 to the variable x and so on.
One important point must be noted regarding the expressions containing variables. A number expression like (4 × 3) + 5 can be immediately evaluated as (4 × 3) + 5 = 12 + 5 = 17. But an expression like 
(4x + 5), which contains the variable x, cannot be evaluated. Only if x is given some value, an expression like (4x + 5) can be evaluated. For example, when x = 3, 4x + 5 = (4 × 3) + 5 = 17 as found above.

Ex.1     Give expressions for the following cases.
(a)     Rita scores x marks in Maths and 46 marks in English. What is her total score in Maths and English. 
 (b)     The difference of x and 9 where x > 9.
(c)     The product of a and b  added to the difference of a and b (a > b).
(d)     One-half of a multiplied by the sum of x and y.

Ex.2 Ali is x years old. Express the following in algebraic form : 
(a)     4 times Ali's age 3 years hence. 
(b)      The present age of Ali's aunt who is four times as old as Ali will be 5 years from today. 
(c)     The present age of Ali's father who is 5 times as old as Ali was 3 years ago. 

Sol.   (a)     Ali's age 3 years hence = (x + 3) years 4 times Ali's age 3 years hence = 4(x + 3) years. 
(b)      Ali's age after 5 years = (x + 5) years. Ali's aunt age = 4 (x + 5) years   
(c)     Ali's age before 3 years = (x – 3) years. Ali's father age = 5(x – 3) years

2. Expressions and Equations

Expressions and Equations

Expressions using variables

Terms of an Expression

 Parts of an expression that are formed independently first and again added or deducted are known as terms.

 In the above- given instance, terms 2x and 5 are added to form the expression (2x+5).

 Factors of a term

 Parts of an expression that are formed independently first and also added or deducted are known as terms.

 Factors of a term are amounts that cannot be further factorised.

 In the above- given instance, factors of the term 2 x are 2 and x.

 Coefficient of a term

 The numerical factor of a term is called the measure of the term.

 In the above- given illustration, 2 is the coefficient of the term 2x.

 Like and Unlike Terms

 Like terms

 Illustration 8xy and 3xy are like terms.

 Unlike terms

 Terms having different variables are called, unlike terms.

 Illustration 7xy and-3 x are unlike terms.

 Formation of an expression

Formation of Algebraic Expressions
Combinations of variables, constants, and operators constitute an algebraic expression.

 Illustration 2x+ 3, 3y +4xy, etc.

 Addition and Deduction of Algebraic Expressions

Addition and Deduction of like terms

Sum of two or more like terms is a like term.

 Its numerical measure will be equal to the sum of the numerical coefficients of all the like terms.


Example 8y + 7y =?
             8y + 7y
 (8 +7) y = 15y

 Difference between two like terms is a like term.

  Its numerical measure will be equal to the difference between the numerical coefficients of the two like terms.

 Illustration 11z − 8z =?


 − 8z



(1- 8) z = 3z


 Addition and Deduction of unlike terms

 For adding or deducting two or more algebraic expressions, as if terms of both the expressions are grouped together and unlike terms are retained as it is.

Addition of − 5x2 +12xy and 7x2 + xy +7x is shown below

 − 5x2 + 12xy

             7x2 + xy +7x



 2x2 +13xy+ 7x

 Deduction of − 5x2 12xy and 7x2 xy 7x is shown below

 − 5x2 +12xy

 − 7x2 +xy +7x



  12 x 2 11xy − 7x


Expressions written in situations

Using Expressions Practically

The use of expressions comes handy in everyday. For e.g. if we know that the price of one kg onion is Rs.5 less than the price of 1kg potato, then we can use expressions to calculate the price of 1kg onion. Let the price of 1kg potato be x. Then the price of 1kg onion if Rs. (x-5).

Similarly, 1. If Seema is 5 years older than Ritu and the age of Ritu is given to be z years. Then Seema’s age is (z + 5) years.


Problem: Answer the following:

Take Sarita’s present age to be y. 

What will be her age 5 years from now?
Her age 5 years from now will be = (y +5) years.
What was her age 3 years back?
Her age 3 years back was = (y – 3) years.

Sarita’s grandfather is six times her age. What is the age of her grandfather?

Age of Sarita’s grandfather = (6*y) years

 3. Grandmother is 2 years younger than grandfather is. What is grandmother's age?

 Age of Sarita’s grandmother = (Age of grandfather – 2) years

                                                  = (6*y -2) years
4. Sarita’s father’s age is 5 years more than 3 times Sarita’s age. What is?
Her father's age

Age of Sarita’s father = (3y + 5) years.

5. A rectangular box has height its length is 5 times the height and breadth is 10 cm less than the length. Express the length and the breadth of the box in terms of the height.

Height of rectangular box = h cm. (given)

Length of rectangular box = 5*h cm

Breadth of rectangular box = (Length – 10) cm

                                        = (5*h -10) cm.

6. A bus travels at km per hour. It is going from Dasburg to Beespur. After the bus has travelled 5 hours, Beespur is still 20 km away. What is the distance from Dasburg to Beespur? Express it using v.

Speed of the bus = km per hour

Distance travelled in 5 hours = Speed*Time

                                          = Speed (km per hour)* 5 hours

                                          = 5kms

Distance from Dasburg to Beespur =Distance travelled in 5 hours + 20km

                                                 = (5+ 20) kms

What is an equation?

An equation is a condition on a variable, which is satisfied only for a definite value of the variable.

An equality sign separates the left- hand side (LHS) and right- hand side (RHS) of an equation. Hence LHS = RHS.

However, again, it is not an equation; If LHS is not equal to RHS.

Working an Equation

Value of a variable in an equation, which satisfies the equation, is called its result.

Solution to equations

One of the simplest methods of finding the solution of an equation is the trial and error method.





Literal Numbers & Power of literal numbers

Literal Numbers
We have mentioned earlier that the letters represent the numbers. These letters are called literal numbers and obey all the rules of arithmetic. 
Note :  5 × p × q = 5pq. 5, p and q are factors of 5pq. 5 is a numerical factor and p, q are literal factors.

Power of literal numbers 
We have read earlier that 2 x 2 x 2 = 23 and (–3) x (– 3) = (–3)2      Similarly, a x a x a = a3 and (– y) x (– y) x (– y)  x (– y) = (–y)4 
a3 is read as 'a to the power three' or 'a raised to the power three' or 'a cube' or 'third power of a' and (–y)4 is read as '–y to the power four' or '–y raised to the power four' or fourth power of –y'. 
In a3, a is called base and 3 is called exponent or index. 

The number expressed in figures or symbols, standing before an algebraic term as a multiplier is called its coefficient. Thus in 3abc, 3 is the coefficient of abc, 3a is the coefficient of bc and 3ab is the coefficient of c. 

Ex.1     Write down the coefficient of :
(a) x in 3xy    (b) abc in – 5abc     (c) y in 2xyz   (d) a2 in – a2bc 

Sol.     (a) 3y             (b) – 5            (c) 2xz             (d) – bc 

Ex.2 Write down the numerical coefficient in each of the following :
           (a) 5 ab         (b) – 3xyz      (c) px          (d) –y 
Sol.    (a) 5              (b) –3            (c) 1            (d) – 1 

Ex.3     If a = 2, b = 3, c = 4, find the value of : 
           (a)     3a – b + 2c 
           (b)     a2 – b2 + c2        
           (c)     ab – 3abc – 2ac 

Sol.    (a)     3a – b + 2c = 3 x 2 – 3 + 2 x 4 = 6 – 3 + 8 = 11 
          (b)     a2 – b2 + c2  = 22 – 32 + 42 = 4 – 9 + 16 = 11 
         (c)     ab – 3abc – 2ac = 2 x 3 – 3 x 2 x 3 x 4 – 2 x 2 x 4 = 6 – 72 – 16 = – 82 

Algebric Equation

Algebric Equation     
An equation is a mathematical statement equating two quantities. An equation has an equal sign (=) between its two sides. The equation says that the value of the left hand side (LHS) is equal to the value of the right hand side (RHS). If the LHS is not equal to the RHS, we do not get an equation.
Equation is satisfied only for a definite value of the variable.
There is an equal sign between the LHS and RHS. Neither of the two sides contain a variable. Both contain numbers. We may call this a numerical equation. Usually, the word equation is used only for equations with one or more variables.
Here are some examples of equations : 2x– 5 = 7, –3x + 2 = 5, y + 3 = 0

Solution of an equation
The value of the variable (unknown) in an equation which satisfies the equation is called a solution to the equation.
The values is called the root(s) of the equation or solution of the equation.
Let us take an example of an equation x + 3 = 10.
We have to find the value of x which will satisfy the above equation. And we observe that if we put 
x = 7 in this equation it will satisfy the equation. So x = 7 is the solution or root of this equation.

Solution of an equation by trial an error
One of the simplest ways of solving an equation is by the trial-and-error method. In this, a guess is made about the value of x, and this value is then substituted in the equation to check if it is the root of the equation. Consider the following example : 4x + 3 = 23
Our equation is 4x + 3 = 23. So we substitute different values for x and try to find out which value of x will satisfy the equation. Make a chart as follow.

Ex.1    Determine if 3 is the root of the equation 5x – 10 = 5.
Sol.     If we put x = 3, then L.H.S. = 5x – 10 = 5 × 3 – 10 = 15 – 10 = 5
        R.H.S. = 5 
             L.H.S. = R.H.S
        Thus, 3 is a root of the given equation.

Ex.2  If 20 is subtracted from a number, the result is 45. Convert this statement into an algebraic equation.
Sol.    Let us suppose that x is the unknown number.
Then x – 20 stands for 20 subtracted from the number x. This is equal to 45.
Hence, x – 20 = 45 
Once you convert a statement into an algebraic equation, it is easier to solve and find the root.

Ex.3     Solve the equation x – 7 = – 2 and check the result.
Sol.    We have, x – 7 = – 2.
In order to solve this equation, we have to get x by itself on the L.H.S., We need to shift – 7. This can be done by adding 7 to both sides of the given equation. Thus,
        x – 7 = – 2
           x – 7 + 7 = – 2 + 7           [Adding 7 to both sides]
           x + 0 = 5             [ – 7 + 7 = 0 and – 2 + 7 = 5]
          x = 5
        Thus, x = 5 is the solution of the given equation.
        L.H.S. = 5 – 7 = – 2 and R.H.S. = – 2
        Thus, when x = 5, we have L.H.S. = R.H.S.

    Ex.4      Solve : 3(x + 3) – 2 (x – 1) = 5 (x – 5).
    Sol.       We have,
     3(x + 3) – 2(x – 1) = 5(x – 5)
        3x + 9 – 2x + 2 = 5x – 25    [Expanding brackets on both side]
       3x – 2x + 9 + 2 = 5x – 25
      x + 11 = 5x – 25        [Taking 5x to the L.H.S. and 11 to the R.H.S.]
      – 4x = – 36
       x = 9

Related Chapter Name