PHP Learning 00

Source: Internet
Author: User
Tags echo date echo name readfile variable scope

   /* * Uppercase small sensitive * Variables are case-sensitive, functions, Keywords (if,else), echo ... Case INSENSITIVE * * ///$str = "hehh";//echo $str;/ * Variable Scope * 2 Scopes * 1. Local * 2. Global globally * variables declared outside the function have global scope * variables declared within a function have local scope and can only be accessed inside the function * Global keywords * GL Obal is used to access all variables inside the function * PHP saves all global variables in the $GLOBAL [index] array, the index is the global variable name, and can be used inside the function, which can be updated directly with the global variable * Static keyword * Used to declare a local variable inside a function, this variable will not be destroyed after the function call is completed, that is, stored in the heap, not in the stack */$x=5;$y=Ten; functionmyTest() {$y=Ten;Echo"variable x is:".$x;Echo"
";Echo"Variable y is: $y";}//mytest ();//echo "variable x is:". $x;//echo "
";//echo "Variable y is: $y";//print_r ($GLOBALS); functionmyTest1() {Global$x,$y;Echo"x variable is: $x";Echo"
";Echo"variable is: $y";Echo"
";$y=$x+$y;Echo"X+y is: $x + $y";}//mytest1 ();//echo "
";//echo "y= $y"; functionmyTest2() {Static$x=0;Echo$x."
";$x++;}//mytest2 ();//mytest2 ();//mytest2 ();/ * echo and print * echo can output at least one string * Print can only output a string, return 1 * echo is a statement, no (), plus () can also * echo is slightly faster than print, because there is no return value * Print Also the statement does not (), plus () can also * * * *///echo "ddddd";//echo ' I ', "AM", ' Xue ';//print "
";//print "I";/ * PHP Data type * 1. String ' aaa ', ' AAA ' * 2. Integers, 3 formats * decimal * hex 0x start * Octal 0 start * 3. Float type, containing small tree, or exponent * 10.45,2.4e5, 2.3e-3 *///$x = 123;//var_dump ($x);//$y = 0x456;//var_dump ($y);//$z = 015;//var_dump ($z);//$a = 0.3;//var_dump ($a);//$b = 0.3e2;//var_dump ($b);/* Array: * *// * * NULL means the variable value is no value * *//* String function * 1.strlen () returns the length of the string * 2.STRPOS () find substring or sub-character * Returns the first position of the found character or string in the original string (starting at 0) * Returns False if not found * ///$str = "123";//$len = strlen ($STR);//echo $len;//$STR 1 = "Woshixue";//var_dump (Strpos ($str 1, "ee"));/* Constant : * Constant similar variable, once defined cannot undo or change definition, defined as global scope * Cannot modify constant in script * define (' name ', ' value ', optional (default = False,) *///define ("NAME", "Xuexinghai", false);//echo name;/* * cycle : * For,foreach,while,do...while * *//$arr = Array ("Apple", "orange", "banana");//foreach ($arr as $v)//Echo $v. "
";/ * Array * 1. Declaration of an array * Direct Declaration * Association * 2 Count gets the number of array elements *///Direct Declaration//$arr = Array ();//$arr [0] =3;//$arr [1]= "Xue";//var_dump ($arr);////Association Definitions//$arr 1 = Array ("name" = "Xue", age=>25);//var_dump ($arr 1);////defining a two-dimensional array//$arr 2 = Array (//0=> $arr,//1=> $arr 1//);//var_dump ($arr 2);//$arr 3[] = $arr 1;//$arr 3[' xxx '] = $arr 2;//$arr 3[7] =;//var_dump ($arr 3);////$arr 4 = Array ();//var_dump ($arr 4);////echo "ARR4 element is:", Count ($arr 4);//Array sorting//1.sort, sorting in ascending order of arrays///* * Super global variable , can be used anywhere * $GLOBALS Save all global Variables * $_server Save header, path, and script location * $_request The user saves the data submitted by the HTML form * $_p OST submitted form * $_get form submitted by GET $_files * $_files = Array (* ' MyFile ' =>array (* [name] + +) Pass-Peer review-backstage demo-20150912.zip [Type] = Application/zip [Tmp_name] = C:\Windows\Temp\phpDEB7.tmp [ERROR] + 0 [s IZE] = 507713 *) *) * $_ENV environment variable * $_cookie * $_session * *//session_start ();//$_session[' name '] = "Xue";//var_dump ($_session);//print_r ($_server);//if (!empty ($_post[' Sub ') )//print_r ($_files[' myFile ' [' tmp_name ']);/* dates and times * Date (FORMAT,TIMESTAMP); * Format: Formatted * timpstamp: timestamp, default current time timestamp * format: * d: Day of January 01-31 * * * M: A month of the year 01-12 * M: abbreviated as September Sep * * y: Year, 4 digits * y: Year 2 digits (without preceding 0) * * L: One day of the week, in English, such as Monday Monday * L: One day of the week, with The serial number means, for example, Monday is 0, Tuesday is 1 * D: Abbreviated day of the week * * Other characters, such as "/", "." or "-" can also be inserted into characters to add other formats. * Time Format: * H: With first 0 12-hour time * H: 24 hour system * I: With first 0 part 00-59 * S: With first 0 seconds 00-59 * A: Lowercase noon and afternoon * *//* * Created Date * 1. Returns the timestamp of Unix by Mktime () * Mktime (), which is the number of seconds from January 1, 1970 00:00:00 to the current period * Mktime (Hour,minute,second,month,day,yea R) * 2.strtotime () Use string to create date * resolves any English text time to UNIX timestamp * strotime (string $time [, int $now]); * $now for the specified time, default current time * This function expects to accept a string containing the U.S. English date format and attempt to resolve it to a Unix timestamp (seconds from January 1 1970 00:00:00 GMT) whose value is relative to the time given by the now parameter , if this parameter is not provided then the first parameter strtotime the current time of the system can be our common English time format, such as "2008-8-20" or "September 2000" and so on. It can also be a time description based on the parameter now, such as "+1 Day" and so on. * * */////$time = mktime ("x", "X", "1", "9", "Ten", Date (' Y '));//$time = Strtotime (' next month ');//Echo date ("Y year m month D,h:i:sa", $time);/* include require * server-side inclusion (SSI) for creating functions, headers, footers, or elements that can be reused on multiple pages. * Different: * require If an error occurs to stop the current script from running, give E_compile_error * include If an error occurs to continue the execution of the script, give e_warning warning * Therefore, as If you want to continue and output the results to the user, use includeif the included file is missing. Otherwise, in framework, CMS, or complex PHP application programming, always use require to reference critical files to the execution stream. This helps improve the security and integrity of your application in the event that a critical file is accidentally lost. * *//* * File Operation * ReadFile () reads the contents of the file into the output stream, returns the number of bytes, the read succeeds in the number of bytes returned * fopen (file name, mode of operation), *///readfile ("indexa.php");//$file = fopen (' index.php ', ' R ');//echo fread ($file, FileSize (' index.php '));/* exception Handling * try{*}catch (Exception $ex) {*} * Custom Exception class *///class MyException extends Exception{//Public Function errormessage () {//echo "error occurred in". $this->file. " The section ". $this->getline ()." Yes
";//echo "error message:". $this->getmessage ();// }//}//function hander () {//throw new Exception("No catch Exception");//}//set_error_handler (' hander ');//function Checknum ($num) {//if ($num > 1) {//throw new MyException("value must below 1");// }//}//throw New Exception(' uncaught Exception occurred ');/ * * Filter * Filter function * Filter_var filter a single variable by specifying a filter * Filter_var_array filter multiple variables by the same or different specified filters * filter_input get an input variable and filter it * filt Er_input_array gets multiple variables and filters him through the same or different filters * *///$i = 123;//if (Filter_var ($i, Filter_validate_int)) {//echo "is an integer";//}/* * MYSQL operation * Connection: mysql_connect ("", "", ""); * Execution: mysql_query ("", $link); * *///$link = mysql_connect ("localhost", "root", "root");//if ($link) {//if (mysql_query ("CREATE datebase my_db", $link)) {//echo "my_db was created";//}else{//echo "Create failed";//Echo mysql_error ();// }//}else {//Echo ' link failed ';//}/ * Object -oriented * 1. Construction method * __CONSTRUCT () constructs methods and class names in PhP4 all the time, PHP5 uses __construct, and when there is no __construct method, the default is to find the same method as the class name * constructor defaults to Public * * / class person {//var $name;//default to public//var $age;var$name;var$age; Public function__construct($name,$age) {Echo"is constructing $name
";$this->name =$name;$this->age =$age; } Public function__set($name,$value){Echo"Calling the __set method
";$this-$name=$value; } Public function__get($name){Echo"Calling the __get method
";if(isset($this-$name)){return$this-$name; }Else{returnNULL; } } Public functionsay(){Echo$this->name."In speaking his age is:".$this->age."
"; } Public function__destruct(){Echo"Goodbye $this->name."."
"; }}//$p 1 = new Person ("Zhang San");//$p 2 = new Person ("John Doe", +);//$p 3 = new Person ("Harry", +);//$p 2->name = "Xue Xinghai";//echo $p 1->name. "
";//$p 2->say ();//class Student extends person{//var $college;//Public function __construct ($name, $age, $college) {//Parent::__construct ($name, $age);//$this->college = $college;// }//Public function say () {////Person::say ();//Parent::say ();//echo "His school is:". $this->college, "
";// }//}//$stu = new Student ("Zhang San", 23, "Tsinghua University");//echo $stu->say (); classMyClass { Public$public="Public";protected$protected=' Protected ';Private$private=' Private '; Public functionprinthello(){Echo$this- Public,"
";Echo$this-protected,"
";Echo$this-Private,"
"; }Private functionprivated(){}} classSubextendsMyClass{ Public functionsay(){Parent::$this->privated (); }}//$s = new Sub ();//echo $s->public;//echo $s->say ();/* Final is used only to define classes and methods and cannot be used to define member properties *///The class using final decoration cannot be inherited classA{ PublicFinal functionsay(){}}//Use final decoration method cannot cover class//static static member methods and static member properties classST {Static$a=3;var$msg;ConstCON =34324; Public function__construct($msg) {$this->msg =$msg; } Public functionsay(){Echo$this->msg; } Public function__tostring() {RETURN' ST '; } Public function__clone() {Echo"Cloning ...";$this->msg ="I'm a fake."; }}//__call Call __call when calling a non-existent way of a class//class Call {//Public Function __call ($name, $arg) {//Echo $name, "method does not exist", "parameter is:", Print_r ($arg);// }//}//$p = new Call ();//$p->h ("hah", 3);/* Abstract function func (); * No {}, followed by parentheses * As long as a method in a class is declared abstract, this class becomes an abstract class and must be decorated with abatract, and abstract classes can contain non-abstract methods. But at least one method is abstract. * Abstract classes cannot be instantiated, only subclasses can be inherited, subclasses must implement all the abstract methods of the parent class before they can be instantiated, otherwise the subclass is still abstract class * */Abstract classparents {ConstCONT =3; PublicAbstract functionfunc1();Abstract functionfunc2(); Public functionfunc3(){Echo"Parent"; }} classSubsextendsparents{ functionfunc1(){} functionfunc2(){}}//$s = new Subs ();//$s->func3 ();/* Interface * interface is a special kind of abstract class, abstract class is a special class, so, the interface is a special class, if all the methods of an abstract class are abstract methods, then this class is an interface * interface cannot declare variables, you can declare a constant * interface declaration of the keyword is interface , not class * / interfaceJK{ConstJJ =244; functionfunc1();//default to public abstract functionfunc2();} classJimplementsJK{ functionfunc1(){} functionfunc2(){}}$t=NewJ ();$str=serialize ($t); Unserialize ($str); classPerson1{//The following is a member attribute of a personvar$name;//person's namevar$sex;//Sex of personvar$age;//person's age//Define a constructor method parameter to assign a property name $name, gender $sex, and age $age function__construct($name = "", $sex = "", $age = "") {$this->name =$name;$this->sex =$sex;$this->age =$age; }//This person can speak in a way that speaks his own attributes functionsay() {Echo"My name is called:".$this->name."Gender:".$this->sex."My Age is:".$this->age."
"; }//Specify serialization to serialize the $name and $age values in the returned array, ignoring attributes that are not in the array $sex function__sleep() {$arr=Array("Name","Age");//At this time, the property $sex will be deleted!!! return($arr); }//Regenerate the object and re-assign the value $age to function__wakeup() {$this->age = +; }}$p 1=NewPerson1 ("Zhang San","Male", -);//serialization of an object , returning a string, calling the __sleep () method, ignoring the attribute not in the array $sex$p 1_string= Serialize ($p 1);Echo$p 1_string."
";//serialization of strings we don't usually parse$p 2= Unserialize ($p 1_string);//crossdress to form objects $p 2 re-assignment $age$p 2->say (); function__autoload($classname){require$classname.'. php ';}NewDM ();

The above introduces PHP learning 00, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.