PHP consist of two compound types, arrays and objects.

Arrays

Arrays are special types of variables that can store more than one value. Arrays are useful when you need to store related variables data under one variable name.

An array can be declare in fallowing way:

$arr-name [key]= value; 


In the above statement
arr_name is the name of the array.
Key is any string or a non negative integer. if you want to modify an array, it is best not to specify a key and to leave the square bracket empty.
Value is the data that is held in the array.

To understand the concept arrays, consider the fallowing codes:

<?php
$arr_name[0]="My";
$arr_name[1]="name";
$arr_name[2]="is";
$arr_name[3]="Rishi";
echo "$arr_name[0] $arr_name[1] $arr_name[2] $arr_name[3] $arr_name[4]"; ?>

Output of the above code is: My name is Rishi

Objects

Objects are are the building blocks of a programming language . An object is an entity that can hold data and specify what need to be done with the data. Most of the internal working of an object are hidden from the code that uses them. Objects are a bundle of variable and functions that are derived from a class. To understand the concept of object you should be familiar with the fallowing terminology

Object orientation

The programming concept that revolves around objects.An object is an instance of a class.

Class.

A class is an entity that exibit certain behavior and expose certain attributes to retain state.

Properties

The character of an object defined by the class to which it belongs.

Methods

Task that can be performed by object on the data they contain.

Juggling Data Types

PHP does not require the developer to absolutely specify a data type for a variable at the time of declaration. In PHP you need not worry about data type mismatches in an expression. Consider the fallowing example:

<?php
 $myVal = 7+ "2 is my number";
 echo $myVal;
?> 

The output will be: 9.

In most other language ,the preceding code will not be valid. In PHP this statement is valid, and it also generates the expected output. This is because in PHP a variable doesn't have fixed data type assign to it and can store any data type. The result of an expression is calculated on the basic of the operator and not on the basic of the operands. Examine the fallowing code for a better understanding of juggling data type:

<?php
$a = "Rishi";
$a +=2; // The += operator is use to add the initial value assigned to // the variable to the number specified.r> echo?>


The output will be : 2

In the above example ,$a contains a value that is of the string data type. In the next line, the += operator changes the initial variable assigned to $a to 2.

Consider another example where the .= operator is used to concatenate string:

<?php
$a = "Rishi";r> $a .=2; //The .= operator is use to concatenate two string .
echo $a; //The value of $a is now Rishi2
?>

The output will be: Rishi2

The . = operator tells the PHP that the number 2 is a string even though it is not specified in single quotes or double quotes. As a result it concatenates the initial value with the new value of $a, and the output is Rishi2.

Constants

A constant is a name that identifies a simple value. By nature, a constant is the opposite of a variable because the value of a constant remains fixed and doesn't change when the script is executed. Constant have the fallowing characteristics:
 - A constant is case sensitive by default.
 - By convention, constant are expressed in uppercase letters.
 - A consent can begin with either an underscore or with a letter, fallowed by any number of underscore, letters or numbers.

There are several other differences between variable and constants:

 -

A constant name never begins with a dollar sign .
 - After a constant is set, it is not possible to to redefine or undefined it.
 - A constant can contain only scalar data type like : Boolean, integer, float and string.
 - A constant can be accessed and use anywhere in a script regardless of variable scoping rules.

Defining constants

A simple way to define a constant by using the define ( ) function. After you define a constant, you can read it's value anytime you want by simply using it's name. To read a constant value you can use the constant ( ) function. To obtained a list of all defined coinstants, you can use the get-defined-constants function.
In the fallowing code, a constant ADMIN_MAIL is defined with the value "contatc@a2zwebhelp.com". Notice that constants are case sensitive, and the value assigned to the constant ADMIN_MAIL doesn't appear when you try to print the value of the constant admin_mail:

<?php
define ("ADMIN_MAIL" ,"contatc@a2zwebhelp.com");
echo MIN_MAIL; ?>

The output will be: contatc@a2zwebhelp.com


Top