Phalcon ?? Basic PHP knowledge (I)-php Tutorial

Source: Internet
Author: User
Tags bitwise operators
Phalcon ?? Basic PHP knowledge (I) I. variables and constants

1.1. variable name (Identifier)

1) variable: sign starting with $
2) variable name: it may consist of letters, numbers, and _ 3. it cannot start with a number.

3) the identifier is case-sensitive, but the function name is case-insensitive.

4) variable names can be the same as function names. although they are allowed, try to avoid confusion.

PHP does not need to declare a variable before using it. this variable is created only when a variable is assigned a value for the first time.

For example:

$ 3age; // error $ _ = 6; // $ * p = 30; // error


1.2. data type

1) Integer: used to represent an Integer.

2) Float: used to represent all real numbers.

3) String: used to represent a String.

4) Boolean: used to indicate true or false.

5) Array: used to save multiple data items of the same type.

6) Object: the instance used to save the class.

PHP can determine the type of the variable at any time based on the value stored in the variable. when forced type conversion is required, it can be converted according to the java syntax.

For example:

$icount = 0;$fcount = (float)$icount;

1.3 Definition of constants

A constant can save a value, but the value of a constant cannot be changed once the program is initialized.

define('PI', 3.14159);echo PI;

Constants are generally expressed in uppercase and defined using the define function without the $ symbol.


1.4. variable scope

Scope refers to the range in which a variable can be used or visible in a script. PHP has six basic scope rules.

1) built-in Super global variables can be used anywhere in the script.

For example:

$ GLOBALS: an array of all global variables. $ _ SERVER: SERVER environment variable array $ _ GET: the variable array passed to the script by the GET method $ _ POST: the variable array passed to the script by The POST method $ _ COOKIE: cookie variable array $ _ FILES: variable array related to file Upload $ _ ENV: environment variable array $ _ REQUEST: variable data input by all users, including $ _ GET, $ _ POST, and $ _ COOKIE $ _ SESSION: SESSION variable array

2) once declared, constants can be globally visible.

3) The global variables declared in the script are visible in the whole script.

4) when the variable used in the function is declared as a global variable, the name must be consistent with the global variable name.

5) variables created and declared as static inside the function are invisible outside the function, but this value is maintained during multiple execution of the function.

6) the variable created inside the function is local. when the function is terminated, the variable does not exist.


II. operators or operators

2.1 Arithmetic operators

Arithmetic operators are numeric operators, which are usually used for integer or double-precision data. If it is applied to strings, PHP will try to convert these characters into a number. if it contains "e" or "E ", it is treated as a scientific notation and converted to a floating point number. Otherwise, it is converted to an integer. PHP searches for numbers at the beginning of the string and uses these numbers as the value of the string. If no number is found, the value of the string is 0.

Operator Name Example
+ Add $ A + $ B
- Subtraction $ A-$ B
* Multiplication $ A * $ B
/ Division $ A/$ B
% Module $ A % $ B






2.2 string connection

Use "."

$string = "hello"." world";

2.3 value assignment

Compound value assignment operator
Operator Usage Equivalent
+ = $ A + = $ B $ A = $ a + $ B
-= $ A-= $ B $ A = $ a-$ B
* = $ A * = $ B $ A = $ a * $ B
/= $ A/= $ B $ A = $ a/$ B
% = $ A % = $ B $ A = $ a % $ B
. = $ A. = $ B $ A = $ a. $ B








2.4 Reference

The reference operator "&" can be used in correlated values. A reference is equivalent to an alias rather than a pointer. it points two variables to the same memory address and can be reset using unset.

For example:

$a = 1;$b = &$a;$b = 2;unset($a);

2.5 Comparison

Comparison operator
Operator Name Usage
= Equal $ A = $ B
=== Heng et al $ A ===$ B
! = Not supported $! = $ B
! = Nonconstant $! ==$ B
<> Not supported $ A <> $ B
< Less $ A <$ B
> Greater $ A> $ B
<= Less than or equal $ A <= $ B
> = Greater than or equal $ A> = $ B










2.6. logical operations

Logical operators
Operator Name Usage
! Non ! $ B
&& And $ A & $ B
| Or $ A | $ B
And And $ A and $ B
Or Or $ A or $ B
Xor Exclusive or $ A xor $ B








2.7 bitwise operations

