PHP, Variables and Constants.

A program contains two basic things, commands and values. The commands all have certain tasks they perform. The values are the information that the tasks are performed with.
The following program uses the variable $strText, instead of a literal:

/* example.php – “Hello, (something)!” variable demonstration */
// $strText is set to the value specified in the URL
$strText = $HTTP_GET_VARS[‘strText’];
echo ““Hello, $strText!”;
?>

For now, don’t be too distracted by the line that says:

$strText = $HTTP_GET_VARS[‘strText’];

This statement takes whatever value is specified in the URL and stores it in $strText; since it’s more related to program input, we won’t discuss it further. It is important to note that although variables and constants serve the same purpose, they aren’t at all identical.Whereas, the value of a variable might change any number of times throughout the execution of a program, constants are values that will never change (and in fact can’t change) during the execution of the program.

Declaration is the term used to describe the creation of a variable or constant. Assignment, which is covered in the following sections, is the process of storing a value in a variable. Declaring a variable is quite simple—all you have to do is assign something to it. If, for example, you wish to assign the number five to a variable named $intFive, you would use the following:

$intFive = 5;

The dollar sign in the variable name isn’t part of the variable name, Instead, it’s how PHP knows you’re referring to a variable named intFive. Whenever you use a variable, you must precede it with a dollar sign.
Variable names in PHP must follow the following requirements:
• Any combination of letters, numbers, and underscores can be used.
• Names can be as short as one character and can be of any length.
• Names can begin only with a letter or an underscore; variable names cannot begin with a number.

There are two ways a variable can be assigned, in the script itself, or by the PHP interpreter. For now, let’s focus on variables that are created (declared and assigned) by the script itself.
Assignment, as you’ve already learned, occurs when a value is stored in a variable. Whenever you assign a value to a variable, you must keep in mind the order in which the assignment will be processed. For example, the following two statements are not equivalent:

$intFive = 5;
5 = $intFive;

As a rule of thumb, read assignments from right to left—the first would read, “the number 5 should be assigned to the variable $intFive.” Reading assignments in this fashion will become more and more important as your programs and statements grow in complexity. Now, take a look at the second assignment—“the variable $intFive should be assigned to the number 5.” The latter doesn’t make sense; it will definitely not work.

Declaring a constant is done using the define function. The following example demonstrates the declaration of a constant, EXAMPLE, with a valueof 5:

/* ch02ex02.php – demonstrates constant declaration */
define(‘EXAMPLE’, 5);
echo EXAMPLE;
?>

Notice that EXAMPLE is not preceded by a dollar sign in either statement.
The dollar sign is reserved for use only with variables, constants should never be preceded by a dollar sign. Just as variables have naming requirements that must be followed, so do constants. The following guidelines will ensure that you always use a valid name for a constant:
• Aconstant’s name should not be preceded by a dollar sign.
• The name should begin with a letter or underscore, but never anumber.

Constants cannot be redefined; that is, the value of EXAMPLE cannot be changed after it has already been defined. After all, that’s why it’s defined as a constant instead of a variable. As with variables, constants should also be subject to naming conventions for clarity and good style. If you find wondering whether to use a variable or a constant, think about how the information will be needed within the program.

Variable types, which are handled automatically by PHP, tell PHP what kind of value it’s working with.PHP recognizes a value as belonging to a certain data type depending on the characteristics of the value. The following describe certain data type,

An integer is any numeric value that does not have a decimal point, such as
the following:
• 5 (five)
• –5 (negative five)
• 0123 (preceded by a zero; octal representation of decimal number 83)
• 0x12 (preceded by 0x; hexadecimal representation of decimal number18)
The last two values on the list are somewhat advanced topics and are rare.

A floating-point number (commonly referred to simply as a float or double, which, in PHP, are exactly the same thing) is a numeric value with a decimal point.
The following are all floating-point values:
• 5.5
• .055e2 (scientific notation for .055 times 10 to the second power, which is 5.5)
• 19.99
The second example is a float represented in scientific notation. The e, as is
often the case on graphing calculators, means “times 10 to the”. It is followed
by whatever power 10 should be raised to in order to put the decimal wherever
you want it. Thus, .055e2 is the same as .055 times 100, which is 5.5.

Arrays are a little different than the numeric data types discussed so far. Arrays can be thought of as lists of variables, all contained within one variable. These variables can contain values of any data type, including being arrays themselves. For example, if five people are involved in a task, their names could be stored in a five-element array, with one element for each person. The people on the task, represented collectively by the array, would each have their information stored in separate variables within the array.

Strings are values that contain text—anything from one character to a whole string of characters. For example, a sentence such as “This is a string” is a string value.

