Section 4 PHP getting started

Source: Internet
Author: User
Tags bitwise operators
PHP entry 4.1 data type PHP supports integers, floating-point numbers, strings, arrays, and objects. The variable type is usually determined by the PHP running process rather than the programmer (it is a good relief !). Of course, if you like, you can also use cast or settype () to convert a type of variable to a specified type. The value type can be an integer. PHP Getting started

4.1Data type

  PHPSupports integers, floating-point numbers, strings, arrays, and objects. Variable types are usually determined by programmers.PHPThe running process is decided (it is really a good relief !). You can also useCastOr functionSettype ()Converts a type of variable to a specified type.

Value

The value type can be an integer or a floating point number. You can use the following statement to assign a value to a value:
$ A = 1234 ;#Decimal number
$ A =-123 ;#Negative
$ A = 0123 ;#Octal(Equal to the decimal number83)
$ A = 0x12 ;#Hexadecimal number(Equal to the decimal number18)
$ A = 1.234 ;#Floating point number"Double precision"
$ A = 1.2e3 ;#Exponential form of double precision

String

A string can be defined by single or double quotation marks. Note that the strings enclosed by single quotes are literally defined, while strings enclosed by double quotes can be extended. In addition, you can use a backslash () to add escape sequences and conversion characters to double quotation marks. Example:

$ First = 'hello ';
$ Second = "World ";
$ Full1 = "$ first $ second ";#GenerateHello World
$ Full2 = '$ first $ second ';#Generate$ First $ second
$ Full3 = "01DC studio,." 2000 copyright ."";

Pay attention to the last line. if you need to use double quotation marks in the string, you can use the backslash character, as shown in the statement of this line. The backslash here is used to change the function of double quotation marks. You can connect characters and numbers with operator numbers. The character is converted to a number to use its original position. InPHPDetailed examples are provided in the manual.

Array and hash table

Arrays and hash tables are supported in the same way. How to use it depends on how you define them. You can useList ()OrArray ()Or assign values to the array directly. Array index from0Start. Although I have not explained it here, you can easily use multi-dimensional arrays.

//An array containing two elements
$ A [0] = "first ";
$ A [1] = "second ";
$ A [] = "third ";//Simple method for adding array elements
//Now$ A [2]Assigned"Third"
Echo count ($ );//Print out3Because the array has3Elements
//Define an array with a statement and assign values
$ Myphonebook = array (
"Sbabu" => "5348 ",
"Keith" => "4829 ",
"Carole" = & gt; "4533"
);
//Oh, forget the instructor. let's add an element.
$ Myphonebook ["dean"] = "5397 ";
//You definedCaraleElement Error. let's correct it.
$ Myphonebook ["carole"] => "4522"
//I have not told you how to use the similar support method of array? Let's take a look
Echo "$ myphonebook [0]"; // sbabu
Echo "$ myphonebook [1]"; // 5348

Other functions that are useful for arrays or hash tables includeSort (),Next (),Prev ()AndEach ().

Object

UseNewStatement to generate an object:
Class foo
{
Function do_foo ()
{
Echo "Doing foo .";
}
}
$ Bar = new foo;
$ Bar-> do_foo ();

Change variable type

InPHPAs mentioned in the manual:"PHPIt is not supported (or not required) and the variable type is defined when a variable is declared. the variable type is determined based on its application. If you are a variableVarIf the value is assigned to a string, it becomes a string. If you assign an integer to it, it becomes an integer."

$ Foo = "0"; // $ fooIs a string(ASCII 48)
$ Foo ++; // $ fooIs a string"1" (ASCII 49)
$ Foo + = 1; // $ fooIt is an integer.(2)
$ Foo = $ fool + 1.3; // $ fooIs a double-precision number(3.3)
$ Foo = 5 + "10 Little Piggies"; // $ fooIs an integer.(15)
$ Foo = 5 + "10 Small Pigs"; // $ fooIs an integer.(15)

