All posts by admin

PHP Arrays


An array stores multiple values/data/string in an single variable.

$name = array("Vijay","Gopi","Edwin","Munee");
echo "Members are ".$name[0].",".$name[1].",".$name[2].",".$name[3];


Output: Members are Vijay,Gopi,Edwin,Munee

Array Definition:

An array is an special variable which holds more than one / multiple variable at a time.

If you have a list of data, storing the names should be could look like this

$name1 = 'Vijay';
$name2 = 'Gopi';
$name3 = 'Edwin';
$name4 = 'Munee';

If you have more number of data, example more than 10 members in a team we can use array instead of creating 10 different variable.

Creation of Array

In PHP the array will create using array() function.

There are three types of arrays are there in PHP.

  • Indexed Array ( Array with numeric index )
  • Associative Array ( Array with named key )
  • Multidimensional Array ( Array containing multiple arrays with in it )

Indexed Array

An array with numeric index is called Indexed array

$a = array("IND","US","AUS");
echo $a[0]."<br />";
echo $a[1]."<br />";
echo $a[2]."<br />";


Associative Array

An array with named key is call as Associative array

$a = array("India"=>"IND","United States"=>"US","Australia"=>"AUS");
echo $a['India']."<br />";
echo $a['United States']."<br />";
echo $a['Australia']."<br />";


Multidimensional Array

An array containing multiple array with in it called as Multidimensional .

$a = array("India"=>array("KA","TN","KL"),"United States"=>array("AL","AK","AZ"));

PHP Function

Functions are used to reduce the repetition of codes in your scripts.

The user define function are starts with function tag.

The function name should not start with numbers ( 0 – 9 ). The allowed format is letters ( a-z, A-Z )  and underscores ( _ ).

