PHP Learning Notes-Getting Started (1)--Grammar & variables

Source: Internet
Author: User
Tags variable scope

Basic PHP syntax

The PHP script can be placed anywhere in the document.

PHP scripts start with <?php and End with ?> ;

PHP files usually contain HTML tags and some php script code.

notes: The PHP statement ends with a semicolon (;). The close tag of a PHP block also automatically indicates a semicolon (so you don't have to use a semicolon in the last line of the PHP block).


PHP supports three types of annotations:
    • Single-line Comment

    • #单行注释

    • /* Multiline Comment */

PHP Case Sensitivity:

in In PHP, all user-defined functions, classes, and keywords (such as if, else, Echo, and so on) are case-sensitive not sensitive

in PHP, all variables are case- sensitive Sensitive .

PHP Variable rules:
    • The variable starts with the $ sign, followed by the name of the variable

    • Variable names must begin with a letter or underscore

    • Variable names cannot start with a number

    • Variable names can contain only letters , numeric characters, and underscores (A-Z, 0-9, and _)

    • Variable names are case sensitive ($y and $Y are two different variables)


Creating PHP Variables

PHP does not have a command to create a variable.

Variables are created the first time they are assigned a value;

notes: If the value you assign to the variable is text, enclose the value in quotation marks.

PHP is a loosely-typed language

In the example above, please note that we do not have to tell the data type of the PHP variable.

PHP automatically converts the variable to the correct data type based on its value.

Similar to the assignment statement in JavaScript: Var *=*.


PHP variable Scope
    • Local (partial)

    • Global (globally)

    • Static (statically)

Local and Global Scopes

Variables declared outside the function have Global scope and can only be accessed outside of the function.

Variables declared inside a function have a local scope and can only be accessed within the function.

Note: java does not support global variables because 1. The transparency of the reference is broken; 2. Namespace collisions are created, global variables only can be in function outside access.

PHP Global Keywords

The global keyword is used to access variables within the function.

To do this, you need to use the Global keyword before (inside the function) variable. ($x = 5; .... function ... () {global $x;} )

PHP also stores all global variables in an array named $GLOBALS [index]. The subscript contains the variable name. This array is also accessible within the function and can be used to update global variables directly.

($x = 5; .... function ... (){

$GLOBALS [' y ']= $GLOBALS [' x ']+ $GLOBALS [' Y '];

})

PHP Static Keywords

When you declare a variable by using static, the variable is not deleted.

This article from "Only wait for a moment" blog, please be sure to keep this source http://divergery.blog.51cto.com/10722563/1745117

PHP Learning Notes-Getting Started (1)--Grammar & variables

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.