PHP: PHP scripts, types, and variables

Source: Internet
Author: User

PHP is a language designed for website background development. More and more object-oriented content is added after the new version 5.3. I am gradually optimistic about this language, I have recently learned some things and recorded some key points for future review.

The document content of PHP is divided into three parts: Basic Language introduction, library function description, and feature description. The basic language is the basic part. It introduces the basic syntax, type, variable, operator, process control, function, class, and namespace of PHP. The Library Function Description section mainly introduces the related PHP Extension function library content. The feature section describes related web development content such as requests, sessions, and database connections. The first part is the main content of this series of articles.

On the whole, PHP has many similarities with C or Java in terms of syntax and semantics, but there are only a few differences.

This article first records the basic usage, variables, and types of PHP.

1. How to run a PHP script

<? Phpecho "Hello World"; // output Hello world?>

Search for the PHP interpreter <? PHP?> Mark to explain the start and end of the corresponding PHP script. The PHP interpreter will directly output the content that is not marked until the next start mark is encountered. If the entire file belongs to a single start and end tag, the end tag at the end of the file can be omitted.

The above script can be executed on the command line, root @ localhost # PHP hello. php. We can see the output.

The comments in PHP are the same as those in C. Single-line comments/And multi-line comments/**/are supported /**/. In addition, Unix shell-style annotations and # annotations are supported.

2 types supported by PHP

PHP supports eight built-in types: bool int float String Array object resource callable null

There are also three pseudo types: Mixed number callback

The first four built-in types do not need to be described. array supports linear and Hash Storage and use. When using arrays, they can be stored and accessed directly by location.

The object type is an instance of the class generated through new. Resource is the handle of the external resources stored in PHP. You can use get_resource_type to view the corresponding resource type. Callable can be interpreted as a function.

The three pseudo types are only used to describe the function logarithm in the document. Mixed indicates that the type is not persistent, number indicates int, float, and callback indicates the callback function type.

For different types of PHP, is_int, is_string, and Other types are defined. get_type () and set_type () are used to obtain the variable type.

Different types can be converted. There are two conversion methods: Operator and function. Type $ A = (INT) 132.3 or $ A = intval (132.3)

  • (INT), (integer)-cast
    Integer
  • (Bool), (Boolean)-cast
    Boolean
  • (Float), (double), (real)-cast
    Float
  • (String)-cast
    String
  • (Array)-cast to array
  • (Object)-cast
    Object
  • (Unset)-cast to null (PHP 5)

3. php Variables

3.1 variable usage

You do not need to declare or specify the type before the definition. The variable starts with the $ symbol. Variable naming rules are similar to C. The variable name is case sensitive.

3.2 variable role type

PHP has four scopes: Global, local, static, and parameter.

Variables in the global scope are not declared in the task function body. They are non-static variables.

Local variables are declared in the function body. The parameters are declared in the function declaration.

If the variable is declared before the include or require statement, the variable is visible in the contained file.

It is worth noting that the global scope of PHP is different from that of C. The global scope of PHP cannot be applied to local scopes. If global variables need to be referenced locally, global variables must be declared in a local scope. As shown below, the global variable $ A cannot be accessed in the test () function. Use the global $ a statement if you want to use it.

<?php$a = 1; /* global scope */ function test(){     echo $a; /* reference to local scope variable */ } test();?>

 

3.3 external variables

Variables submitted from HTML can be accessed through Super global variables.

3.4 super global variables

PHP defines several super global variables for convenient web development. As follows:

  • $ Globals
  • $ _ Server: Describes the configuration information of web servers and clients.
  • $ _ Get: GET request data of the Client
  • $ _ Post: client's POST request data
  • $ _ FILES: the client uploads file data.
  • $ _ COOKIE: client cookie data
  • $ _ Session: client session data
  • $ _ Request: alias of get/post
  • $ _ Env: Environment Information

4 Constants

4.1 global Constants

Define ("Pi", 3.1415926 );

4.2 Constants

class Test{    const MATH_PI = 3.1415926;    //...}

4.3 magic Constants

Name Description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file. If used inside an include, the name of the specified ded file is returned. Since PHP 4.0.2,__FILE__Always contains an absolute path with symlinks resolved whereas in older versions it contained
Relative Path under some circumstances.
__DIR__ The Directory of the file. If used inside an include, the Directory of the specified ded file is returned. This is equivalentDirname (_ file __). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0 .)
__FUNCTION__ The function name. (Added in PHP 4.3.0) as of PHP 5 this constant returns the function name as it was declared (case-sensitive). in PHP 4 its value is always lowercased.
__CLASS__ The class name. (Added in PHP 4.3.0) as of PHP 5 this constant returns the class name as it was declared (case-sensitive ). in PHP 4 its value is always lowercased. the class name provided des The namespace it was declared in (e.g.Foo \ bar). Note that
As of PHP 5.4 _ class _ works also in traits. When used in a trait method, _ class _ is the name of the class the trait is used in.
__TRAIT__ The trait name. (Added in PHP 5.4.0) as of PHP 5.4 This constant returns the trait as it was declared (case-sensitive ). the trait name provided des The namespace it was declared in (e.g.Foo \ bar).
__METHOD__ The class method name. (Added in PHP 5.0.0) the method name is returned as it was declared (case-sensitive ).
__NAMESPACE__ The name of the current namespace (case-sensitive). This constant is defined in compile-time (added in PHP 5.3.0 ).

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.