Dynamic feature learning notes in PHP (1/3)

Source: Internet
Author: User

This article is mainly summarized for beginners who don't know anything about PHP features, so the demo code takes up more space.
Let's see what you need to add. Welcome to the Mail or in Phpchina's "Phper" magazine section to correct the error. A
Dynamic language is a language that can change the program structure and variable types at run time. For example, new classes and pairs of
images can be loaded and created, new functions or methods can be added and removed, and so on, such as Smalltalk, Ruby,
Python, PHP, Lua, Perl, Groovy, and so on. The converse is static language, such as C + +, Java, C #, and so on. The dynamic nature of the
Dynamic language determines that it requires less code and has more flexibility when it is developed. The dynamic nature of the PHP
lays the value of its existence, and familiarity with the dynamic nature of PHP makes us more able to utilize PHP. The
1 weakly typed variable
Dynamic language is designed to be a weak type, meaning that a variable is assigned to determine its data type, and the
Current code does not detect whether the variable is being used illegally until it is actually executed. A
PHP variable is a weakly typed variable means that we do not need to declare the type of the variable, automatically check the type of the variable at run time, and consider the type of the variable to be changed.
2, PHP dynamic characteristics:
Attribute 1, weakly type variable
PHP variable is a weakly typed variable means that we do not need to declare the type of the variable, automatically detect the type of the variable at run time, and can think of the type of change variable.

  code is as follows copy code

//php Weak type variable Example
$test = 1;
Print GetType ($test). ": $testn";
$test = 1.23456789;
Print GetType ($test). ": $testn";
$test = ' Test '; //variable automatically change type
Print GetType ($test). ": $testn";
$test = Array (' t ' => ' hi,i m from array! ');
Settype ($test, ' object '); //change the type of variable
print GetType ($test). ": {$test->t}n";
//Test variable type
if (Is_object ($test)) {
    print "Test type:my type is OBJECT.N";
} else {
    print GetType ($test);
}

A weak type does not mean that the code is unsafe or not robust. Extreme Programming has become a software development approach. This approach focuses on testing and uses a comprehensive unit test scenario to drive the development process. Through different environments
Execute the code you write, you can guarantee the security and robustness of your code. Experience has shown that in weakly typed languages, comprehensive use of weak types and unit tests is usually better than the type checking of traditional system programming languages (please join
Attribute 2, variable variable
Variable names for a variable can be dynamically set and used

The code is as follows Copy Code

Variable variable Example
$var = ' Hi ';
$ $var = ' Hello ';
Print $var;
Print $ $var;
Print $hi; Equivalent to the previous line

Feature 3, variable function
This means that if a variable name is followed by parentheses, PHP will look for a function with the same name as the value of the variable, and will attempt to execute it. This can be used to implement callback functions, function tables, and so on, among other things.

The code is as follows Copy Code

Variable Function Example
No parameter function
function A () {
print "I ' m an";
}
have parameter function
Function B ($param) {
print "I ' m b, param: $paramn";
}
$x = ' a ';
$x ();
$x = ' B ';
$x (' xxxxxxxxxxx ');

Feature 4, variable length parameter list
When calling a function, the number of parameters of the function can be different

The code is as follows Copy Code

function foo () {
$numargs = Func_num_args ();
Print "Number of arguments: $NUMARGSN";
Print "The third argument:". Func_get_arg (2). "N";
}
Foo (1, 2, ' ... @ ... ');
Foo (1, 2, ' ############ ', 4);

Home 1 2 3 last

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.