Friendly PHP Course – Part #2 Using Constants , Maths,Formating numbers,Strings

Deprecated: Function create_function() is deprecated in /home/vhosts/ on line 4698



Similar to variables , constants are given a name and a value is stored in them they can’t be changed .

Used when a value is needed several places in the program and doesn’t change.




echo constantname;

to see its value .When you echo a constant, you can’t enclose it in quotes .If you do, it echoes the constant name.

Constant names are not preceded by a dollar sign ($).

By convention, constants are given names that are all uppercase.

   Maths & Numbers

You indicate arithmetic operations with two numbers and an arithmetic operator , such as:
2 + 3
You can also perform arithmetic operations with variables.


$nu1 =2;

$nu2 =3;

$sum = $nu1 + $nu2;

echo "sum of two numbers = ";

echo " $sum <br />";



The output is

sum of two numbers = 5

Arithmetics Operators

  • +    –   Add and substract
  • *   /    Multiply and Divide
  • a%b   The reminder of a/b (modulus)

PHP does multiplication and division first, followed by addition and subtraction.  If other considerations are equal, PHP goes from left to right.

You can change the order by using parentheses. Its better use them..

Formating numbers


formats a number into a dollar amount.

$newvariablename = number_format($oldvariablename,2);

also add commas to separate thousands.


echo " formating number " ;

$f_price = sprintf ("%01.2f", $sum);

echo "$f_price";


The output  is

formating number = 5.00

Character strings

A string   is a series of characters. Characters are letters, numbers,and punctuation You tell PHP where the string begins and ends by using double quotes or single quotes.

$string = 'It is John's house';

echo $string;


It won’t work because when PHP sees the ‘ (single quote) it thinks that this is the end of the string.

Escaping the character

The backslash tells PHP that the single quote does not have any special meaning.

$string = 'It is Tom\'s house';

echo $string;

When you enclose a string in double quotes, you must also use a backslash in front of any double quotes in the string.

Single quoted  & double quoted strings

Single quoted strings are stored literally, with the exception of \

If you enclose a variable in single quotes, PHP uses the literal variable name.

$enclosedvar = '$age';

echo $enclosedvar;

shows $age;

The special characters \n tell PHP to start a new line.
The special characters \t tell PHP to insert a tab.

Double-quoted strings, variables and some special characters are evaluated before the string is stored.

If you enclose a variable in double quotes, PHP uses the value of the variable.

Joining strings

You can  concatenate strings  by using a dot (.)

$stringconcatenated = $string1.$string2;

use .= to add characters to an existing string


Finally read the summary in a mindmap


Download PDF Mindmap


Previous chapter


Next Chapter




You may also like...