If you want to forcibly convert the variable type, you can useCFunctions with the same languageSettype ().

4.2Variables and constants

You may have noticed that all variables have a dollar sign ($. All variables are local variables. to enable the use of external variables in the defined functionGlobalStatement. And you need to limit the scope of the variable to the function, useStaticStatement.
$ G_var = 1 ;//Global range
Function test ()
{
Global $ g_var ;//In this way, global variables can be declared.
}

More advanced is variable representation. SeePHPManual. This is sometimes useful.

  PHPMany defined variables are built in. You can also useDefineFunction defines your own constants, suchDefine ("CONSTANT", "value ").

4.3Operator

  PHPWithC, C ++AndJavaCommon operators in. These operators have the same priority. Same value assignment"=".

Arithmetic and character

Only one of the following operators is related to characters:
$ A + $ B: Add
$ A-$ B: Minus
$ A * $ B: Multiplication
$ A/$ B: Division
$ A % $ B: Modulo (remainder)
$ A. $ B: String connection

Logic and comparison

Logical operators include:
$ A | $ B: Or
$ A or $ B: Or
$ A & $ B: And
$ A and $ B: And
$ A xor $ B: Exclusive or (when$Or$ BIsTrueIsTrueWhen the two are the sameFalse)
! $: Non
Comparison operators:
$ A = $ B: Equal
$! = $ B: Not equal
$ A <$ B: Less
$ A <= $ B: Less than or equal
$ A> $ B: Greater
$ A> = $ B: Greater than or equal
AndCSamePHPThere are also three operators (? :). Bitwise operators inPHPAlso exists.

Priority

Just sumCAndJavaSame!

4.4Control Process Structure

  PHPWithCSame process control. I will give a rough introduction below.

If, else, elseif, if (): endif

If (Expression 1)
{
...
}
Elseif (Expression 2)
{
...
}
Else
{
...
}
//Or imagePythonSame
If (Expression 1):
...
...
Elseif (Expression 2):
...
Else:
...
Endif;

Loops. while, do... while,

While (Expression)
{
...
}
Do
{
...
}
While (Expression);
For (Expression 1;Expression 2;Expression 3)
{
...
}
//Or imagePythonSame
While (expr ):
...
Endwhile;

Switch

SwitchIs for multipleIf-elseif-elseThe best replacement of the structure:
Switch ($ I)
{
Case 0:
Print "I equals 0 ";
Case 1:
Print "I equals 1 ";
Case 2:
Print "I equals 2 ";
}

Break, continue

BreakInterrupt the current loop control structure.
ContinueUsed to jump out of the remaining current loop and continue executing the next loop.Require, include

Just likeCIn# IncludeSame as preprocessing. You areRequireThe file specified in will replace its location in the main file. You can useInclude (). In this way, you canPHPFiles are divided into multiple files and referenced separately when different needs exist.

4.5Function

You can define your own functions as in the following example. The return value of a function can be any data type:
Function foo (Variable name 1,Variable name 2,...,Variable nameN)
{
Echo "Example function. n ";
Return $ retval;
}

AllPHPCode can appear in the function definition, and even include the definition of other functions and classes. The function must be defined before reference.

4.6Class

Create a class using the class model. ReferPHPA detailed explanation of the class in the manual.
Class Employee
{
Var $ empno ;//Number of employees
Var $ empnm ;//Employee Name

Function add_employee ($ in_num, $ in_name)
{
$ This-> empno = $ in_num;
$ This-> empnm = $ in_name;
}

Function show ()
{
Echo "$ this-> empno, $ this-> empnm ";
Return;
}

Function changenm ($ in_name)
{
$ This-> empnm = $ in_name;
}
}

$ Sbabu = new Employee;
$ Sbabu-> add_employee (10, "sbabu ");
$ Sbabu-> changenm ("babu ");
$ Sbabu-> show ();

Related Article

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.