PHP Lesson 3: Basic Data Types of PHP, and php Data Types

Source: Internet
Author: User
Tags constant definition

PHP Lesson 3: Basic Data Types of PHP, and php Data Types

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
<?php$hello = "Hello";echo $hello."  World";?>




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
<?php    $arr=array("hello","world","junzaivip",88,true);echo "<pre>";print_r ($arr);echo "</pre>";?>



Array value:
<?php    $arr=array("hello","world","junzaivip",88,true); echo $arr[1];?>



Add an array:
<?php    $arr=array("hello","world","junzaivip",88,true);    $arr[] = "d";    print_r($arr);?>



Object Type
// An object consists of features and functions, attributes, and methods.
<? Phpheader ("content-type: text/html; charset = gbk"); class per {function say () {echo "I'm talking";} function eat () {echo "I'm eating" ;}function sleep () {echo "I'm sleeping" ;}function run () {echo "I'm walking ";}} $ user1 = new per (); $ user1-> say ();?>

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

<? Php // header ("content-type: text/html; charset = UTF-8"); $ conn = mysql_connect ("localhost", "root", "1234 "); mysql_select_db ("test"); // select the database mysql_query ("set names utf8"); // set the database encoding to utf8 $ SQL = "select * from user "; // SQL statement // Execute SQL statement $ rst = mysql_query ($ SQL); // extract data while ($ row = mysql_fetch_assoc ($ rst )) {echo "




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: <? Php
$ Conn = mysql_connect ("localhost", "root", "1234 ");
Var_dump (is_resource ($ conn ));
?>

4. Automatic type conversion
1. integer-> string
<?php   $num=1243;    echo $num."abd";?>


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)
<? Php // $ str = 'hello'; // $ str2 = 'World'; // echo $ str. $ str2; $ name = 'lei shun'; echo "I am {$ name}, I want to sleep for a while"; echo 'I am '. $ name. ', I want to sleep for a while'; echo "I am ". $ name. ", I want to go to bed for a while"; // if there is no variable in the string, single quotation marks are used. If there is a variable, double quotation marks are used (single quotation marks can also be used, but must be used. )?>



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



What are the main data types of PHP?

PHP supports eight basic data types.
Four scalar types:
Boolean (boolean)
Integer)
Float (float, also called double)
String (string)
Two composite types:
Array)
Object)
There are two special types:
Resource)
NULL)

Php Data Type?

PHP is a weak language, and variables are of no type and are automatically processed by the compiling environment.
To avoid errors, we recommend that you initialize the variables.
For example, $ var = ''; is the character $ var = 0; is the number $ var = array () is the array

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.