PHP: Conversions between data types and declaration and use of constants

Source: Internet
Author: User
Tags bool constant scalar variable scope

PHP is a weak type of language

PHP has 8 types of Chinese
4 scalar//Only one value can be stored
1. Integral type: int integer
2. Boolean: BOOL Boolean
3. Floating-point type: float,double,real
4. Character type: string
2 compound types//A variable can store multiple values
1. Arrays: array//Only multiple variables can be stored
2. Objects: Object//stored variables and functions
Special type in 2
1. Resource type: Resource
2. NULL type: null

Example: var_dump (variable or value); That is, you can view the types of variables or values, and you can look at the data


Code:

<?php
Output integer type
$var = 10;

echo "<pre>"; <pre> tags are exported as code prototypes
Var_dump ($var);//output result: Int (10), meaning integer, value 10
echo "</pre>";

echo "----------------------<br>";

Output floating-point type
$var = 11.11;

echo "<pre>";
Var_dump ($var);//output Result: float (11.11), meaning is a floating-point type with a value of 11.11
echo "</pre>";

echo "----------------------<br>";

Output Boolean
$var =true;

echo "<pre>";
Var_dump ($var);//Output Result: BOOL (true), meaning Boolean, value true
echo "</pre>";

echo "----------------------<br>";
Output character type
$var = "MK";

echo "<pre>";
Var_dump ($var);//Output Result: string (2) "MK", meaning is character type, value is MK
echo "</pre>";

echo "----------------------<br>";
Output array
$var =array (1,2,3);

echo "<pre>";
Var_dump ($var);
echo "</pre>";

echo "----------------------<br>";
?>

Conversion of data types


One is the cast:

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 ();

Code:

$str = "100.1254ABC";
SetType ($str, int);
Var_dump ($STR);

2. Use the form of (type) before assigning a value that does not change the type of the original variable ($a = (int) $str;)

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);

Code:

$str = "100.1254ABC";
$a =intval ($STR);
Var_dump ($a);

Note: An integral type occupies 4 bytes in memory
Floating-point type occupies 8 bytes in memory

One is the automatic conversion (this is the most common way PHP, because we do not need to manage the type of development, the variable will be automatically converted according to the operating environment)

Code:

$a = 10;
$b =true;
$c = "100ABC";
$d = 12.14;

$sum = $a + $b + $c + $d;
Var_dump ($sum);

Some common functions related to variables and types

        isset (); //is to determine that the variable does not exist, the value if NULL, also indicates NULL
           code:
         $a = "";
       if (isset ($a)) {
            echo "exists";
     }else{
             echo "does not exist";
     }

        Empty (); //Determine if a variable is empty, such as ""    null
            code:
         $a = "";
      if (empty ($a)) {
              echo "null";
     }else{
              echo "No null";
     }

        unset ();
        SetType ();
        GetType ();
 
  Variable type test function
 
    is_bool ();
    is_int (); Is_intege R (); Is_long ();
    is_string ();
    is_float (); is_double (); Is_real ();
    Is_array ();
    is_object ();
    Is_resource ();
    is_null ();
   
    is_scalar ();
    is_numberic ();
     is_callable ();

  Declaration and use of constants
    1. Constant is a simple identifier
    2. Constant can no longer change his value or use unset () to cancel
    3. Constants can be defined and accessed anywhere in the
    4 without regard to the rules of variable scope. Constants use define ("Constant name", value)
     5. Constant names do not use "$"
    6 in both declaration and use. Constant names are used in uppercase
    7. Values of constants can only be scalar types (Int,float,bool, String)
    8. Constants must be given a value at declaration
    9.defined ("Constants")  //Judgment constants do not exist
    
          Code:
            define ("MK", "Morker");
               Echo MK;
       
   defined ();
          code:
           Define ("Mk2b", "Morker");

             if (defined ("MK")) {
        
             Echo MK;
       
       }else{
        
              Define ("MK", 504815135);
       
       }
         Echo MK;

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.