Archive for November, 2007

Chapter 5 . (Unlimited web hosting) Syntax and Variables 73 memory

Friday, November 30th, 2007

Chapter 5 . Syntax and Variables 73 memory use and functionality were often agonizing. The PHP designers made what we think is a good decision to simplify this by having only two numerical types, corresponding to the largest of the integral and floating-point types in C. Integers Integers are the simplest type they correspond to simple whole numbers, both positive and negative. Integers can be assigned to variables, or they can be used in expressions, like so: $int_var = 12345; $another_int = -12345 + 12345; // will equal zero Read formats Integers can actually be read in three formats, which correspond to bases: decimal (base 10), octal (base 8) , and hexadecimal (base 16). Decimal format is the default, octal integers are specified with a leading 0, and hexadecimals have a leading 0x. Any of the formats can be preceded by a - sign to make the integer negative. For example: $integer_10 = 1000; $integer_8 = -01000; $integer_16 = 0×1000; print( integer_10: $integer_10
); print( integer_8: $integer_8
); print( integer_16: $integer_16
); yields the browser output: integer_10: 1000 integer_8: -512 integer_16: 4096 Note that the read format affects only how the integer is converted as it is read the value stored in $integer_8 does not remember that it was originally written in base 8. Internally, of course, these numbers are represented in binary format; we see them in their base 10 conversion in the preceding output because that is the default for printing and incorporating int variables into strings. Range How big (or small) can integers get? Because PHP integers correspond to the C long type, which in turn depends on the word-size of your machine, this is difficult to answer definitively. For most common platforms, however, the largest integer is 231 1 (or 2,147,483,647), and the smallest (most negative) integer is (231 1) (or 2,147,483,647). As far as we know, there is no PHP constant (like MAXINT in C) that will tell you the largest integer on your implementation. If you really need integers even larger or smaller than the preceding, PHP does have some arbitrary-precision functions see the BC section of the Mathematics chapter (Chapter 27). Doubles Doubles are floating-point numbers, such as: $first_double = 123.456; $second_double = 0.456 $even_double = 2.0;
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Free web servers - 72 Part I . PHP: The Basics The

Friday, November 30th, 2007

72 Part I . PHP: The Basics The substr function is designed to take a string of characters as its first input and return a substring of that string, with the start point and length determined by the next two inputs to the function. Instead of handing the function a character string, however, we gave it the integer 12345. What happens? As it turns out, there is no error, and we get the browser output: sub is 34 Because substr expects a character string rather than an integer, PHP converts the number 12345 to the character string 12345 , which substr then slices and dices. Because of this automatic type conversion, it is very difficult to persuade PHP to give a type error in fact, PHP programmers need to exercise a little care sometimes to make sure that type confusions do not lead to error-free but unintended results. Type Summary PHP has a total of eight types: integers, doubles, Booleans, strings, arrays, objects, NULL, and resources. . Integers are whole numbers, without a decimal point, like 495. . Doubles are floating-point numbers, like 3.14159 or 49.0. . Booleans have only two possible values: TRUE and FALSE. . NULL is a special type that only has one value: NULL. . Strings are sequences of characters, like PHP 4.0 supports string operations. . Arrays are named and indexed collections of other values. . Objects are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class. . Resources are special variables that hold references to resources external to PHP (such as database connections). Of these, the first five are simple types, and the next two (arrays and objects) are compound the compound types can package up other arbitrary values of arbitrary type, whereas the simple types cannot. We treat only the simple types in this chapter, since arrays (Chapter 9) and objects (Chapter 20) need chapters all to themselves. Finally, the thorniest details of the type system, including discussion of the resource type, are deferred to Chapter 25. The Simple Types The simple types in PHP (integers, doubles, Booleans, NULL, and strings) should mostly be familiar to those with programming experience (although we will not assume that experience and will explain them in detail). The only thing likely to surprise C programmers is how few types there are in PHP. Many programming languages have several different sizes of numerical types, with the larger ones allowing a greater range of values, but also taking up more room in memory. For example, the C language has a short type (for relatively small integers), a long type (for possibly larger integers), and an int type (which might be intermediate, but in practice is sometimes identical either to the short or long type). It also has floating-point types, which vary in their precision. This kind of typing choice made sense in an era when tradeoffs between
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Chapter 5 (Best web hosting site) . Syntax and Variables 71 This

