Monday, May 9, 2016

WDP - Arrays

PHP Arrays

An array is a special variable, which can hold more than one value at a time.
An array can hold many values under a single name, and you can access the values by referring to an index number.

In PHP, the array() function is used to create an array (syntax):
array();

-------------------------------------------------------------------------------------
array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
<?php
$color1 = "Red";
$color2 = "Green";
$color3 = "Blue";

echo $color1;
echo "<br>";

echo $color2;
echo "<br>";

echo $color3;
?>

---------------------------------------------------------------------------------
Output:

Red
Green
Blue
In PHP, there are three types of arrays:
  • Indexed arrays - Arrays with a numeric index
  • Associative arrays - Arrays with named keys
  • Multidimensional arrays - Arrays containing one or more arrays

The index can be assigned automatically (index always starts at 0), like this:

<?php

$colors = array("Red", "Green", "Blue");

// Printing array structure
print_r($colors);
?>

This is equivalent to the following example, in which indexes are assigned manually:

<?php
$colors[0] = "Red"; 
$colors[1] = "Green"; 
$colors[2] = "Blue";

// Printing array structure
print_r($colors); 

//Printing one by one
echo "Value is $colors[1] <br/>";

echo "Value is $colors[2] <br/>";
?>
Output: Array ( [0] => Red [1] => Green [2] => Blue )
Value is Green 
Value is Blue 


Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array: 
<?php
$ages = array("Peter"=>22, "Clark"=>32, "John"=>28);

// Printing array structure
print_r($ages); 
?>

The following example is equivalent to the previous example, but shows a different way of creating associative arrays:


<?php

$ages["Peter"] = "22";
$ages["Clark"] = "32";
$ages["John"] = "28";

// Printing array structure
print_r($ages); 

echo "<br/>";
echo "Age of Peter is ". $ages["Peter"];
?>
OutputArray ( [Peter] => 22 [Clark] => 32 [John] => 28 )
Array ( [Peter] => 22 [Clark] => 32 [John] => 28 ) 
Age of Peter is 22


A multidimensional array is an array containing one or more arrays.
PHP understands multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. An example of a multidimensional array will look something like this:

<?php
// Define nested array
$contacts = array(
    array(
        "name" => "Peter Parker",
        "email" => "peterparker@mail.com",
    ),
    array(
        "name" => "Clark Kent",
        "email" => "clarkkent@mail.com",
    ),
    array(
        "name" => "Harry Potter",
        "email" => "harrypotter@mail.com",
    )
);
// Access nested value
echo "Peter Parker's Email-id is: " . $contacts[0]["email"];
?>
Output: Peter Parker's Email-id is: peterparker@mail.com



PHP File Upload

PHP File Upload


A PHP script can be used with a HTML form to allow users to upload files to the server. Initially files are uploaded into a temporary directory and then relocated to a target destination by a PHP script.

First, ensure that PHP is configured to allow file uploads.
In your "php.ini" file, search for the file_uploads directive, and set it to On:

file_uploads = On

Next, create an HTML form that allow users to choose the image file they want to upload:
<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>




PHP Include

PHP Include

The include statement includes and evaluates the specified file.

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Syntax

include 'filename';

or

require 'filename';

Example :

File my_include.php :
  1. <?php  
  2. $sports1 = "football";  
  3. $sports2 = "cricket";  
  4. ?>  
File myfile.php, which includes my_include.php :
  1. <?php  
  2. include('my_include.php');  
  3. echo "I prefer&nbsp;".  $sports1 ."&nbsp;  
  4. than&nbsp;".  $sports2;  
  5. ?>  

Output

I prefer football  than cricket

WDP - PHP Date & Time

PHP Date and Time

The date() function formats a local date and time, and returns the formatted date string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().

date(format,timestamp);

string date ( string $format [, int $timestamp = time() ] )
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
  • l (lowercase 'L') - Represents the day of the week
Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting.
<?php// set the default timezone to use. Available since PHP 5.1date_default_timezone_set('UTC'. "<br>";

// Prints something like: Mondayecho date("l"
. "<br>";
// Prints something like: Monday 8th of August 2005 03:12:46 PMecho date('l jS \of F Y h:i:s A'
. "<br>";

// Prints: July 1, 2000 is on a Saturdayecho "July 1, 2000 is on a " date("l"mktime(000712000)) 
. "<br>";
/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822
. "<br>";
// prints something like: 2000-07-01T00:00:00+00:00echo date(DATE_ATOMmktime(000712000)) 
. "<br>";
?>
Here are some characters that are commonly used for times:
  • h - 12-hour format of an hour with leading zeros (01 to 12)
  • i - Minutes with leading zeros (00 to 59)
  • s - Seconds with leading zeros (00 to 59)
  • a - Lowercase Ante meridiem and Post meridiem (am or pm)
The example below outputs the current time in the specified format:

Example

<?php
echo "The time is " . date("h:i:sa");
?>
Output: The time is 09:54:12pm
Other formats...