Basic php knowledge
<Body>
<? Php
// Single line comment
/* Multi-line comment */
Echo "hello world", "php hello"; // (most commonly used) Output Mode 1, which can output multiple strings
Print "hello"; // output method 2. Only one string can be output.
Print_r (); // an array is required to print the array.
Var_dump (); // output variable content, type, length, test data
Data Type
Integer int decimal (float, double, double, float) decmial
Character char string boolean data bool Date and Time datetime
Object
$ A = 5;
$ B = "10 ";
$ B = (int) $ B; // type conversion method 1
Settype ($ a, "string"); // type conversion method 2
About Variables
Unset ($ a); // delete a variable
Var_dump (isset ($ a); // determines whether a variable is defined, as long as it exists, it is true.
Var_dump (empty ($ a); // determines whether the variable is null. 0 indicates null, and "" indicates null.
Fetch address
$ A = 10;
$ B = & $ a; // the address in a is handed over to B.
$ B = 5; // as long as one of the changes, the other changes accordingly.
Echo $;
Variable
$ A = "hello ";
$ Hello = "hello ";
Echo $;
Definition of strings
$ S = "James ";
$ A = "El \" lo {$ s} ";
$ B = 'El \ "lo $ ';
// Escape characters can be parsed in double quotes, but not in single quotes
// Variables are allowed in double quotes, but not in single quotes.
// Use.
Echo "hello". "world ";
Echo $;
Echo $ B;
$ S = <
<Div> // $ B = $ a at $ a = $ a + 1;
$ B = ++ $ a; // $ a = $ a + 1 at $ B = $
Echo $ B;
$ A = true;
$ B = true;
Var_dump ($ a & $ B); // If B = false, the output is used | (OR) is also true;
Var_dump (! $ );
Notice: reminder Warning: Warning Error: Error
$ A = 10;
$ B = 5;
Echo $ a = $ B? "OK": "NO"; // ternary Operator
$ Sex = false;
Echo $ sex? "Male": "female"; // ternary operator. The value true indicates male, and the value false indicates female.
Statement
Branch statement
If (){}
If () {} else {}
If () {} else if {}
Switch () {case}
$ A = 2;
Switch ($)
{
Case 1:
Echo & quot; 1111 & quot ";
Break;
Case 2:
Echo & quot; 2222 & quot ";
Break;
Default:
Echo & quot; 3333 & quot ";
}
Loop statement
For ($ I = 0; $ I <10; $ I ++)
{
Echo "{$ I} <br> ";
}
While (1)
{
Echo "111 <br>"; // endless loop
}
$ A = 0;
While ($ a <10)
{
$ A ++;
Echo "{$ a} <br> ";
}
Function
Four Elements: Return type function name parameter list function body
Function Dong () // weak type language
{
}
Public void Say (int a) // strongly typed language
{}
1. Simple Functions
Function Test ()
{
Echo "simple functions ";
}
Test ();
2. Functions with Parameters
Function Test ($)
{Echo $ ;}
Test ("hello ");
3. Functions with return values
Function Test ()
{Return "test ";}
Echo Test ();
4. Variable Parameter Functions
Function Test ()
{
$ Arr = func_get_args (); // obtain all parameters and return an array
$ Sum = 0;
For ($ I = 0; $ I <count ($ arr), $ I ++)
{
$ Sum = $ sum + $ arr [$ I];
}
Return $ sum;
}
Echo Test (1, 2, 3, 4, 5 );
Generate random number
Echo rand ();
Echo rand (); // random number between 0 and 10
Date and Time
Echo time (); // Timestamp
Echo date ("Y-m-d H: I: s"); // or date ("Y, m, d, H: I: s ");
Y year m month d day H Represents the hour in the 24 hour system I minute s second
Echo strtotime ("14:57:57"); // converts a string to a timestamp
?>
</Body>