Note:A literal is a value that you give explicitly within a program. For example, 5, 5.5, and “World!” are all literals.
Although a variable type prefix is very useful in variables, it isn’t necessary in constants.
The purpose and type of a constant never change (and can’t change), so it’s
safe to assume that a variable is whatever type it is defined to be when it’s first
defined.

Embedding JavaScript in HTML

The <script> is to assist the browser in recognizing lines of code in an HTML document as belonging to a script, you surround lines of script code with a
<script>...</script> tag set. tag. Script with src,
<script src="my-script.js" type="text/javascript"></script>
The purpose is to define functions, objects, and variables. Functions will later be triggered by buttons, other user events, inline script tags with body content, etc. script with body content

<script type="text/javascript">JavaScript code</script>

Purpose is to define functions, objects, and variables. To directly invoke code that will run as page loads, to output HTML content built by JavaScript Don't use this approach for defining functions or for doing things that could be done in external files. Slower (no browser caching) and less reusable.

Learn By Example-1:
//Example (phish.js)
function getMessage() {
var amount = Math.round(Math.random() * 100000);
var message =
"You won $" + amount + "!\n" +
"To collect your winnings, send your credit card\n" +
"and bank details to oil-minister@phisher.com.";
return(message);
}
function showWinnings1() {
alert(getMessage());//"alert" pops up dialog box
}
function showWinnings2() {
document.write("<h1><blink>" + getMessage() +
"</blink></h1>");
//"document.write" inserts text into page at current location.

//Example (loading-scripts.html)
<!DOCTYPE ...><html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Loading Scripts</title>
...
<script src="./scripts/phish.js"//Loads script from previous page
type="text/javascript"></script>
</head>
<body>
...
<input type="button" value="How Much Did You Win?"
onclick='showWinnings1()'/>
//Calls showWinnings1 when user presses
//button. Puts result in dialog box.
...
<script type="text/javascript">showWinnings2()</script>
//Calls showWinnings2 when page is loaded in
//browser. Puts result at this location in page.
...
</body></html>

Java and Class Fundamentals

The General Form of a Class, When you define a class, you declare its exact form and nature.You do this by specifying the data that it contains and the code that operates on that data. A class’ code defines the interface to its data. A class is declared by use of the class keyword. The general form of a class definition is shown here:

class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list)
{
// body of method
}
type methodname2(parameter-list)
{
// body of method
}
// ...
type methodnameN(parameter-list)
{
// body of method
}
}

The data, or variables, defined within a class are called instance variables.The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class. In most classes, the instance variables are acted upon and accessed by the methods defined for that class. Thus, it is the methods that determine how a class’ data can be used. Variables defined within a class are called instance variables because each instance
of the class (that is, each object of the class) contains its own copy of these variables.
Thus, the data for one object is separate and unique from the data for another.
All methods have the same general form as main( ),however, most methods will not be specified as static or public. Notice that the general form of a class does not specify a main( ) method. Java classes do not need to have a main( ) method. You only specify one if that class is the starting point for your program. Further, applets don’t require a main( ) method at all.

Learn By Examples: Here is a class called Box that defines three instance variables: width, height, and depth. Currently, Box does not contain any methods.

class Box {
double width;
double height;
double depth;
}

A class defines a new type of data. The new data type is called Box. You will use this name to declare objects of type Box. It is important to remember that a class declaration only creates a template; it does not create an actual object. Thus, the preceding code does not cause any objects of type Box to come into existence.
To create a Box object, you will use a statement like the following:

Box mybox = new Box(); // create a Box object called mybox.

To assign the width variable of mybox the value 100, you would use the following statement:

mybox.width = 100;

Example-1: Here is a complete program that uses the Box class,

/* A program that uses the Box class.
Call this file BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}

You should call the file that contains this program BoxDemo.java, because the main( ) method is in the class called BoxDemo, not the class called Box. When you compile this program, you will find that two .class files have been created, one for Box and one for BoxDemo. The Java compiler automatically puts each class into its own .class file. It is not necessary for both the Box and the BoxDemo class to actually be in the same source file. You could put each class in its own file, called Box.java and BoxDemo.java, respectively. To run this program, you must execute BoxDemo.class. When you do, you will see the following output: Volume is 3000.0

Example-2: The followingprogram declares two Box objects:

// This program declares two Box objects.
class Box {
double width;
double height;
double depth;
}
class BoxDemo2 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to mybox2's
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
// compute volume of first box
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + vol);
// compute volume of second box
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
}
}

The output produced by this program is shown here:
Volume is 3000.0
Volume is 162.0
Download The Source Code From Here