Bitwise operators
Operator Name Usage
& Bitwise AND $ A & $ B
| By bit or $ A | $ B
~ Non-Bitwise ~ $
^ Bitwise OR $ A ^ $ B
< Left Shift $ A <$ B
> Right Shift $ A> $ B








2.8 Others

1) comma operator

Comma operator "," is used to separate function parameters and other list items. this operator is often used together.

2) Class operators

"New" and "->" are used to initialize the instance of the class and the member of the category class respectively.

3) ternary operators

Ternary operator "? : "It is similar to the expression version of the condition statement if-else. the syntax format is as follows:

condition ? value if true : value if false

4) error blocker

The error blocker "@" can be used before any expression. this operator can be used to suppress warnings generated by code.

5) execution operator

The execution operator is a pair of operators. it is a pair of reverse single quotes "'"'. PHP will try to use the commands between reverse single quotes as the command line on the server side for execution, the expression value is the execution result of the command, for example:

echo `ls -l`;


6) type operators

The "instanceof" operator allows you to check whether an object is an instance of a specific class. for example:

if ($object instanceof sampleClass)  echo "Object is an instance of sampleClass";

2.9 operator priority and associativity

From low to high:

Operator priority
Associativity Operator
Left ,
Left Or
Left Xor
Left And
Right Print
Left = + =-= * =/=. = % = & =! = ^ = ~ = <=> =
Left ? :
Left |
Left &&
Left !
Left ^
Left &
Unrelated =! ===! =
Unrelated <=> =
Left <>
Left + -.
Left */%
Right ! ~ ++ -- (Type )@
Right []
Unrelated New
Unrelated ()




















III. test functions and test variables

3.1. PHP provides specific types of test functions.

For example:

1) is_array (): Check whether the variable is an array.

2) is_double (), is_float (), and is_real (): Check whether the variable is a floating point. all functions are the same.

3) is_long (), is_int (), and is_integer (): Check whether the variable is an integer. all functions are the same.

4) is_string (): Check whether the variable is a string.

5) is_bool (): Check whether the variable is a Boolean value.

6) is_object (): Check whether the variable is an object.

7) is_resource (): Check whether the variable is a resource.

8) is_null (): Check whether the variable is null.

9) is_scalar (): checks whether the variable is a scalar, that is, an integer, a Boolean value, a string, or a floating point number.

10) is_numeric (): checks whether the variable is a numeric or numeric string.

11) is_callable (): checks whether the variable is a valid function name.


3.2 Test the variable status

PHP has several functions to test the variable state.

For example:

1) isset ()

bool isset(mixed var[, mixed var[, ...]])

Issset () requires a variable as the parameter. if this variable exists, true is returned; otherwise, false is returned. You can also pass a list of variables separated by commas. If all variables are set, true is returned.

You can also use the unset () corresponding to isset () to destroy a variable:

void unset(mixed var[, mixed var[, ...]])

2) empty ()

bool empty(mixed var)

Empty () can be used to check whether a variable exists and whether its value is non-null and non-0. the corresponding return value is true or false.

IV. control statements

4.1. if else

if (condition) {  expression;}elseif (condition) {  expression;}else {  expression;}

4.2. switch

switch (expression) {  case value1:    expression;    break;  case value2:    expression;    break;  defalut:    expression;    break;}

The switch statement works like an if statement, but it allows more than two possible values. In a switch statement, as long as the condition is a simple data type, you can provide a case statement to process each condition value and provide the corresponding action code, in addition, there is a default case condition to handle situations where no specific value is provided.

When a specific case in the switch statement is matched, PHP will execute the code in the case until a break statement is encountered. if there is no break statement, switch will execute all the code in the case below this case with the value true.

4.3 while

while (condition) {  expression;}

4.4.

for (expression1; condition; expression2) {  expression3;}

Expression1 is executed only once at the beginning. Generally, the initial value of the counter is set here. Before each loop starts, the condtion expression is tested. if the expression value is false, the loop ends. Expression2 is executed at the end of each loop. Generally, the counter value is adjusted here. Expression3 is executed once in each loop.

4.5. do while

do {  expression;}while (condition);

4.6 jump out of control structure

There are three methods to stop a piece of code.

1) If you want to terminate a loop, you can use the break statement. The script starts to execute the first statement after the loop body.

2) If you want to skip to the next loop, you can use the continue statement.

3) If you want to end the execution of the entire PHP script, you can use the exit statement.

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.