Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
PHP and MySQL Web Development - P13: PHP and MySQL Web Development teaches the reader to develop dynamic, secure, commercial Web sites. Using the same accessible, popular teaching style of the first edition, this best-selling book has been updated to reflect the rapidly changing landscape of MySQL and PHP. | Variable Scope 27 In our sample application we might store the prices for each of the items on sale as constants.You can define these constants using the define function define TIREPRICE 100 define OILPRICE 10 define SPARKPRICE 4 Add these lines of code to your script. You will notice that the names of the constants are all in uppercase. This is a convention borrowed from C that makes it easy to distinguish between variables and constants at a glance. This convention is not required but will make your code easier to read and maintain. We now have three constants that can be used to calculate the total of the customer s order. One important difference between constants and variables is that when you refer to a constant it does not have a dollar sign in front of it. If you want to use the value of a constant use its name only. For example to use one of the constants we have just created we could type echo TIREPRICE As well as the constants you define PHP sets a large number of its own. An easy way to get an overview of these is to run the phpinfo command phpinfo This will provide a list of PHP s predefined variables and constants among other useful information. We will discuss some of these as we go along. Variable Scope The term scope refers to the places within a script where a particular variable is visible. The four types of scope in PHP are as follows Built-in superglobal variables are visible everywhere within a script. Global variables declared in a script are visible throughout that script but not inside functions. Variables used inside functions are local to the function. Variables used inside functions that are declared as global refer to the global variable of the same name. In PHP 4.2 onwards the arrays _GET and _POST and some other special variables have their own scope rules. These are known as superglobals and can be seen everywhere both inside and outside functions. 28 Chapter 1 PHP Crash Course The complete list of superglobals is as follows GLOBALS