Thursday, November 29th, 2007

Chapter 5 . Syntax and Variables 71 This is identical to calling error_reporting() on the integer value of E_ALL, but is better because the actual value of E_ALL may change from one version of PHP to the next. It s also possible to create your own constants using the define() form, although this is more unusual than referring to built-in constants. The code: define(MY_ANSWER, 42); would cause MY_ANSWER to evaluate to 42 everywhere it appears in your code. There is no way to change this assignment after it has been made, and like variables, constants that are not part of PHP itself do not persist across pages unless they are explicitly passed to a new page. Ultimately, you probably will not need to define constants very often, if ever. When created constants are used, they are generally most usefully defined in an external include file and might be used for such information as a sales-tax rate or perhaps an exchange rate. Types in PHP: Don t Worry, Be Happy All programming languages have some kind of type system, which specifies the different kinds of values that can appear in programs. These different types often correspond to different bitlevel representations in computer memory, although in many cases programmers are insulated from having to think about (or being able to mess with) representations in terms of bits. PHP s type system is simple, streamlined, and flexible, and it insulates the programmer from low-level details. PHP makes it easy not to worry too much about typing of variables and values, both because it does not require variables to be typed and because it handles a lot of type conversions for you. No variable type declarations As you saw in Chapter 4, the type of a variable does not need to be declared in advance. Instead, the programmer can jump right ahead to assignment and let PHP take care of figuring out the type of the expression assigned: $first_number = 55.5; $second_number = Not a number at all ; Automatic type conversion PHP does a good job of automatically converting types when necessary. Like most other modern programming languages, PHP will do the right thing when, for example, doing math with mixed numerical types. The result of the expression $pi = 3 + 0.14159 is a floating-point (double) number, with the integer 3 implicitly converted into floating point before the addition is performed. Types assigned by context PHP goes further than most languages in performing automatic type conversions. Consider: $sub = substr(12345, 2, 2); print( sub is $sub
);
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

70 Part I . PHP: The (Web server version) Basics accomplish

Thursday, November 29th, 2007

