PHP Lesson 3 Basic Data Types of PHP

Source: Internet
Author: User
Tags constant definition

PHP Lesson 3 Basic Data Types of PHP

Learning outline:

1. php seven variable types

2. Differences between isset and empty Functions
3. Type Test
4. Automatic type conversion

5. Forced type conversion


Note:

1. The call method in the object is called through the variable-> method name. $ user1 = new per (); $ user1-> say ();
2. Change the page encoding:
Header ("content-type: text/html; charset = gbk ");
3. the header information cannot be output.



1. php eight variable types
Integer
Floating Point Type
Character Type
Boolean
// The above four types are scalar types

Array
Object
// The above two types are composite types

Resources
Null type
// The above two types are special



Integer
$ A = 10;
Floating Point Type
$ A = 10.3;
Character Type
$ A = "Hello World ";
Use. To link strings
 




Boolean
$ A = true;
Boolean types are generally obtained by comparison operations,> <>==! ===! =
When echo print_r is used to output a boolean type, true is changed to 1, and false is null.
$ A = true;
Var_dump ($ );
Array
// Definition: when multiple values are assigned to a variable
 ";print_r ($arr);echo "
";?>


Array value:
 



Add an array:
 



Object Type
// An object consists of features and functions, attributes, and methods.
 Say ();?>

Resources
Null type
$ A = null;
Database-based linked resources and operation tables:

 




2. Differences between isset and empty Functions
Whether the isset variable exists and does not exist:
1) No definition
2) null

Whether or not the empty variable is empty:
1) 0
2 )""
3) "0"
4) false
5) array ()
6) null
7) No definition
3. Type Test
Var_dump ();
1. Integer is_int ();
2. Float is_float ();


Database connection: $ Conn = mysql_connect ("localhost", "root", "1234 ");
Var_dump (is_resource ($ conn ));
?>

4. Automatic type conversion
1. integer-> string
 


2. String-> integer
$ Num = "1243alj ";
// The string is automatically converted to an integer.
Echo $ num + 1;

3. Other types-> Boolean Type
1) 0
2 )""
3) "0"
4) false
5) array ()
6) null
7) No definition
// The Boolean Type above is false.



5. Forced type conversion
(Int) $ num integer
(Float) $ num floating point type
(String) $ num String
(Bool) $ num Boolean

Use to delete variables:
$ Num = "user ";
Unset ($ num );
Var_dump (isset ($ num ));

Single double quotation marks of a string:
1. If no variable exists in the string, use single quotation marks.
2. If there is a variable, use double quotation marks (single quotation marks can also be used, but must be linked)
 



Constant definition:
// Same as the variable, but cannot be modified once defined
Define ("HOST", "localhost ");
Define ("USER", "root ");
Define ("PASS", "123 ");
Define ("DBNAME", "test ");

// For example, the database configuration file must use constants and cannot be modified later.

Constant output:
Echo HOST;
Constants cannot be placed in double quotation marks.
Echo "my host is". HOST;


Operator


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.