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>
 

About Me

It's Me!Hi, I'm Moinuddin. I'm a Software Engineer at WIPRO working on Microsoft .NET Technology. I'm interested in a wide range of technology topics, mainly Microsoft including Windows Azure, WCF, WPF, Silverlight and any other cool technology that catches the eye.

Site Info

ProjectCSharp.com is mainly about demonstrating real time examples in different technologies like ASP.NET, WCF, WPF, Silverlight, Azure etc..,

Followers

Help Links

Project CSharp (C#) Copyright © 2011. All Rights Reserved.