Learn 10 basic knowledge of php summary
After reading some basic knowledge of PHP, I summarize here:
1, there are three ways to embed PHP script in HTML:
The following is quoted content:
<script language = "php">
One way to embed
echo ("test");
</ script>
<?
// embedded way two
echo "<br> test2";
?>
<? php
// embedded way three
echo "<br> test3";
?>
There is also an embedded way to use the same tag as Asp <%%>, but to modify the PHP.ini related configuration, it is not recommended.
2, PHP comments sub-single-line and multi-line comments, and java comments the same way.
The following is quoted content:
<?
// Here is a single comment
echo "test";
/ *
Here is a multi-line comment! Can write a lot of line comment content
* /
?>
Be careful not to have nested comments, such as / * aaaa / * asdfa * / asdfasdfas * /, there is a problem with such comments.
3, PHP main data type has 5 kinds:
integer, double, string, array, object.
4, the function calls external variables in the function, you need to use global to declare, otherwise can not be accessed, this is a PHP and other programming languages a difference. Case Code:
The following is quoted content:
<?
$ a = 1;
function test () {
echo $ a;
}
test (); // This will not output the result "1".
function test2 () {
global $ a;
echo $ a;
}
test2 (); // This will output the result "1".
?>
Note: PHP can declare static variables inside functions. Use the same language C.
5, variable variables, variable functions
The following is quoted content:
<?
// Variables variables
$ a = "hello";
$$ a = "world";
echo "$ a $ hello"; // will output "hello world"
echo "$ a $ {$ a}"; // will also output "hello world"
?>
<?
// variable function
function func_1 () {
print ("test");
}
function fun ($ callback) {
$ callback ();
}
fun ("func_1"); // This will output "test"
?>
6, PHP supports both scalar arrays and associative arrays, you can use list () and array () to create an array, the array subscript from 0 start. Such as:
The following is quoted content:
<?
$ a [0] = "abc";
$ a [1] = "def";
$ b ["foo"] = 13;
$ a [] = "hello"; // $ a [2] = "hello"
$ a [] = "world"; // $ a [3] = "world"
$ name [] = "jill"; // $ name [0] = "jill"
$ name [] = "jack"; // $ name [1] = "jack"
?>
7, the associated parameters passed (& use), two methods. example:
The following is quoted content:
<?
//method one:
function foo (& $ bar) {
$ bar. = "and something extra";
}
$ str = "This is a String,";
foo ($ str);
echo $ str; // output: This is a String, and something extra
echo "<br>";
//Method Two:
function foo1 ($ bar) {
$ bar. = "and something extra";
}
$ str = "This is a String,";
foo1 ($ str);
echo $ str; // output: This is a String,
echo "<br>";
foo1 (& $ str);
echo $ str; // output: This is a String, and something extra
?>
8, the function default value. PHP function support set the default value, and C + + style the same.
The following is quoted content:
<?
function makecoffee ($ type = "coffee") {
echo "making a cup of $ type.n";
}
echo makecoffee (); // "making a cup of coffee"
echo makecoffee ("espresso"); // "making a cup of espresso"
/ *
Note: When using parameter defaults, all parameters that have default values should be defined after parameters without defaults. Otherwise, the program will not follow the desired work.
* /
function test ($ type = "test", $ ff) {// Error example
return $ type. $ ff;
}
9, PHP a few special symbolic significance.
$ Variable
& Variable address (before adding variable)
@ Does not show the wrong message (added before the variable)
-> Class method or property
=> The element value of the array
?: Ternary operator
10, include () statement and require () statement
If you want to include files based on conditions or loops, you need to include ().
The require () statement is simply included once, and any conditional statements or loops are not valid for it.
Because include () is a special statement structure, if the statement in a statement block, you must include him in a statement block.
The following is quoted content:
<?
// The following is the wrong statement
if ($ condition)
include ($ file);
else
include ($ other);
// The following is the correct statement
if ($ condition) {
include ($ file);
} else
{
include ($ other);
}
?>