PHP and MySQL Web Development - P13

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 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 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

Không thể tạo bản xem trước, hãy bấm tải xuống
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.