programming

PHP – Global Variables – Superglobals

PHP – Global Variables – Superglobals

Superglobals are variables that are always available in all scopes.
Superglobals always accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special.

The PHP superglobal variables are:

$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

$GLOBAL

$GLOBAL is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods).

In the next example, since z is a variable present within the $GLOBALS array, it is also accessible form outside the function!

<?php 

$x = 75;
$y = 25; 

function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

addition();
echo $z; // Output: 100

?>
By |PHP, Web Design|Commenti disabilitati su PHP – Global Variables – Superglobals

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 – 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

Luce Digitale/JQ Sunny Panorama – Plugin

Luce Digitale/JQ Sunny Carousel – Plugin

By |JQuery|Commenti disabilitati su Luce Digitale/JQ Sunny Panorama – Plugin