Basic code
<? PHP Echo "Hello World";
?>
Variable definition
<? PHP $a=true; $bool _value=true; $integer _value=9; $float _value=3.1415926; $string _value= "pi_is_{{$float _value}}." ; Echo $string _value; // pi_is_{3.1415926}.? >
Output
<?PHP$str= "String_";$return _value=Print($str);//Success:return 1, Faile:return 0Echo $return _value;//no return value$return _value=printf("Value:%f", 3.1415926);$str=sprintf("Value:%f", 3.1415926);//Print to Variable strEcho $str;?>
Data Structures-arrays
<? php $season [0]= ' Spring ' $season [1]= ' Summer ' $season [2]= ' Autumn ' $season [3]= ' Winter ' $map [' Key1 ']= ' value1 ' $map [' Key2 ']= ' value2 ' // season & Map is all array echo $season echo $map ; ?
Data Structures-Objects
<?PHPclasspoint{Private $id=0; Public $x,$y; function__construct ($x,$y) { $this->x=$x; $this->y=$y; } functionPrint_info () {Echo $this->id, "<br>"; Echo $this->x, "<br>"; Echo $this->y, "<br>"; }}$p=NewPoint (1,3);$p-print_info ();?>
Data Structures-Resource data types
Similar to the concept of a handle, it needs to be destroyed when it is finished.
Data structure-NULL type
<? PHP $uninitialized ; $null _var1=null; $var= "123"; unset ($var); // This three variable is null?>
Type conversions
<?PHP//int or integer, float or double or real, string, array, object, bool or boolean//if a string starts with number, it w Ill is truncated to a number in arithmetic//if a string starts with Non-number, it'll be is zero in Arithmetic//it's OK to Run "3.14ABC" +6, so double can be neglectableEcho(Double) "A3.1415926ABC";//intval, Doubleval, Floatval, Floatval, StrvalEcho intval(3.1415926);//var is supposed by array, Boolean, float, integer or int, null, object, Unknow, String$value= "3.1415926";$return _value=Settype($value, int);//success:1Echo $value;?>
Variable
Value pass/reference pass, mutable variable
<?PHP//by value$int 1=1;$int 2=int1;$int 2=5;Echo $int 1, "<br>",$int 2, "<br>";//1 5//by Reference$int 1=1;$int 2=&$int 1;$int 2=5;Echo $int 1, "<br>",$int 2, "<br>";//5 5//variable variables:use Variable value to define a Variable named value$sun= "Hot";$$sun= "Moon";//equal to $hot = "Moon"//${$sun} are eual to $hotEcho $sun, "<br>", ${$sun}, "<br>";//User AliasesEcho $hot, "<br>";?>
Super global variable Superglobals
- $GLOBALS
- $_SERVER
- $_GET
- $_POST
- $_FILES
- $_COOKIE
- $_SESSION
- $_REQUEST
- $_ENV
Variable destruction
Re-assign Value
Unset ()
Constant
<? PHP class test{ //The scope is thisclass const name= ""; function classn () { //user without $ Echo test::name*312;} } // The scope is global and it can be used anywhere Define ("Site_global", "www.site.com");? >
Magic Constants
A Few "magical" PHP constants
Name |
Description |
__LINE__ |
The current line number of the file. |
__FILE__ |
The full path and filename of the file with Symlinks resolved. If used inside an include, the name of the included file is returned. |
__DIR__ |
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname (__file__). This directory name does not has a trailing slash unless it is the root directory. |
__FUNCTION__ |
The function name. |
__CLASS__ |
The class name. The class name includes the namespace it is declared in (e.g. Foo\bar). Note that as of PHP 5.4 __class__ works also in traits. When used in a trait method, __class__ is the name of the CLASS the trait is used in. |
__TRAIT__ |
The trait name. The trait name includes the namespace it is declared in (e.g. Foo\bar). |
__METHOD__ |
The class method name. |
__NAMESPACE__ |
The name of the current namespace. |
Special operators
': the inverse quotation marks, equivalent to the Shell_exec () function (Safe mode can only use functions),
<? PHP echo 'dir';? >
@: Error control, placed before the expression, the resulting error is ignored. If the Track_errors property is activated, the error is stored in the $php_errormsg variable.
Foreach
<? PHP $season [0]= ' Spring '; $season [1]= ' Summer '; $season [2]= ' Autumn '; $season [3]= ' Winter '; foreach ($seasonas$s) { echo$s, "<br>" ;}? >
PHP Quick Learn