Now say the function, PHP function is simple
<?phpfunction A () {echo "Hello";} a ();?>
Functions are defined as a function, as JavaScript does not require a function type, and even formal parameters do not need
<?php$a=1; function A ($a) {echo $a;} a ($a);?>
Output 1
<?php$a=1; function A ($a) {$a = $a +1;echo $a;} a ($a);?>
Output 2
<?php$a=1; function A ($a) {$a = $a +1;echo $a;} a ($a); echo $a;?>
The last A is actually the beginning a will print 1
Of course, the function can have a return value.
<?phpfunction A () {return 1;} echo a ();?>
Printing 1
In addition, the PHP function is a bit different from other programming languages, that is, you can assign default values to formal parameters directly
<?php$a=2; function add ($a, $b =3) {return $a + $b;} echo Add ($a);?>
Printing 5
Isn't that a good point?
Of course, you can also send a different value to B.
<?php$a=2; function add ($a, $b =3) {return $a + $b;} echo Add ($a, $b =5);?>
Printing 7
Here's a simple introduction to PHP
As early as the C language has this concept, the introduction of header files
PHP has two functions to introduce a file
The Include () function and the require () function are included and introduced in English meaning, the meaning is almost
Two function functions are actually the same, but a little different
Include () function if an error occurs (such as the introduction of a file does not exist), the code will still be executed later
Require () function once an error occurs, the following code is no longer executed, that is, the interrupt program
See below
Create a new PHP file a.php in the root directory
The code is as follows
<?phpecho "Hello";? >
Very simple
Then another PHP file introduces
<?phpinclude ' a.php '; echo "Hello";? >
Will print a two Hello
If the file does not exist
<?phpinclude ' a1.php '; echo "Hello";? >
There will be a warning, but the 2nd hello will still print
and
<?phprequire ' a1.php '; echo "Hello";? >
will report a fatal error, 2nd Hello will not print
In addition the introduction of require (' a.php '); It can also be enclosed in parentheses, or it can be enclosed in double or single quotes.
There are also include_once () and require_once ()
Functionality is the same as Include,require, but it will determine before introducing a file whether it has been introduced before, and if it has been introduced before, no longer introduce
<?phprequire (' a.php '); require (' a.php ');? >
Print two Hello
<?phprequire (' a.php '); require_once (' a.php ');? >
Print a Hello
Even if the previous include is the same
<?phpinclude (' a.php '); require_once (' a.php ');? >
Print a Hello
Include_once () acts the same as above
Generally used in the majority of require_once, especially in large-scale projects
Include is the HTML code that introduces the head and tail of HTML, and seldom introduces PHP code.
Require is the introduction of classes or functions.
In fact, if confident enough to confirm that there will not be more than the introduction of the same file, with require is completely no problem, and efficiency is high, at least not to judge, but large projects are used with caution, because it is teamwork, others write code may introduce the file you want to introduce, it is impossible to check each one, So it's appropriate to use require_once.
In addition to saying a small problem, the introduction of pure PHP files (no HTML code) the best one to be introduced into the file do not have a return tag?>
actually PHP code (no HTML)
<?phpecho "Hello";
Is able to execute, you do not write the system read to the document finally found that there is no default complement one
Why would you do that?
Because, by General
<?phpecho "Hello";? >
Then another PHP introduced
<?phprequire (' a.php '); echo "Hello";? >
Will print Hellohello
But if you accidentally
<?phpecho "Hello";? >------------cursor in > After you hit a few returns---------there is a carriage return symbol, but you do not see the------------carriage return symbol-------------
Most people have this habit.
And then introduce the print
<?phprequire (' a.php '); echo "Hello";? >
Will print
The middle of Hello Hello is separated by a space
Without?>, there's no such situation.
So.. Unless you ensure that there are no symbols behind the?> tag (carriage return, spaces, etc.)
You can use the back tag.
In fact, many projects are now aware of this. Writing classes rarely use a back-closing tag
The above is the PHP learning formally set sail (4) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!