I PHP Unit2 Degree
I PHP Unit2 Degree
UNIT II
TOPIC 1:
Arrays
arrays are special types of variables that enable you to store as many values. Arrays are indexed,
which means that each entry is made up of a key and a value.
• The key is the index position, beginning with 0 and increasing incrementally by 1 with each new
element in the array.
• The value is whatever value you associate with that position—a string, an integer, or whatever you
want.
Creating Arrays
You can create an array using either the array() function or the array operator []. The array() function
is usually used when you want to create a new array with more than one element. The array operator is
often used when you want to create a new array with just one element at the outset, or when you want to
add to an existing array element. The following code shows how to create an array
$rainbow = array(“red”, “orange”, “yellow”, “green”, “blue”, “indigo”, “violet”);
The following shows the same array being created incrementally using the array operator ([]):
$rainbow[] = “red”;
$rainbow[] = “orange”;
$rainbow[] = “yellow”;
$rainbow[] = “green”;
$rainbow[] = “blue”;
$rainbow[] = “indigo”;
$rainbow[] = “violet”;
Both examples create a seven-element array called $rainbow, with values starting at index position 0 and
ending at index position 6.
$rainbow[0] = “red”;
$rainbow[1] = “orange”;
$rainbow[2] = “yellow”;
$rainbow[3] = “green”;
$rainbow[4] = “blue”;
$rainbow[5] = “indigo”;
$rainbow[6] = “violet”;
you initially create your array using the array() function or the array operator, you can still add to it
using the array operator. In the first line of the following example, six elements are added to the array, and
one more element is added to the end of the array in the second line:
EX: $rainbow = array(“red”, “orange”, “yellow”, “green”, “blue”, “indigo”);
$rainbow[] = “violet”;
TOPIC 2:
Types of Arrays:
There are three different kind of arrays and each array value is accessed using array index.
• Numeric array
• Associative array
• Multidimensional array
Numeric array
An array with a numeric index. Values are stored and accessed in linear fashion.
You can create an numeric array using either the array() function or the array operator []. The array() function
is usually used when you want to create a new array with more than one element. The array operator is
often used when you want to create a new array with just one element at the outset, or when you want to
add to an existing array element. The following code shows how to create an array
III BCom- SEMVI UNIT II Page 1
Srinivasa Degree College PHP and My SQL
$rainbow = array(“red”, “orange”, “yellow”, “green”, “blue”, “indigo”, “violet”);
The following shows the same array being created incrementally using the array operator ([]):
$rainbow[] = “red”;
$rainbow[] = “orange”;
$rainbow[] = “yellow”;
$rainbow[] = “green”;
$rainbow[] = “blue”;
$rainbow[] = “indigo”;
$rainbow[] = “violet”;
Both examples create a seven-element array called $rainbow, with values starting at index position 0 and
ending at index position 6.
$rainbow[0] = “red”;
$rainbow[1] = “orange”;
$rainbow[2] = “yellow”;
$rainbow[3] = “green”;
$rainbow[4] = “blue”;
$rainbow[5] = “indigo”;
$rainbow[6] = “violet”;
you initially create your array using the array() function or the array operator, you can still add to it
using the array operator. In the first line of the following example, six elements are added to the array, and
one more element is added to the end of the array in the second line:
EX: $rainbow = array(“red”, “orange”, “yellow”, “green”, “blue”, “indigo”);
$rainbow[] = “violet”;
Associative array:
An array with strings as index. This stores element values in association with key values rather than
in a strict linear index order.
To store the salaries of employees in an array, a numerically indexed array would not be the best
choice. Instead, we could use the employees names as the keys in our associative array, and the value would
be their respective salary.
The following example demonstrates this by creating an array called $character with four elements:
$character = array(“name” => “Bob”,
“occupation” => “superhero”,
“age” => 30,
“special power” => “x-ray vision”);
The four keys in the $character array are name, occupation, age, and special power. The associated
values are Bob, superhero, 30, and x-ray vision, respectively. You can reference specific elements of an
associative array using the specific key, such as in this example:
echo $character[‘occupation’];
The output:
superhero
As with numerically indexed arrays, you can use the array operator to add to an associative array:
$character[‘supername’] = “Mega X-Ray Guy”;
This example adds a key called supername with a value of Mega X-Ray Guy. The only difference
between an associative array and a numerically indexed array is the key name. In a numerically indexed
array, the key name is a number. In an associative array, the key name is a meaningful word.
Multidimensional array -
An array containing one or more arrays and values are accessed using multiple indices. A multi-
dimensional array each element in the main array can also be an array. And each element in the sub-array
can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
Specifying Precision
If you want to output data in floating-point format, you can specify the precision to which you want
to round your data. This proves particularly useful when dealing with currency. The precision identifier
should be placed directly before the type specifier. It consists of a dot followed by the number of decimal
places to which you want to round. This specifier has an effect only on data that is output with the f type
specifier:
<?php
printf(“%.2f”, 5.333333); // prints “5.33”
?>
Conversion Specifications:
Listing 10.4 uses getdate() in line 2 to extract information from a timestamp, employing a foreach
statement to print each element (line 3). You can see typical output of this script, called getdate.php, in
Figure 10.5. LISTING 10.4 Acquiring Date Information with getdate()
1: <?php
III BCom- SEMVI UNIT II Page 10
Srinivasa Degree College PHP and My SQL
2: $d= getdate(); // no argument passed so today’s date will be used
3: echo "today date is: $d['mon']"/" $d['mday'] "/" $d['year']";
4: ?>
It returns 03/02/2018
Converting a Timestamp with date()
Some times,you want to display the date as a string. The date() function returns a formatted string
that represents a date.
syntax: date(format,timestamp)
Some Format Codes for Use with date()
EX:
1: <?php
2: $time = time(); //stores the exact timestamp to use in this script
3: echo date(“m/d/y”, $time);
4: ?>
output:
01/17/12
Creating Timestamps with mktime()
mktime() returns a timestamp that you can then use with date() or getdate(). mktime() accepts up to
six integer arguments in the following order:
• Hour
• Minute
• Second
• Month
III BCom- SEMVI UNIT II Page 11
Srinivasa Degree College PHP and My SQL
• Day of month
• Year
EX:
1: <?php
2: // make a timestamp for Jan 17 2012 at 9:34 pm
3: $ts = mktime(21, 34, 0, 1, 17, 2012);
4: echo date(“m/d/y”, $ts);
5: ?>
Output:
01/17/12
Date/Time Functions
Function Description
date_add() Adds days, months, years, hours, minutes, and seconds to a date
Returns a new DateTime object formatted according to a
date_create_from_format()
specified format
date_create() Returns a new DateTime object
date_date_set() Sets a new date
date_default_timezone_get() Returns the default timezone used by all date/time functions
date_default_timezone_set() Sets the default timezone used by all date/time functions
date_diff() Returns the difference between two dates
date_format() Returns a date formatted according to a specified format
date_get_last_errors() Returns the warnings/errors found in a date string
date_interval_create_from_date_string() Sets up a DateInterval from the relative parts of the string
date_interval_format() Formats the interval
date_isodate_set() Sets the ISO date
date_modify() Modifies the timestamp
date_offset_get() Returns the timezone offset
Returns an associative array with detailed info about a specified
date_parse_from_format()
date, according to a specified format
Returns an associative array with detailed info about a specified
date_parse()
date
Subtracts days, months, years, hours, minutes, and seconds from
date_sub()
a date
Returns an array containing info about sunset/sunrise and
date_sun_info()
twilight begin/end, for a specified day and location
date_sunrise() Returns the sunrise time for a specified day and location
date_sunset() Returns the sunset time for a specified day and location
date_time_set() Sets the time
date_timestamp_get() Returns the Unix timestamp
date_timestamp_set() Sets the date and time based on a Unix timestamp
date_timezone_get() Returns the time zone of the given DateTime object
date_timezone_set() Sets the time zone for the DateTime object
date() Formats a local date and time