PHP Learning Note 1

Source: Internet
Author: User
Tags variable scope

1. What is PHP?
    • Hypertext Preprocessor (Hyper-text preprocessing language).

    • is a scripting language.

    • is the most popular web development language.

What can 2.PHP do?
    • Dynamic page content can be generated.

    • You can create, open, read, write, and close files on the server.

    • can phone form data.

    • Cookies can be sent and received. (Data stored on the user's local terminal)

    • You can add, delete, and modify data in a database

    • You can restrict users from accessing some pages.

    • Data can be encrypted

3.PHP Basic Syntax
    • The PHP script starts with <?php and ends with?>.

    • The default extension for PHP files is ". php".

    • PHP uses semicolons to split statements.

    • The variable starts with $, followed by the variable name:

1 $first = "Hello World";
    • Comments in PHP:

A. Single-line Comment:

1 // , #

B. Multiline Comment:

1 /* ... */

4.PHP Variable Scope

The scope of a variable is the part of the script that can be referenced.

    • Local

    • Global

    • Static

    • Parameter

5.Local and global scopes

All variables defined outside the function have global scope. In addition to functions, global variables can be accessed by any part of the script, but to access a global variable in a function requires the use of the global keyword.

1<?PHP2 $x=5;3 $y=10;4  5 functionmyTest ()6 {7     Global $x,$y;8     $y=$x+$y;9 }Ten   One myTest (); A Echo $y;//Output -?>

PHP stores all global variables in an array called $GLOBALS [index]. Index holds the name of the variable. This array can be accessed inside the function, or it can be used to update global variables directly.

1<?PHP2 $x=5;3 $y=10;4  5 functionmyTest ()6 {7     $GLOBALS[' Y ']=$GLOBALS[' X ']+$GLOBALS[' Y '];8 } 9  Ten myTest (); One Echo $y;//Output A?>
6.Static Scopes

When a function is complete, all variables are usually deleted, but the addition of static allows them to be preserved after the operation is completed.

1<?PHP2 functionmyTest ()3 {4     Static $x=0;5     Echo $x;6     $x++;7 }8  9MyTest ();//The variable keeps the previous value each time it is calledTenMyTest ();//the variable is still a local variable OneMyTest ();//Output 012 A?>

7. Parameter Scope

A parameter is a local variable that passes a value to a function by calling code.

Parameters are declared in the parameter list as part of the function declaration:

1 <? PHP 2 function myTest ($x)3{4     echo$x ; 5 }6 myTest (5); 7 ?>

8. Output Mode

Two basic output modes:

Echo: You can output one or more strings. (output speed is fast, no return value)

1 <? PHP 2 Echo "// HTML tags can be added 3 Echo ("How's it Going?");                 // Echo () =echo 4 ?>

Print: Only one string is allowed, and the return value is always 1.

9.PHP Constants

Constants are a simple and worthwhile identifier that cannot be changed in a script.

Set Syntax:

1 Define string $name Mixed $value $case _insensitive false ] )

Three parameters:

    • Name: Required, constant name, and identifier.

    • Value: Required, constant.

    • Case_insensitive: Optional. If set to true, the case is not sensitive and is sensitive by default.

 1  <? php    2  define  ("Alcohol", "Pads"); //  case sensitive             3  echo  Alcohol; //  output pads  4  echo  ' </br> '             5  echo  alcohol; //  output alcohol  6 ? 
1 <? PHP 2 Define ("NIVEA", "protection",true);     // Case insensitive 3 Echo NIVEA; 4 Echo "</br>"; 5 echo Nivea;             // All output Protection 6 ?>

10.PHP collocated operator

PHP has only one collocated operator, "." : (Used to concatenate strings together)

1<?PHP2 Define("NIVEA", "protection");3 4 $txt 1= "Protect Your Skin";5 $txt 2= "From the sunlight!";6 7 EchoNivea. " " . ":" . " " .$txt 1. " " .$txt 2;8 //Nivea:protect your skin from the sunlight!9?>

11.strlen (), Strpos () function

Strlen (): Calculates the string length.

Strpos (): Calculates the first matching position of a string occurrence

1 <?  2echostrpos("Hello world!", "World"3 ?>

12.PHP Increment, decrement operator

13. Comparison Operators

Note: the Var_dump () function can return not only the value, but also the type of the value.

14.Array Operators

15. Ternary Operators
1 (EXPR1)? (EXPR2): (EXPR3)

When EXPR1 evaluates to TRUE, the value is EXPR2, and the value is EXPR3 when the EXPR1 evaluates to FALSE.

From PHP 5.3, the middle part of the ternary operator can be omitted. Expression expr1?: Expr3 returns EXPR1 when EXPR1 evaluates to TRUE, otherwise returns EXPR3.

1<?PHP2 $name= "Annika";3 //General Wording4 $username=isset($name) ?$name: ' No Name ';5  //Php_eol is a line break compatible with different platforms to improve code portability6 Echo $username,Php_eol; 7 8 //the wording after 5.39 $username=$name?: ' No Name ';Ten Echo $username; One?>

More than one NULL merge operator in the php7+ version "??" :

1 <? PHP 2 // if $_get[' user '] does not exist return ' nobody ', return the value of $_get[' user '] 3 $username $_get [' User ']?? ' Nobody '; 4 ?>

16. Operators have precedence, but parentheses can increase code readability
1<?PHP2 //Priority: && > = > and3 //Priority: | | > = > or4  5 $a= 3;6 $b=false;7 $c=$aOr$b;8 Var_dump($c);//the $c here is an int value of 3 instead of a Boolean value of True9 $d=$a||$b;Ten Var_dump($d);//The $d here is the Boolean value true One?>

PHP Learning Note 1

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.