PHP Language Basics (i)

Source: Internet
Author: User

First, PHP markup style

1.1xml Style (Common)

<?php echo "XML style";?>

1.2 Scripting Style

1 <script language= "php" >2     echo' scripting Style '; 3 </script>

1.3 Short Style

1 echo' short style ';? >

1.4 ASP Style

1 <%2     echo ' asp style ';3 %>

If you need to use short and ASP style, you need to turn it on in the php.ini file Short_open_tag and Asp_tags set to ON

C:\Windows\php.ini.

Second, PHP comments (three kinds)

2.1 #,/* */,//

Iii. types of data and their usage

3.1 Constant data types

Boolean (bool) string (String) integer (shape) float (float)

3.1.1 BOOL Example

1 <? PHP 2     $boo true ; 3     if ($bootrue) 4         Echo ' Boo is true '; 5     Else 6         Echo ' Boo is false '; 7 ?>        

3.1.2 String Example

1 <? PHP  2     $a = 'hello '; 3 ?> 4 or 5 <?php 6     $a = "Hello"; 7     echo "$a";//output Hello  8     echo '$a';//Output $ A 9     echo "<p>";//output segment marker Ten ?>

Common escape characters, such as the following table

The delimiter (<<<), can be temporarily regardless of, later in another section of the detailed;

3.1.3 Example of integral type

1<?PHP2     $str 1= 123456;//decimal Integer3     $str 2= 0x123456;//hexadecimal integer4     $STR 3= 012345670;//octal integer5     $STR 4= 01234567;//octal integer6     Echo' Decimal str1: '.$str 1.‘ <br> '; <br> simple line breaks that can be inserted7     Echo' Hex str2: '.$str 2.‘ <br> ';8 if($STR 3==$STR 4){9    Echo' $str 3= $str 4 = '.$STR 3;Ten } One Else{ A     Echo' $str 3!= $str 4 '; - }  -?>

3.1.4 Floating-Point type example

1 <? PHP2echo ' pi= '. Pi () .<br>; 3 Echo ' pi= '. 3.1415926. ' <br> '; 4 Echo ' pi= '. 31415926E-7. ' <br> '; 5 ?>

3.2 Composite data types

Array object (object)

3.2.1

Examples of arrays:

1 $array = (' value1 ', ' value2 '); 2 $array [key] = ' Vlaue '; 3 $array Array (key1>=value1,key2>=value2);

Examples of 3.2.2 Objects-------14 Chapters

3.3 Special data types

3.3.1 Resource Null value

A resource is a special variable, also called a handle, that is saved to a reference to an external resource. Resources are created and used through specialized functions.

A null value means that there is no value, and the only value is NULL, not case-sensitive.

Example of a null value

1<? PHP2 $string 1=NULL;3 $string 3= "Str";4 if(Is_null($string 1))5     Echo"Null<br>";6 unset($string 3);7 if(Is_null($string 3))8     Echo"String3 = null<br>";9?>

3.3.2 Conversion Data type

Type conversions can also be implemented through function settype (mixed var,string type). Examples are as follows:

1 <? PHP 2 $num = ' 3.14 '; 3 Echo (integer) $num ; 4 Echo Settype ($num, ' Integer '); 5 ?>

3.3.3 Detection Data type:

3.4php Constants

Constants are the amount of constant values. Constants are defined and cannot be changed anywhere else in the script, constants are usually defined by the function define (). The function prototypes are as follows:

Define (string constant_name,mixed value,case_sensitive=true)

Constant_name: Constant Name Value: Constant case_sensitive: Whether it is case-sensitive

-----------------------------------------------------------------------

Mixed constant (string const_name)//Gets the constant value, returns the value of the constant

bool Defined (string constant_name)//Determine if the constant is defined

Example:

1 <? PHP 2 Define ("MESSAGE", "HELLO"); 3 Echo MESSAGE. " <br> "; 4 $name = "Count"; 5 Echo constant ($name). " <br> "; 6 Echo (define("MESSAGE")). " <br> "; 7 ?>

3.4.2 Pre-defined constants

Example:

1 <? PHP2echo "current path:". __file__ ; 3 Echo "Current number of rows:". __line__ ; 4 ?>

3.5PHP variables

Global variables and static variables

Static variables: Static variables are initialized only once, and then the assignment operation is not valid, only the other than the assignment of the operation is valid, usually used to count the number of chat rooms online;

Examples of static variables:

1<? PHP2 functionfunc1 () {3      $message=0;4     $message+=1;5     Echo $message." ";}6 functionFunc2 () {7     $message=0;8     $message+=1;9     Echo $message." ";}Ten  for($i= 0;$i<10;$i+ +) func1 ();//Output 1-10 One Echo"<p>"; A  for($i= 0;$i<10;$i+ +) Func2 ();//output 10 x 1 - Echo"<p>"; -?>

3.5.2 Variable variable

Allows variable names to be dynamically changed

Example:

1 <? PHP 2 $change _name = "trans"; 3 $trans = "can see me!" ; 4 Echo $change _name . ' <br>'; 5 Echo $$change _name; 6 ?>

3.5.3php pre-defined variables

3.6php operator

3.6.1 Arithmetic operators

Example:

1 <? PHP 2 $a = -100; 3 $b =; 4 Echo "\ $a +\ $b =". ($a+$b). " <br> ";
?>

3.6.2 string operator

Example

<? PHP $n = "3.14"; $m = 1; $mn $n+$m; Echo $mn . " <br> ";? >

3.6.3 assignment operator

3.6.4-bit operators

3.6.5 logical operators

3.6.6 comparison Operators

3.6.7 Error control operator

  <?php
$err = 5/0; The error message is output: Warning:division by zero in ... $err = @5/0 error still in, not output
?>

3.6.83-dollar operator

1 <? PHP 2 $value =100; 3 Echo ($value= =true)? Ternary operation: No value:// output ternary operation 4 ?>


3.8php function

3.8.1 Defining and calling functions

Example:

1 <? PHP 2 function Example ($num) {3     return .  $num*$num; 4 }5echo example (ten); 6 ?>

3.8.2 parameter passing

To pass an example by value:

1<?PHP2 functionEample ($m){3     $m=$m*5+10;4 Echo $m;5 }6 $m= 1;7Example$m);8 Echo $m;9?>

Passing examples by reference

1 <? PHP function eample (&$m) {3     $m$m*5+10; Echo $m ; 5 }$m = 1; 7 Example ($m); Echo $m ; 9?>

PHP Language Basics (i)

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.