Operator and iterative finishing in PHP

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise operators error handling numeric logical operators php code php script

10. Operator

10.1 Arithmetic operators

Arithmetic operators are typically used for integer or double-precision types of data. If you apply them to a string, PHP attempts to convert the strings into a number. If it contains "E" or "E" characters, it will be treated as a scientific notation and converted to floating-point numbers, otherwise it will be converted to integers. PHP looks for numbers at the beginning of the string and uses them as the value of the string, and if no number is found, the string has a value of 0.

10.2 string operator

You can use the string concatenation operator to concatenate two strings to build and save them in a new string.

$a = "Bob ' s";

$b = "Auto Parts";

$result = $a. $b; "Bob ' s Auto Parts"
10.3 Assignment Operators

10.3.1 Assignment Operation return value

The value of the entire assignment statement is assigned to the operand on the left.

You can use parentheses to increase the precedence of a subexpression, the same as the math algorithm.

10.3.2 Compound assignment operator

+=,-=,*=,/=,%=,.=

10.3.3 forward increment and back increment decrement operator

As an example:


$a = 4;

echo + + $a; 5


$a = 4;

echo $a + +; 4

10.3.4 reference operator

The reference operator & can be used in the associated assignment. Look at the following example:

$a = 5;

$b = $a;

$a = 7; $b would still be 5
It's embarrassing. How can we avoid the above situation?

You can use the reference operator & to prevent such a copy from being produced.

$a = 5;

$b = & $a;

$a = 7; $a and $b are now both 7
You can change the address you point to by resetting them:

unset ($a);
Resetting does not change the value of $b (7), but can destroy $a and value 7 links that are saved in memory.

10.4 Comparison Operators

10.4.1 equals operator

Equality comparison operator = = Allows you to test whether two values are equal.

10.4.2 Other comparison operators

The return value is true only if the identical operator (= = =) has the same operand on both sides and has the same data type.

10.5 Logical operators

Operator "and" and "or" ratio && and | | Have a lower priority.

10.6-bit operator

Bitwise operators can treat an integer variable as a series of bits.

10.7 Other operators

The comma operator "," is used to separate function arguments and other list items.

New to initialize the instance of the class.

-> is used to access members of a class.

10.7.13-dollar operator

Condition? Value if True:value if False
10.7.2 Error suppression operator

The error suppression operator @ can be used in front of any expression:

$a = @ (57/0);
Can suppress the exception of 0 warnings.

If you suppress some warnings in this way, once you encounter a warning, write some error handling code.

If you have enabled the Track_errors attribute in the PHP configuration file, the error message will be saved in the global variable $php_errormsg.

10.7.3 execution operator

PHP will try to execute the command between the reverse single quotes as a server-side command line.

$out = ' dir c: ';

Echo ' <pre> '. $out. ' </pre> ';
Get a list of directories and save the list in $out, and then display the list in a browser or otherwise.

10.7.4 array operator

array element operators ([]) allow access to array elements. In some array contexts, you can also use the => operator.

10.7.5 type operator

The instanceof operator allows you to check whether an object is an instance of a particular class.

11. Calculate the total amount of the form


Define (' Tireprice ', 100);

Define (' Oilprice ', 10);

Define (' Sparkprice ', 4);

Create short variable names

$tireqty = $_post[' Tireqty '];

$oilqty = $_post[' Oilqty '];

$sparkqty = $_post[' Sparkqty '];


$totalqty = 0;

$totalqty = $tireqty + $oilqty + $sparkqty;

echo "Items ordered:". $totalqty. " <br/> ";

$totalamount = 0.00;

$totalamount = $tireqty * tireprice + $oilqty * oilprice + $sparkqty * sparkprice;

echo "Subtotal: $". Number_format ($totalamount, 2). " <br/> ";


$taxrate = 0.10; Local sales tax is 10%

$totalamount = $totalamount * (1 + $taxrate);

echo "total including Tax: $". Number_format ($totalamount, 2). " <br/> ";

12. Precedence and associativity of operators

Priority: Execution order.

Binding: The order in which operators of the same priority are executed.

13. Using Variable functions

13.1 Testing and setting variable types

String GetType (mixed Var);

BOOL Settype (mixed var, string type);
Other test functions:

Is_array (): Checks whether the variable is an array.

Is_double (), Is_float (), Is_real (): Whether it is a floating-point number

Is_long (), Is_int (), Is_integer (): is an integer

Is_string (): Whether it is a string

Is_bool (): Boolean value

Is_object (): Is an object

Is_resource (): is a resource

Is_null (): null

Is_scaler (): Whether it is a scalar

Is_numeric (): Is any type of numeric or numeric string

Is_callable (): is a valid function name

13.2 Test Variable Status

Isset (): To determine whether a variable exists (a list of variables can be passed)

Unset (): Destroys a variable

Empty (): Checks whether a variable exists, and whether its value is non-null and non-0

13.3 Re-interpretation of variables

Visual forced conversion:

int intval (mixed var [, int base]);

Float floatval (mixed Var);

String Strval (mixed Var);
The Intval () function also allows you to specify the base of the conversion when the variable to be converted is a string.

14. Making decisions on the basis of conditions

The space in the middle of the ElseIf statement is optional.

15. Iterative implementation of repetitive actions

HTML code:


<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>freight</title>
<body>
<table border= "0" cellpadding= "3" >
<tr>
&LT;TD bgcolor= "#ccc" align= "center" >Distance</td>
&LT;TD bgcolor= "#ccc" align= "center" >Cost</td>
</tr>
<tr>
&LT;TD align= "right" >50</td>
&LT;TD align= "right" >5</td>
</tr>
<tr>
&LT;TD align= "right" >100</td>
&LT;TD align= "right" >10</td>
</tr>
<tr>
&LT;TD align= "right" >150</td>
&LT;TD align= "right" >15</td>
</tr>
<tr>
&LT;TD align= "right" >200</td>
&LT;TD align= "right" >20</td>
</tr>
<tr>
&LT;TD align= "right" >250</td>
&LT;TD align= "right" >25</td>
</tr>
</table>
</body>

15.1 While loop

PHP Code:


<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>freight.php</title>
<body>
<table border= "0" cellpadding= "3" >
<tr>
&LT;TD bgcolor= "#ccc" align= "center" >Distance</td>
&LT;TD bgcolor= "#ccc" align= "center" >Cost</td>
</tr>
<?php
$distance = 50;
while ($distance <= 250) {
echo "<tr>
&LT;TD align=\ "right\" > ". $distance." </td>
&LT;TD align=\ "right\" > ". ($distance/10). " </td>
</tr>\n ";
$distance + 50;
}
?>
</table>
</body>

Note that right here uses \ instead of single quotes.

15.2 for and foreach loops

You can combine a variable variable with a for loop to repeat a series of form fields.


for ($i =1; $i <= $numnames; $i + +) {

$temp = "name$i";

echo $ $temp. ' <br/> ';

}

By dynamically creating variable names, you can access each form field in turn.

16. Jump out of control structure or script

A) End loop: Break

b) transferred to the next cycle: Continue

C End execution of the entire PHP script: Exit

17. Use replaceable control structure syntax


if ($totalqty = = 0):

echo "You did don't order anything on the previous page!<br/>";

Exit

endif

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.