70 Part I . PHP: The Basics accomplish this, and the different techniques are a lot of what the rest of this book is about. For example, you can pass information from page to page using GET and POST variables (Chapter 7), store information persistently in a database (all of Part II of this book), associate it with a user s session using PHP s session mechanism (Chapter 24), or store it on a user s hard disk via a cookie (Chapter 24). Functions and variable scope Except inside the body of a function, variable scope in PHP is quite simple: Within any given execution of a PHP file, just assign a variable, and its value will be there for you later. We haven t yet covered how to define your own functions, but it s worth a look-ahead note: Variables assigned within a function are local to that function, and unless you make a special declaration in a function, that function won t have access to the global variables defined outside the function, even when they are defined in the same file. (We will discuss the scope of variables in functions in depth when we cover function definitions in Chapter 6.) You can switch modes if you want One scoping question that we had the first time we saw PHP code was: Does variable scope persist across tags? For example, we have a single file that looks like: ); ?> Should we expect our assignment to $username to survive through the second of the two PHP-tagged areas? The answer is yes variables persist throughout a thread of PHP execution (in other words, through the whole process of producing a Web page in response to a user s request). This is a single manifestation of a general PHP rule, which is that the only effect of the tags is to let the PHP engine know whether you want your code to be interpreted as PHP or passed through untouched as HTML. You should feel free to use the tags to switch back and forth between modes whenever it is convenient. Constants In addition to variables, which may be reassigned, PHP offers constants, which have a single value throughout their lifetime. Constants do not have a $ before their names, and by convention the names of constants usually are in uppercase letters. Constants can contain only scalar values (numbers and string). Constants have global scope, so they are accessible everywhere in your scripts after they have been defined even inside functions. For example, the built-in PHP constant E_ALL represents a number that indicates to the error_reporting() function that all errors and warnings should be reported. A call to error_reporting() might look like this: error_reporting(E_ALL);
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Chapter 5 . Syntax and Variables (Simple web server) 69 print( never_set

Wednesday, November 28th, 2007

Chapter 5 . Syntax and Variables 69 print( never_set print value: $never_set
); if ($set_var == $never_set) print( set_var is equal to never_set!
); if (IsSet($set_var)) print( set_var is set.
); else print( set_var is not set.
); if (IsSet($never_set)) print( never_set is set.
); else print( never_set is not set. ); Oddly enough, this code will produce the following output: set_var print value: 0 never_set print value: set_var is equal to never_set! set_var is set. never_set is not set. The variable $never_set has never been assigned, so it produces an empty string when a string is expected (as in the print statement) and a zero value when a number is expected (as in the comparison test that concludes that the two variables are the same). Still, IsSet can tell the difference between $set_var and $never_set. Assigning a variable is not irrevocable the function unset() will restore a variable to an unassigned state (for example, unset($set_var); will make $set_var into an unbound variable, regardless of its previous assignments). Variable scope Scope is the technical term for the rules about when a name (for, say, a variable or function) has the same meaning in two different places and in what situations two names spelled exactly the same way can actually refer to different things. Any PHP variable not inside a function has global scope and extends throughout a given thread of execution. In other words, if you assign a variable near the top of a PHP file, the variable name has the same meaning for the rest of the file; and if it is not reassigned, it will have the same value as the rest of your code executes (except inside the body of functions). The assignment of a variable will not affect the value of variables with the same name in other PHP files or even in repeated uses of the same file. For example, let s say that you have two files, startup.php and next_thing.php, which are typically visited in that order by a user. Let s also say that near the top of startup.php, you have the line: $username = Jane Q. User ; which is executed only in certain situations. Now, you might hope that, after setting that variable in startup.php, it would also be preset automatically when the user visited next_thing.php, but no such luck. Each time a PHP page executes, it assigns and reassigns variables as it goes, and those variables disappear at the end of a page s production. Assignments of variables in one file do not affect variables of the same name in a different file or even in other requests for the same file. Obviously, there are many situations in which you would like to hold onto information for longer than it takes to generate a particular Web page. There are a variety of ways you can
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

68 Part I . PHP: The Basics It s (Web server extensions)

Wednesday, November 28th, 2007

68 Part I . PHP: The Basics It s conceivable that you will want to actually print the preceding math expression rather than evaluate it. You can force PHP to treat a mathematical variable assignment as a string by quoting the expression: $pi = 3 + 0.14159 ; Reassigning variables There is no interesting distinction in PHP between assigning a variable for the first time and changing its value later. This is true even if the assigned values are of different types. For example, the following is perfectly legal: $my_num_var = This should be a number hope it s reassigned ; $my_num_var = 5; If the second statement immediately follows the first one, the first statement has essentially no effect. Unassigned variables Many programming languages will object if you try to use a variable before it is assigned; others will let you use it, but if you do you may find yourself reading the random contents of some area of memory. In PHP, the default error-reporting setting allows you to use unassigned variables without errors, and PHP ensures that they have reasonable default values. If you would like to be warned about variables that have not been assigned, you should change the error-reporting level to E_ALL (the highest level possible) from the default level of error reporting. You can do this either by including the statement error_reporting(E_ALL); at the top of a script or by changing your php.ini file to set the default level (see Chapters 30 and 31). Default values Variables in PHP do not have intrinsic types a variable does not know in advance whether it will be used to store a number or a string of characters. So how does it know what type of default value to have when it hasn t yet been assigned? The answer is that, just as with assigned variables, the type of a variable is interpreted depending on the context in which it is used. In a situation where a number is expected, a number will be produced, and this works similarly with character strings. In any context that treats a variable as a number, an unassigned variable will be evaluated as 0; in any context that expects a string value, an unassigned variable will be the empty string (the string that is zero characters long). Checking assignment with IsSet Because variables do not have to be assigned before use, in some situations you can actually convey information by selectively setting or not setting a variable! PHP provides a function called IsSet that tests a variable to see whether it has been assigned a value. As the following code illustrates, an unassigned variable is distinguishable even from a variable that has been given the default value: $set_var = 0; //set_var has a value //never_set does not print( set_var print value: $set_var
); Cross- Reference
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Chapter 5 . Syntax and Variables 67 different (Web hosting billing)

