Web Design

PHP Arrays

PHP Arrays

An array it is a single variables that stores multiple values.

Two dimensional Array

<?php

// ---------------
// Basic Structure
// ---------------
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
//Output: I like Volvo, BMW and Toyota.

// ---------------
// Associative array
// ---------------

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
// Output: Peter is 35 years old

// ---------------
// Count - Get the lenght of an array
// ---------------
$cars=array("Volvo","BMW","Toyota");
echo count($cars); // Output: 3

// ---------------
// Loop through and indexed array
// ---------------
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);

for($x=0;$x<$arrlength;$x++)
  {
  echo $cars[$x];
  echo "<br>";
  }
// Output: Volvo BMW Toyota

// ---------------
// Loop through an Associative array
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

foreach($age as $x=>$x_value)
  {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
  }
// Output: Key=Peter, Value=35 Key=Ben, Value=37 Key=Joe, Value=43

?>

Sorting Arrays (Ordinamento degli Array)

<?php

// ---------------
// sort() - sort arrays in ascending order
// ---------------
$cars=array("Volvo","BMW","Toyota");
sort($cars);

$clength=count($cars);
for($x=0;$x<$clength;$x++)
   {
   echo $cars[$x];
   echo "<br>";
   } 
// Output: BMW Toyota Volvo

// ---------------
// rsort() - sort arrays in descending order
// ---------------
$cars=array("Volvo","BMW","Toyota");
rsort($cars);

$clength=count($cars);
for($x=0;$x<$clength;$x++)
   {
   echo $cars[$x];
   echo "<br>";
   }
// Output: Volvo Totota BMW

// ---------------
// asort() - sort associative arrays in ascending order, according to the value (NOT THE KEY - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
// Output: Peter Ben Joe

// ---------------
// arsort() - sort associative arrays in descending order, according to the value (NOT THE KEY - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
Output: Joe Ben Peter

// ---------------
// ksort() - sort associative arrays in ascending order, according to the key (NOT VALUE!!! - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
// Output: Ben Joe Peter

// ---------------
// krsort() - sort associative arrays in descending order, according to the key (NOT VALUE!!! - KEY IS PETER VALUE IN 35)
// ---------------
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);

foreach($age as $x=>$x_value)
    {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
    }
Output: Peter Joe Ben

?>

Multidimensional Array

A multidimensional array is an array containing one or more arrays.

<?php
$families = array   // Multidimensional Array
  (
  "Griffin"=>array  // First Array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array // Second Array
  (
  "Glenn"
  ),
  "Brown"=>array    // Third Array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
  );
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
// Statement: multidimensional array name['first array name'][first array index]
// Output: Is Megan a part of the Griffin family?
?>
By |PHP, Web Design|Commenti disabilitati su PHP Arrays

PHP – Classes – Objects

PHP – Classes – Objects

La classe è un contenitore per variabili e funzioni, ad esempio ‘class Car’ contiene variabili e funzioni, è un buon modo per raggruppare cose che funzionano insieme.

An object is a data type which stores data and information on how to process that data.

In PHP, an object must be explicitly declared.

First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods.

We then define the data type in the object class, and then we use the data type in instances of that class.

<?php
class Car // first you HAVE TO declare the object
{
  var $color;
  function Car($color="green") 
  {
    $this->color = $color;
  }
  function what_color() 
  {
    return $this->color;
  }
}
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Classes – Objects

PHP – Functions

PHP – Functions

Remember that function names are case-insensitive

Basic Structure

<?php
function writeMsg()
{
echo "Hello World!";
}

writeMsg(); // call the function and output: Hello World!
?>

Arguments

Variables can be passed to functions through arguments.

<?php

// The argument is: $fname 
function familyName($fname)
{
echo "$fname Tonin<br>";
}

familyName("Andrea");    // Output: Andrea Tonin
familyName("Erica");     // Output: Erica Tonin
familyName("Antonio");   // Output: Antonio Tonin
familyName("Maria");     // Output: Maria Tonin
familyName("Alice");     // Output: Alice Tonin

?>
<?php
function familyName($fname,$year)
{
echo "$fname Tonin. Born in $year <br>";
}

familyName("Andrea","1974");    // Output: Andrea Tonin. Born in 1974 
familyName("Erica","1975");     // Output: Erica Tonin. Born in 1975 
familyName("Serafina","1954");  // Output: Serafina Tonin. Born in 1954 
?>
<?php
function setHeight($minheight=50)
{
echo "The height is : $minheight <br>";
}

setHeight(350); // Output: The height is : 350 
setHeight();    // Output: The height is : 50   -> default value
setHeight(135); // Output: The height is : 135
setHeight(80);  // Output: The height is : 80 
?>
<?php
function sum($x,$y)
{
$z=$x+$y;
return $z;
}

echo "5 + 10 = " . sum(5,10) . "<br>"; // Output: 5 + 10 = 15
echo "7 + 13 = " . sum(7,13) . "<br>"; // Output: 7 + 13 = 20
echo "2 + 4 = " . sum(2,4);            // Output: 2 + 4 = 6
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Functions

PHP – Loops – while – do…while – for – foreach

PHP – Loops – while – do…while – for – foreach

while

<?php 
$x=1; 
while($x<=5) // It executes the code as long as the specified condition is true.
  {
  echo "The number is: $x <br>";
  $x++;
  } 
?>

do…while

<?php 
$x=1; 
do
  {
  echo "The number is: $x <br>";
  $x++;
  }
while ($x<=5)  // It executes the code as long as the specified condition is true.
?>

NOTICE:
while -> 1. condition verification 2. code execution
do…while -> 1. code execution 2. condition verification

for

<?php 
for ($x=0; $x<=10; $x++)
  {
  echo "The number is: $x <br>";
  } 
?>

foreach

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

<?php 
$colors = array("red","green","blue","yellow"); 
foreach ($colors as $value)
  {
  echo "$value <br>";
  }
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Loops – while – do…while – for – foreach

PHP – Switch

PHP – Switch

<?php
$favcolor="red";
switch ($favcolor)
{
case "red":
  echo "Your favorite color is red!";
  break;
case "blue":
  echo "Your favorite color is blue!";
  break;
case "green":
  echo "Your favorite color is green!";
  break;
default:
  echo "Your favorite color is neither red, blue, or green!";
}
?>
By |PHP, Web Design|Commenti disabilitati su PHP – Switch