Diligent (1) PHP-data types, variables, constants, functions, classes, interfaces, attributes

Source: Internet
Author: User
Tags scalar

Tag: The His function cannot start with the object source contains LSH integer

1. Data type
PHP has eight basic data types, including:
Four scalar types: Boolean (Boolean), Integer (int), float (floating-point), String (string)
Two composite types: Array (array), object
Two special types: resource (Resource), NULL
To know that a variable belongs to a type, you can use the GetType () function, for example:

<?php$a = 100;echo gettype($a);  //integer

2. Variables
The variables defined in PHP begin with $, the first letter of the variable name must be a letter (A-Z-a-to-a-a-a-to), an underscore (_), and cannot use PHP's predefined keywords (if, else, switch, array, function, class, interface, etc.), for example:

<?php$a = 10;

The variables contain the following variables:
Common variables, example: $a = 10;
Static variables: Variables identified with the static keyword must be defined inside the function. After the function executes, the value of the variable is not lost and can be used for recursive invocation
Super global variable: contains $_get, $_post, $_request, $_files and so on, detailed introduction see PHP official website: http://www.php.net/manual/zh/language.variables.superglobals.php

3. Constants
Use the Define () function to define a constant, noting that the value of a constant can only be defined as a scalar, for example:

<?phpdefine(‘API_VERSION‘, 3.1.2);   //定义API_VERSION常量

4. Functions

    • Common functions, such as:
<?php//定义一个函数,函数名为test,有两个参数$a,$bfunction test($a, $b){    return $a + $b;}
    • anonymous function: In fact, there is no Name function, the function can be assigned to the variable. For example:
<?php$func = function ($name) {    return sprintf(‘hello %s‘, $name);};//使用一个变量作为函数名,然后向函数传入参数echo $func(‘yunshu‘); //hello yunshu
    • callback function
      Many PHP functions use callback functions, such as Array_map () and Preg_replace_callback (). For example:
<?phpfunction incrementNumber($number) { return $number+1;}//这里的第一个参数要求是一个回调函数,第二个参数是个数组。函数实现了//对数组的每个元素加一的操作$res = array_map(‘incrementNumber‘, [1,2,3]);//第一个参数也可以是一个匿名函数,例:function ($number) {return $num+1}var_dump($res); //结果数组元素变为2,3,4

5. Classes, interfaces
The use of classes and interfaces can make your code more modular.
Here is an example of using classes, abstract classes, interfaces:

<?phpDefine a class of methods using interfaces. Member variables (containing class static variables) cannot be defined in an interfaceInterfaceishop {PublicfunctionBuy($gid);PublicfunctionSell($gid);PublicfunctionView($gid); }Abstract classes use an abstract identity, which abstracts out common methods of shop, and subclasses can override methods.To implement an interface, use the Implements keyword.AbstractClassBaseshopImplementsishop {PublicfunctionBuy($gid) {Echo' You have purchased an ID of: '. $gid.' Goods '); }PublicfunctionSell($gid) {Echo' You sold the ID for: '. $gid.' Goods '); }PublicfunctionView($gid) {Echo' You looked at the ID: '. $gid.//inherits the parent class, using the extends keyword class ballshop extends BaseShop {protected $itme _id = null; public function __construct () { $this->itme_id = 2314; } public function open () { $this->sell (  $this->itme_id); } } 

6. Traits
Character (trait), is the concept introduced by PHP5.4.0. Trait like class and interface, the modular implementation method is injected into several unrelated classes, which facilitates the reuse of code.

<?phpTrait Hello {PublicfunctionSayHello() {Echo' Hello '; }}Trait World {Publicfunction sayworld () {echo class myhelloworld {//uses trait: the properties and methods of the trait are imported, just like the class itself has this property and method. use hello, World; public function  Sayexclamationmark () {echo  '! ';}} $obj = new Myhelloworld (); $obj->sayhello (); $obj->sayworld (); $obj Sayexclamationmark (); //hello world!            

Today, we'll talk about it tomorrow. namespaces, auto-loading, composer, database operations, error exceptions ...

Welcome to the public number: Yun-Shu's self-media ~ ~ ~

Diligent (1) PHP-data types, variables, constants, functions, classes, interfaces, attributes

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.