Quick fix (1)-PHP: Overview, constants, variables, operators, expressions, control statements

Source: Internet
Author: User
Tags try catch

[SOURCE DOWNLOAD]


Quick fix (1)-PHP: Overview, constants, variables, operators, expressions, control statements



Webabcd


Introduced
Quick and fast PHP

    • Overview
    • Constant
    • Variable
    • Operator
    • An expression
    • Control statements



Example
1. Overview
basic/summary.php

<!--    PHP overview --><?  PHPecho "I am between PHP start tag and end tag"; Echo "<br/>"; if (time ()% 2 = = 0):?>     I am HTML (used to demonstrate PHP and HTML    mixed)else:?>     here is HTML (used to demonstrate PHP and HTML mixed    )endif;? ><? PHP Echo "Can not have PHP end tag"; Echo "<br/>";


2. Constants
basic/constant.php

<?PHP/** * Constant*///use define to define constantsDefine("MyConst1", "MyConst1");EchoMyConst1;Echo"<br/>";//Const is supported for defining constants after PHP 5.3.0ConstMyConst2 = "MyConst2";EchoMyConst2;Echo"<br/>";/** Magic Constants (Magic constant) * Magic constants, different values in different scenarios*///__line__-The current line number in the file, determined by its row in the scriptEcho __line__;Echo"<br/>";//__file__-The full path and file name of the current file, and if used in the included file, returns the included filenameEcho __file__;Echo"<br/>";//__DIR__-The directory where the file resides, and if used in the included file, returns the directory where the included file is locatedEcho__dir__;Echo"<br/>";//__function__-function name//__CLASS__-class name//__method__-method name//__namespace__-namespace name//__trait__-TRAIT name


3. Variables
basic/variable.php

<?PHP/** * Variable*///defining variables by $$a= "a";//by using a variableEcho $a;//output:aEcho"<br/>";//by using a variable, the variable is also used even if you add double quotes outsideEcho"$a";//output:aEcho"<br/>";//Using {} to define the variable name, such as "$ab", PHP uses the variable $ab, and in fact we want to use a $ A, at this point we can define the variable name by {}, as follows:Echo"{$a}b ";//Output:abEcho"<br/>";//add double quotes outside, but you don't want to use a variable, you can change the "$" to a string by using the "\" character .Echo"\ $a";//output: $aEcho"<br/>";//If you put a single quote outside, it's treated as a string.Echo' $a ';//output: $aEcho"<br/>";//assigning values between variables$b=$a;//define references by &. The following example is for $c reference $a$c= &$a;//re-assigning a value to a $a$a= "AA";Echo"$b";//output:aEcho"<br/>";Echo"$c";//Output:aaEcho"<br/>";functionF1 () {//The $a here refers to the $a of the local variable, but there is no $a defined in the local scope of the F1, so you have to use $a it will not have any content    Echo $a;//Output:    Echo"<br/>";} F1 ();functionF2 () {//The global keyword is used to mark the $a, and after this local scope, the $a will be used when the world version $a    Global $a; Echo $a;//Output:aa    Echo"<br/>";} F2 ();functionF3 () {//use the global version of the specified variable directly by $GLOBALS    Echo $GLOBALS[' A '];//Output:aa    Echo"<br/>";} F3 ();functionf4 () {//static variables are defined through static, and their resident memory    Static $i= 0; Echo $i; Echo"<br/>"; $i++;} F4 (); //output:0F4 ();//output:1F4 ();//Output:2$x= "AAA";//variable variables (mutable variable), the value of the variable as the variable name//the following sentence is equivalent to $aaa = "BBB"; (because the value of $x is AAA, $ $x equals $aaa)$$x= "BBB";Echo $aaa;//output:bbbEcho"<br/>";Echo$$x;//output:bbbEcho"<br/>";$s= "12345";//the effect of curly braces: used to indicate a single character in a string variable (subscript starting from 0)$s{1} = ' x ';Echo $s;//output:1x345Echo"<br/>";//the role of curly braces: used to define the name of the variable (for example, the variables used below are $s, not $ss)Echo"{$s}s ";//output:1x345sEcho"<br/>";


4, operators, expressions, control statements
basic/statement.php

<?PHP/** * operator, expression, control statement*///operators, expressions, control statements, and other languages are almost//below write something that the individual thinks needs to be emphasized//. String Operators$s 1= "Hello";$s 2= "WEBABCD";$s 3=$s 1.$s 2;Echo $s 3;//Output:hello WEBABCDEcho"<br/>";$s 1. = "Wanglei";//Output:hello WangleiEcho $s 1;Echo"<br/>";//the difference between = = and = = =$a 1= "1";//string$b 1= 1;//Integral type$c 1= "1";//stringif($a 1==$b 1)//two different types of data do = = comparison, the system will automatically convert{    Echo' $a 1 = = $b 1 ';//output: $a 1 = = $b 1    Echo"<br/>";}if($a 1===$b 1)//two different types of data do = = = comparison, the system does not automatically convert{    //not established}if($a 1===$c 1){    Echo' $a 1 = = = $c 1 ';//output: $a 1 = = = $c 1    Echo"<br/>";}//The ElseIf and else if are the same for If/else statements with the {} mode or the {} method omittedif(1 = = 2)    Echo"1 = = 2";Else if(1 = = 1)    Echo"1 = = 1";ElseIf(2 = = 2)    Echo"2 = = 2";Else    Echo"Else";Echo"<br/>";//for: The If/else statement of the way, only ElseIf can be used instead of the else ifif(1 = = 2):Echo"1 = = 2";//else if (1 = = 1)://echo "1 = = 1";ElseIf(2 = = 2):Echo"2 = = 2";Else:Echo"Else";endif;Echo"<br/>";//usage of the foreach statement$array 1=Array(1, 2, 3, 4);foreach($array 1  as $value){    Echo $value; Echo"<br/>";}foreach($array 1  as $key=$value){    Echo $key. " - " .$value; Echo"<br/>";}//use of Try Catch finallyTry{    Echo"Try"; Echo"<br/>"; Throw New Exception("error message", 999);//If you want to write a custom Exception, then inherit Exception .}Catch(Exception $ex){    Echo"Catch"; Echo"<br/>"; Echo sprintf("Message:%s, code:%d, File:%s, line:%d, trace:%s",$ex->getmessage (),$ex->getcode (),$ex->getfile (),$ex->getline (),$ex-gettraceasstring ()); Echo"<br/>";} finally{Echo"Finally"; Echo"<br/>";}



Ok
[SOURCE DOWNLOAD]

Quick fix (1)-PHP: Overview, constants, variables, operators, expressions, control statements

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.