Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Wednesday, 26 March 2014

Arrays in PHP | Chapter 3

 - Indexed arrays - Arrays with numeric index or numeric keys.

Understanding Numeric Index Number -  Every value in the array is assigned with an index number. Below image shows how an index number is assigned to a set of values. The first value is assigned with a index value of 0 as the index number always starts with Zero.


php indexed arrays












Example -

<?php

$numbers = array("one","two","three");

for($i=0; $i<sizeof($numbers); $i++) {

echo $numbers[$i]."<br>";

}

?>

Output:

one
two
three




  - Associative arrays - Arrays with string index values or keys. Here we can manually define the value as keys instead of 0's & 1's which we saw in Indexed arrays.The figure below includes the age of few candidates in an array, going by the indexed array will not be the best choice, instead we can use their names as the key. Accordingly we can associate the key to their ages.


php associative arrays











Thursday, 26 December 2013

PHP Variable & Data Types | Chapter 2 Cont.



Boolean - It is a data type containing two values which is TRUE (1) & FALSE (0) . They are mainly used to compare conditions for use in conditional statements

php boolean













 General Syntax -
$variable=True;
$variable=False;



 Integer - It is a data type containing numbers. Integers can be defined in three formats which is Decimal, hexadecimal & octal. An integer can be both positive or negative.

General Syntax -
$variable = decimal number;
$variable = negative number ;
$variable = octal number ;
$variable = hexadecimal number;




Example -
<?php
$variable = 1234; // decimal number (base 10)
$variable = -123; // a negative number
$variable = 0123; // octal number (equivalent to 83 decimal) (base 8)
$variable = 0x1A; // hexadecimal number (equivalent to 26 decimal) (base 16)
?>




Float - A number containing a decimal point is known as Float Value. They can be positive or negative.

General Syntax -
$variable = 10.365;