Tuesday, November 27th, 2007

Chapter 5 . Syntax and Variables 67 different result if you take a single-line comment and replace one of the spaces with an end-of-line character. A more accurate way of putting it is that, after the comments have been stripped out of the code, PHP code is whitespace insensitive. Variables The main way to store information in the middle of a PHP program is by using a variable a way to name and hang on to any value that you want to use later. Here are the most important things to know about variables in PHP (more detailed explanations will follow): . All variables in PHP are denoted with a leading dollar sign ($). . The value of a variable is the value of its most recent assignment. . Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right. . Variables can, but do not need, to be declared before assignment. . Variables have no intrinsic type other than the type of their current value. . Variables used before they are assigned have default values. PHP variables are Perl-like All variables in PHP start with a leading $ sign just like scalar variables in the Perl scripting language, and in other ways they have similar behavior (need no type declarations, may be referred to before they are assigned, and so on). (Perl hackers may need to do no more than skim the headings of this section, which is really for the rest of us.) After the initial $, variable names must be composed of letters (uppercase or lowercase), digits (0 9), and underscore characters (_). Furthermore, the first character after the $ may not be a number. Declaring variables (or not) This subheading is here simply because programmers from some other languages might be looking for it in languages such as C, C++, and Java, the programmer must declare the name and type of any variable before making use of it. However in PHP, because types are associated with values rather than variables, no such declaration is necessary the first step in using a variable is to assign it a value. Assigning variables Variable assignment is simple just write the variable name, and add a single equal sign (=); then add the expression that you want to assign to that variable: $pi = 3 + 0.14159; // approximately Note that what is assigned is the result of evaluating the expression, not the expression itself. After the preceding statement is evaluated, there is no way to tell that the value of $pi was created by adding two numbers together.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Web design service - 66 Part I . PHP: The Basics You

Tuesday, November 27th, 2007

66 Part I . PHP: The Basics You can put any kind of statement in a brace-enclosed block, including, say, an if statement that itself has a brace-enclosed block. This means that if statements can have other if statements inside them. In fact, this kind of nesting can be done to an arbitrary number of levels. Comments A comment is the portion of a program that exists only for the human reader. The very first thing that a program executor does with program code is to strip out the comments, so they cannot have any effect on what the program does. Comments are invaluable in helping the next person who reads your code figure out what you were thinking when you wrote it, even when that person is yourself a week from now. PHP drew its inspiration from several different programming languages, most notably C, Perl, and Unix shell scripts. As a result, PHP supports styles of comments from all those languages, and those styles can be intermixed freely in PHP code. C-style multiline comments The multiline style of commenting is the same as in C: A comment starts with the character pair /* and terminates with the character pair */. For example: /* This is a comment in PHP */ The most important thing to remember about multiline comments is that they cannot be nested. You cannot put one comment inside another. If you try, the comment will be closed off by the first instance of the */ character pair, and the rest of what was intended to be an enclosing comment will instead be interpreted as code, probably failing horribly. For example: /* This comment will /* fail horribly on the last word of this */ sentence */ This is an easy thing to do unintentionally, usually when you try to deactivate a block of commented code by commenting it out. Single-line comments: # and // In addition to the /* … */ multiple-line comments, PHP supports two different ways of commenting to the end of a given line: one inherited from C++ and Java and the other from Perl and shell scripts. The shell-script-style comment starts with a pound sign, whereas the C++ style comment starts with two forward slashes. Both of them cause the rest of the current line to be treated as a comment, as in the following: # This is a comment, and # this is the second line of the comment // This is a comment too. Each style comments only // one line so the last word of this sentence will fail horribly. The very alert reader might argue that single-line comments are incompatible with what we said earlier about whitespace insensitivity. That would be correct you will get a very
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Chapter 5 . Syntax and Variables 65 (Web hosting isp) Assignment

Monday, November 26th, 2007

