PHP and MySQL Web Development - P35

PHP and MySQL Web Development - P35: 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. | Pass by Reference Versus Pass by Value 137 inside the function var contents outside the function var contents Note that the variable is in scope from the point in which the line global var is could have declared the function above or below where we call it. Note that function scope is quite different from variable scope The location of the function declaration is inconsequential what is important is where we call the function and therefore execute the code within it. You can also use the global keyword at the top of a script when a variable is first used to declare that it should be in scope throughout the script. This is possibly a more common use of the global keyword. You can see from the preceding examples that it is perfectly legal to reuse a variable name for a variable inside and outside a function without interference between the two. It is generally a bad idea however because without carefully reading the code and thinking about scope people might assume that the variables are one and the same. Pass by Reference Versus Pass by Value If we want to write a function called incremento that allows us to increment a value we might be tempted to try writing it as follows function increment value amount 1 value value amount This code will be of no output from the following test code will be 10 . value 10 increment value echo value The contents of value have not changed. This is because of the scope rules. This code creates a variable called value which contains 10. It then calls the function incremento .The variable value in the function is created when the function is called. One is added to it so the value of value is 11 inside the function until the function ends and we return to the code that called it. In this code the variable value is a different variable with global scope and therefore unchanged. One way of overcoming this is to declare value in the function as global but this means that in order to use this function the variable that .

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.