PHP: Declaration of conversion between data types and constants and usage

Source: Internet
Author: User
Php variables are a weak type. we can perform fast conversion after each data type. The following describes the examples of php variables and constants, I hope these examples will help you learn php. PHP is a weak language. in PHP, there are four scalar types in 8... php variables are a weak type. we can perform fast conversion after each data type. The following describes the examples of php variables and constants, I hope these examples will help you learn php.

PHP is a weak language

PHP has a total of 8 types

Four scalar// Only one value can be stored)

1. integer: int integer

2. boolean: bool boolean

3. float: float, double, real

4. Character Type: string

Two composite types// One variable can store multiple values

1. array: array // only multiple variables can be stored

2. object: object // saved variables and functions

Two special types

1. resource type: resource

2. null type: null

Example: Var_dump (variable or value); // You can view the type and data of the variable or value.

PHP instance code:

 ";//
The label outputs var_dump ($ var) based on the original code. // The output result is int (10), which indicates the integer type and the value is 10 echo"
"; Echo "----------------------
"; // Output floating point type $ var = 11.11; echo"
"; Var_dump ($ var); // output result: float (11.11), indicating float, with a value of 11.11 echo"
"; Echo "----------------------
"; // Output Boolean $ var = true; echo"
"; Var_dump ($ var); // output result: bool (true), meaning Boolean type, value: true echo"
"; Echo "----------------------
"; // Output struct type $ var =" mk "; echo"
"; Var_dump ($ var); // output result: string (2)" mk ", which indicates bytes type and the value is mk echo"
"; Echo "----------------------
"; // Output array $ var = array (1, 2, 3); echo"
"; var_dump($var); echo "
"; Echo "----------------------
";?>

Data type conversion

One is forced conversion:

1. setType (variable, type); // type int, integer, float, double, real, bool, boolena, string, array, object

This function changes the type of the original variable, var_dump ();

PHP instance code:

$str="100.1254abc"; setType($str,int); var_dump($str);

2. use the (type) format before the value assignment. the type of the original variable is not changed * $ a = (int) $ str ;)

PHP instance code:

$str="100.1254abc"; $a=(int)$str; var_dump($a); var_dump($str);

3. $ variable = intval (variable or value );

$ Variable = float (variable or value );

$ Variable = stringval (variable or value );

PHP instance code:

$str="100.1254abc"; $a=intval($str); var_dump($a);

Note: integer occupies 4 bytes in memory and floating point occupies 8 bytes in memory.

One is automatic conversion (this is the most common method in PHP, because we do not need to manage the type during development, and variables will be automatically converted according to the running environment)

PHP instance code:

$a=10; $b=true; $c="100abc"; $d=12.14;$sum=$a+$b+$c+$d; var_dump($sum);

Common functions related to variables and types:

Isset ();// Judge whether the variable does not exist. if the value is null, it also indicates null. the instance code is as follows:

$ A = ""; if (isset ($ a) {echo "exists" ;}else {echo "does not exist" ;}empty (); // determine whether a variable is null, such as "null". The instance code is as follows: $ a = "";

Empty ();// Determine whether a variable is null, such as "null". The instance code is as follows:

$a=""; (emptyempty($a)){  echo "null"; lse{  echo "No null";   unset(); setType(); getType();

Variable type test function:

is_bool(); is_int(); is_integer(); is_long(); is_string(); is_float(); is_double(); is_real(); is_array(); is_object(); is_resource(); is_null();  is_scalar(); is_numberic(); is_callable();

Constant declaration and use

1. a constant is a simple identifier.

2. after a constant is set, its value cannot be changed or unset () can be used to cancel the operation.

3. constants can be defined and accessed anywhere, regardless of the variable range rules.

4. Use define ("constant name", value) for constants)

5. the constant name is not used in both declaration and use "$"

6. constant names are usually capitalized.

7. the constant value can only be of the scalar type (int, float, bool, string)

8. a constant must be given a value during declaration.

9. defined ("constant") // determines whether the constant does not exist.

The PHP instance code is as follows:

 


Article link:

Save this article for favorites!

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.