Thursday, 27 September 2012

Arrays In PHP Tutorials And Examples

PHP Arrays
An array is capable of storing multiple values in one single variable.

Definition of an Array?
A variable is a storage area holding a number or text. The problem is, a variable will hold only one value. An array is a special variable, which can store multiple values in one single variable. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

Example:
$color1="red";
$color2="blue";
$color3="yellow";


What are Numeric Arrays
A numeric array stores each array element with a numeric index.
There are two methods to create a numeric array.

1. In the following example the index are automatically assigned (the index starts at 0):
Example:
$colors=array("red","yellow","pink","blue");

2. In the following example we assign the index manually:
Example:
$colors[0]="red";
$colors[1]="yellow";
$colors[2]="pink";
$colors[3]="blue";

Example:
In the following example you access the variable values by referring to the array name and index:

<?php
$colors[0]="red";
$colors[1]="yellow";
$colors[2]="pink";
$colors[3]="blue";

echo $colors[0] . " and " . $colors[1] . " are my favourite colors.";
?>

Output:
red and yellow are my favourite colors.


What are Associative Arrays
An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it.

With associative arrays we can use the values as keys and assign values to them.

Example:
In this example we use an array to assign ages to the different persons:

$marks = array("jack"=>32, "nancy"=>30, "cathy"=>31);

Example:
This example is the same as example 1, but shows a different way of creating the array:

$marks['jack'] = "32";
$marks['nancy'] = "30";
$marks['cathy'] = "31";
The ID keys can be used in a script:

<?php
$marks['jack'] = "32";
$marks['nancy'] = "30";
$ages['cathy'] = "34";

echo "Jack is " . $ages['jack'] . " years old.";
?>
:
Output:
Jack is 32 years old.

Saturday, 15 September 2012

PHP Switch Case Statement Syntax And Example

Switch case is an alternative of else-if ladder. If we have multiple conditions to check then we can use switch case statement.

Syntax

switch (n)
{
case label1:
  //code is executed when n = label1
  break;
case label2:
  //code is executed when n = label2
  break;
default:
  //code is executed when n is neither label1 nor label2
}

Example:

<html>
<body>
<?php
$x=1;
switch ($x)
{
case 1:
  echo "Today is Monday";
  break;
case 2:
  echo "Today is Tuesday";
  break;
case 3:
  echo "Today is Wednesday";
  break;

case 4:
  echo "Today is Thursday";
  break;
case 5:
  echo "Today is Friday";
  break;
default:
  echo "This is Weekend";
}

?>
</body>
</html>

PHP Example On Database Connectivity With Mysql

Let say you have a table named "EMPLOYEE" with fields "Name" and "City" and the give data in a database named "TEST".

Employee
Name                    City
Jack                      California

Example:
<html>
<body>
<?php
  $con = mysql_connect("localhost","root","");
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("TEST", $con);
  $result = mysql_query("SELECT * FROM employee");
  $row = mysql_fetch_array($result);

  while($row = mysql_fetch_array($result))

  {
     echo $row['Name'] . " " . $row['City'];
     echo "<br />";
  }

?>

The above example displays all the records from the table on to the webpage.

Also see how to connect with database with Mysql in PHP

How To Connect With Mysql Database In PHP

In PHP to connect with the database and manipulate tables and their rows we use the following steps:

Step1
First make a connection with the server.
mysql_connect(servername,username,password);

Example:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
  die('Unable to connect' . mysql_error());
}
?>

Step2
Now select the database you want to use
mysql_select_db("databasename", $con);

Step3
Then execute the SQL query on a table and get the result in a variable here we have taken $result.
$result = mysql_query("SELECT * FROM indexpage");

Step4
Then get the first row from the result set which is stored in $result and store it in another variable here we have taken $row.
$row = mysql_fetch_array($result);

Step5
Finally get the value of a particular fieldname from the first row which is present in the $row array and store it in any variable name.

$value = $row['fieldname'];

Complete Example on PHP database connectivity with Mysql

Wednesday, 5 September 2012

Scope Of Variables In PHP

In PHP there are four different types of scope of variables.
  • Local Scope
  • Global Scope
  • Static Scope
  • Parameter Scope
Local Scope
A variable that is declared within a particular block has a local scope in that block and is not accessible outside that blog.

Example:

<?php
//this is global scope
$x=100;
{
//this is local scope
$y=200;
echo $y;
}

echo $x;  //this is accessible

echo $y;  //this is not accessible
?>


Global Scope
A variable that is declared outside of a particular block or function. Global variables are accessed from anywhere that is not inside a function. Use the global keyword to access the global variable from a function.

Example:

<?php
$x=100;
$y=200;

function sum()
{
global $x, $y;
$y=$x + $y;
}

sum();
echo $ y;
?>

PHP Variables Declaration And Scope

What is a variable?
As we have studied in Algebra that a variable is used to hold a value which can be further used in various formulas for performing calculations.

Examples x=10, speed=100

The words "x" and "speed" are therefore called variables and can be used in various expressions.

Rules For Naming PHP Variables
  • A variable in PHP always begins with a $ sign and then the name of the variable.
  • After $ sign there must be a letter or an underscore character.
  • Spaces are not allowed in variable names.
  • In PHP variable names are case sensitive eg: SUM and sum are considered as two different variables.
  • A variable name should only contain alphanumeric characters (A-Z, a-z, 0-9) and in place of spaces underscore should be used.

Variable Declaration in PHP
Variables are created in PHP starting with a $ sign as follows
Example:
$firstname="John";

Example:
<?php
$firstname="John";
$lastname="Travolta";
$city="California";
echo $firstname;
echo $lastname;
echo $city;
?>

PHP is very Loosely Packed Language
In PHP a variable does not need to be declared before assigning a value to it. PHP automatically converts the variable to the required data type according to the value assigned. Whereas in other languages like C, C++ you have to declare a variable by specifying the type of data it will hold and then the value is assigned to that variable.

Scope Of Variables
Scope defines the accessibility or availability of a variable at different places in a program.
Scope of variables read more...