Beginning with curly braces (  {  ) indicates the function get start to execute the block of code under the function. Closing curly braces ( } ) indicates the closing / end of the function.

     function cityDetails($city){
         echo "Am staying in $city";
         echo "<br />";

     cityDetails("New York");
     cityDetails("Los Angeles");

Am staying in New York
Am staying in Los Angeles
     function add($a,$b){
       $c = $a + $b;
       return $c;
     function sub($a,$b){
       $c = $a - $b;
       return $c;
     function mul($a,$b){
       $c = $a * $b;
       return $c;

    $addingValues = add(25,15);
    $subtractingValues = sub(30,10);
    $multiplicationValues = mul($addingValues,$subtractingValues);
    echo "Multiplication Value is :: $multiplicationValues";

Output :
Multiplication Value is :: 800

// addition value is 40
// subtraction value is 20
// Multiplication of both the data will be 40 * 20 = 800

For more info please refer functions section.



PHP Loops

PHP loops are used to execute a certain block of code, while the specific condition is true.

If you want run over / execute  same set  of codes in your page you can use PHP loops.

In PHP we are having 4 different types of looping statement:

  • while
  • do … while
  • for
  • foreach

For your reference below some examples are given.

The PHP while loop:

$i = 2;
while($i <= 6){
   echo $i."<br />";



The PHP do…while loop:

  echo "The current value is: $i <br>";
while ($i<=4)

The current value is: 2
The current value is: 3
The current value is: 4

The PHP for loop:

for ($i=0; $i<=10; $i++)
  echo "The current value is: $x <br>";

The PHP foreach loop:

$students = array("Jhon","Victor","Ram","Vijay"); 
foreach ($students as $value)
  echo "$value <br>";

PHP Switch statement

The switch statement is similar to if..else statement only.

In many situation, you may need to check / compare different values with the same variable. In that situation switch statement really helps.

Switch case is bit faster compare with series of if statement.

$x = "announcement";
switch ($x) {
    case "login":
        echo "You are entered in Login / Signup page";
    case "home":
        echo "You are entered in Home page";
    case "announcement":
        echo "You are entered in Announcement page";
    case "logout":
        echo "You are entered in Logout page";
        echo "You are entered in Login / Signup page";
You are entered in Announcement


  • break used to prevent the code running from current to next case level.
  • In here you can able to see a special case called default.It is used to define the default value, if no statement match is found in the block segment.

PHP if…else Statements

PHP having different types of conditional statements like C and C ++. Below we listed the conditional statements presents in PHP..

  • if statement
  • if … else statement
  • if … elseif ….. else statement
  • Nested if  statement
  • switch statement

For more information please Click Here.

PHP Constants


A constant is a name/identifier for a simple value. The constant value can not able to change during the time of script execution.

The constants are defined using define() function.

It will return either constant value of NULL.

Two types of constants are there. They are,

  • Case-sensitive constants
  • Case-insensitive constants


echo MAX_SIZE;



echo max_size;


PHP String functions

PHP String Functions PHP strings are sequences of characters, like ” Hello World “. In this chapter we will see the commonly used string functions.

str_len() Function

This function is used get the total number of characters in string .

echo str_len("Test String");


addslashes() Function

This function will return the string with backslashes in-front of the special character.

$str_data = addslashes("It's test data");

It\'s test data

Below mentioned the lists of PHP string function for your reference. The examples for these will be update soon.

  1. addcslashes
  2. addslashes
  3. bin2hex
  4. chop
  5. chr
  6. chunk_split
  7. convert_cyr_string
  8. convert_uudecode
  9. convert_uuencode
  10. count_chars
  11. crc32
  12. crypt
  13. echo
  14. explode
  15. fprintf
  16. get_html_translation_table
  17. hebrev
  18. hebrevc
  19. hex2bin
  20. html_entity_decode
  21. htmlentities
  22. htmlspecialchars_decode
  23. htmlspecialchars
  24. implode
  25. join
  26. lcfirst
  27. levenshtein
  28. localeconv
  29. ltrim
  30. md5_file
  31. md5
  32. metaphone
  33. money_format
  34. nl_langinfo
  35. nl2br
  36. number_format
  37. ord
  38. parse_str
  39. print
  40. printf
  41. quoted_printable_decode
  42. quoted_printable_encode
  43. quotemeta
  44. rtrim
  45. setlocale
  46. sha1_file
  47. sha1
  48. similar_text
  49. soundex
  50. sprintf
  51. sscanf
  52. str_getcsv
  53. str_ireplace
  54. str_pad
  55. str_repeat
  56. str_replace
  57. str_rot13
  58. str_shuffle
  59. str_split
  60. str_word_count
  61. strcasecmp
  62. strchr
  63. strcmp
  64. strcoll
  65. strcspn
  66. strip_tags
  67. stripcslashes
  68. stripos
  69. stripslashes
  70. stristr
  71. strlen
  72. strnatcasecmp
  73. strnatcmp
  74. strncasecmp
  75. strncmp
  76. strpbrk
  77. strpos
  78. strrchr
  79. strrev
  80. strripos
  81. strrpos
  82. strspn
  83. strstr
  84. strtok
  85. strtolower
  86. strtoupper
  87. strtr
  88. substr_compare
  89. substr_count
  90. substr_replace
  91. substr
  92. trim
  93. ucfirst
  94. ucwords
  95. vfprintf
  96. vprintf
  97. vsprintf
  98. wordwrap


PHP Data Types

A data type refers to, the type of data/details a variable can store.

PHP has totally eight data types. They are separated  as three major types. They are Scalar , Compound and special types. Below we listed out all the data types.

Scalar Types:

  • Boolean
  • Integer
  • String
  • Float

Compound Types:

  • Object
  • Array

Special Types:

  • NULL
  • Resources


Note: var_dump() will return data type and value of the variable.

We can specify integers in three formats.

  • Decimal
  • Hexadecimal
  • Octal
$a = 345;
$b = -234;
$c = 0x8C;
$d = 047;
echo "<br />";
echo "<br />";
echo "<br />";


 Float :

As we already know Float is a number with decimal point.

$a = "11.43";
$b = "23.5e2";
$c = "11e-3";

PHP Boolean

The data type Boolean either TRUE or FALSE

$a = true;
$b = false;
PHP Array

Array is used to store multiple data/value in a single variable.

$student_role_no = array("12789","12790","12791");
PHP Objects

In PHP an object is used to stores a data/value and information, how to process the data or we can say objects are user define the data type. We will learn more about this in Object oriented program page.


In PHP NULL is special data type.

Usually we will use this data type to check the variable data is present or empty.  It was assigned as a special constant named as “NULL”.

$a = "test";
$a = NULL;

Note:Here NULL predefined constant data. So we no need to place it under singe(') or double ("") quotes. If we use unset()function for a variable  its value will get change as NULL.

PHP Variables

A variable is just a storage element.

Variables in PHP are represented by a dollar sign ( $ ) by the name of the variable. The variable name is case-sensitive.

The variable name should be start with letters ( a-zA-Z ) or underscore ( _ ) and it should not start with numeric /number

Below we have given some examples for your reference.

$x = 'hello';
$$x = 'world';
echo "$x ${$x}";
echo "$x $hello";

hello world
hello world
class test {
    var $bar = 'I am bar.';
    var $arr = array('I am A.', 'I am B.', 'I am C.');
    var $r   = 'I am r.';

$test = new test();
$bar = 'bar';
$baz = array('foo', 'bar', 'baz', 'quux');
echo $test->$bar . "\n";
echo $test->$baz[1] . "\n";

$start = 'b';
$end   = 'ar';
echo $test->{$start . $end} . "\n";

$arr = 'arr';
echo $test->$arr[1] . "\n";
echo $test->{$arr}[1] . "\n";


I am bar.
I am bar.
I am bar.
I am r.
I am B.
$a = 10;
$b = 15;
echo $c = $a + $b;

function myTest()
global $a,$a;

echo $d;

function myTest()

echo $b;


PHP Echo / Print

In PHP there is 2 ways to print output:

  • echo
  • print

echo is bit faster compare with print.

echo can output one or more strings

print can only output one string, and it returns value as 1.


echo is a language construct, and can be used with or without parentheses: echo or echo().

$txt = "test data";
echo $txt1;
echo "$txt1";
echo '$txt1';

test data
test data

If we use double quotes in echo statement, it will check both the variable data and string.

If we use single quotes in echo statement,  it will consider all data as string only.