PHP learning-PHP source code

Source: Internet
Author: User
Ec (2); PHP has been used for learning PHP for a while, and PHP development speed should be good. PHP code for the same project is estimated to be much less than JSP. However, some problems were also found during use. I also read some other PHP project code to explain my feelings. 1. PHP is flexible but not graffiti. if you write all database operations on a page, maintenance will be difficult. at least the database operations and page rendering should be separated, similar to JSP and JavaBean. I believe this is also PHP script ec (2); script

PHP Learning

After learning to use PHP for a while, PHP development speed should be good. The PHP code of the same project is estimated to be much less than JSP.
However, some problems were also found during use. I also read some other PHP project code to explain my feelings.

1. PHP is flexible but not applicable to graffiti.
If you write all database operations on a page, maintenance will be difficult. At least the database operations and page rendering should be separated, similar to JSP
The method of adding JavaBean is believed to be the simple MVC used by PHPer.

2. PHP Constants
The array of PHP array can be read and written flexibly, and the page may often have such
Echo $ myArray [''mykey'']; The row returned by mysql_fetch_array for database operations is also, in case the key value is developed by ''mykey''
When the definition is not fixed, or ''mykey'' is the column name, it suddenly needs to be changed. If this constant is available in every file, it will be depressing to change the code.
PHP uses define, and const is used in the class to represent constants.
Define ('My _ constant'', 'My _ value '');
Echo MY_CONSTANT;
Class MyClass {
Const MY_CONSTANT = 'my _ value '';
Public function test (){
Echo self: MY_CONSTANT;
}
}
// External usage
Echo MyClass: MY_CONSTANT;

3. = value assignment copy
= In most cases, a value is assigned as a copy.
$ OldValue = array (1, 2, 3 );
$ NewValue = $ oldValue;
$ NewValue [] = 4;
Echo $ oldValue; // 1, 2, 3
Echo $ newValue; // 1, 2, 3, 4
We can see that $ newValue is only a value copy, and its modification will not change $ oldValue. It is estimated that the value assignment of the class instance is also a copy.

What else will cause the copy? The input parameter of the function, and the return value should also be passed by default.
Function test ($ param ){
Echo $ param; // string
$ Param = ''newstring '';
Echo $ param; // newstring
}
$ StrParam = ''string '';
Test ($ strParam );
Echo $ strParam; // string

Foreach may cause copying during array Iteration
$ MyArray = array (''key1' => ''value1 '', ''key2' => ''value2 '');
Foreach ($ myArray as $ key => $ value ){
Echo "$ key = $ value
";
}
According to the PHP manual, a new array is copied during foreach. Even if the value is modified in the loop, the old array is not affected.

= Is it not a copy for clone?

I personally do not like copying. If the copying is large, it may affect the execution efficiency.

4. Use reference to avoid copying
Actually, the = & reference is actually an Alias. I think this is java and the c # class is used by default. PHP must be explicitly added.
C ++.
$ OldValue = array (1, 2, 3 );
$ NewValue = & $ oldValue;
$ NewValue [] = 4;
Echo $ oldValue; // 1, 2, 3, 4
Echo $ newValue; // 1, 2, 3, 4

$ MyClassInstance = new MyClass () in PHP4; it is actually a copy and must
$ MyClassInstance = & new MyClass ();
PHP5 supports much better. The new feature is the & new reference in PHP4 by default.

If the input parameter is large (assuming it is a large array), the reference can be passed to avoid copying the value.
Function test (& $ param ){
Echo $ param; // string
$ Param = ''newstring '';
Echo $ param; // newstring
}
$ StrParam = ''string '';
Test ($ strParam );
Echo $ strP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.