Chapter 5 . Syntax and Variables 65 Assignment expressions A very common kind of expression is the assignment, where a variable is set to equal the result of evaluating some expression. These have the form of a variable name (which always starts with a $), followed by a single equal sign, followed by the expression to be evaluated. For example: $eight = 2 * (2 * 2) assigns the variable $eight the value you would expect. An important thing to remember is that even assignment expressions are expressions and so have values themselves! The value of an expression that assigns a variable is the same as the value assigned. This means that you can use assignment expressions in the middle of more complicated expressions. If you evaluate the statement: $ten = ($two = 2) + ($eight = 2 * (2 * 2)) each variable would be assigned a numerical value equal to its name. Reasons for expressions and statements There are usually only two reasons to write an expression in PHP: for its value or for a side effect. The value of an expression is passed on to any more complicated expression that includes it; side effects are anything else that happens as a result of the evaluation. The most typical side effects involve assigning or changing a variable, printing something to the user s screen, or making some other persistent change to the program s environment (such as interacting with a database). Although statements are expressions, they are not themselves included in more complicated expressions. This means that the only good reason for a statement is a side effect! It also means that it is possible to write legal (yet totally useless statements) such as the second of these: print( Hello ); // side effect is printing to screen 2 * 3 + 4; // useless - no side effect $value_num = 3 * 4 + 5; // side effect is assignment store_in_database(49.5); // side effect to DB Braces make blocks Although statements cannot be combined like expressions, you can always put a sequence of statements anywhere a statement can go by enclosing them in a set of curly braces. For example, the if construct in PHP has a test (in parentheses) followed by the statement that should be executed if the test is true. If you want more than one statement to be executed when the test is true, you can use a brace-enclosed sequence instead. The following pieces of code (which simply print a reassuring statement that it is still true that 1 + 2 is equal to 3) are equivalent: if (3 == 2 + 1) print( Good - I haven t totally lost my mind.
); if (3 == 2 + 1) { print( Good - I haven t totally ); print( lost my mind.
); }
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Apache web server for windows - 64 Part I . PHP: The Basics The

Monday, November 26th, 2007

64 Part I . PHP: The Basics The particular ways that operators group expressions are called precedence rules operators that have higher precedence win in grabbing the expressions around them. If you want, you can memorize the rules, such as the fact that * always has higher precedence than +. Or you can just use the following cardinal rule: When in doubt, use parentheses to group expressions. For example: $result1 = 2 + 3 * 4 + 5; // is equal to 19 $result2 = (2 + 3) * (4 + 5); // is equal to 45 Operator precedence rules remove much of the ambiguity about how subexpressions are associated. But what about when two operators have the same precedence? Consider this expression: $how_much = 3.0 / 4.0 / 5.0; Whether this is equal to 0.15 or 3.75 depends on which division operator gets to grab the number 4.0 first. There is an exhaustive list of rules of associativity in the online manual, but the rule to remember is that associativity is usually left-before-right that is, the preceding expression would evaluate to 0.15, because the leftmost of the two division operators wins the dispute over precedence. The final wrinkle is order of evaluation, which is not quite the same thing as associativity. For example, look at the arithmetic expression: 3 * 4 + 5 * 6 We know that the multiplications will happen before the additions, but that is not the same as knowing which multiplication PHP will perform first. In general, you need not worry about evaluation order, because in almost all cases it will not affect the result. You can construct weird examples where the result does depend on order of evaluation, usually by making assignments in subexpressions that are used in other parts of the expression. For example: $huh = ($this = $that + 5) + ($that = $this + 3); // BAD But don t do this, okay? PHP may or may not have a predictable order of evaluation of expressions, but you shouldn t depend on it so we re not going to tell you! (The one legitimate use of relying on left-to-right evaluation order is in short-circuiting Boolean expressions, which we cover in Chapter 6.) Expressions and types Usually, the programmer is careful to match the types of expressions with the operators and functions that combine them. Common expressions are mathematical (with mathematical operators combining numbers) or Boolean (combining true-or-false statements with ands and ors) or string expressions (with operators and functions constructing strings of characters). As with the rest of PHP, however, the treatment of types is surprisingly forgiving. Consider the following expression, which deliberately mixes the types of subexpressions in an inappropriate way: 2 + 2 * nonsense + TRUE Rather than produce an error, this evaluates to the number 3. (You can take this as a puzzle for now, but we will explain how such a thing can happen in the Types in PHP section of this chapter.)
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.