PHP Class 5 automatic type conversion and process control, php conversion process
Learning Overview:
1. Learn about automatic type conversion
2. Understand basic process control statements
3. Example: compile a calendar table
Automatic type conversion
1) convert integer to string
echo $num."abc";
2) convert string to integer
$ Str + 3;
3) convert to boolean type
False: 0 "" 0 "false array () null undefined
4) Forced type conversion
(int)$str(float)$str(string)$str
5) Constants
define("HOST","localhost");
6) Operators
① RMB
++ --
② Binary
=-*/%
= + =-= * =/= % =
>>=<==! ===! =
& |!
③ Ternary
? :
Process control:
1. Process Control
2. Functions
Process control:
I. Branch Structure
If... elseif... else
Switch... case
If the condition is a fixed value, use the switch statement.
Ii. Loop Control
For
While
3. Abort a loop
Break: end directly
<?PHP header("content-type:text/html;charset=utf-8");for($i=1;$i<10;$i++){if($i==3){break;} else{echo $i."<br>";}}?>//1 2
Continue: end this cycle
<?PHP header("content-type:text/html;charset=utf-8");for($i=1;$i<10;$i++){if($i==3){continue;} else{echo $i."<br>";}}?>//1 2 4 5 6 7 8 9
Tip: exit indicates that the following program is suspended.
Echo date ("w"); w in date indicates the day of the week <? PHP header ("content-type: text/html; charset = UTF-8"); echo date ("Y-M-D"); // indicate year month day exit; echo "John";?>
5. remaining parts
1. do... while
<?php $score=31;do{echo "
4. Multiplication table
<?php for($i=1;$i<=9;$i++){ for($j=1;$j<=$i;$j++){ echo "$i*$j=".$i*$j." "; } echo "<br>"; }?>
3. Implement calendar tables in PHP
Calendar table:
1. Two-layer for Loop
2. line-by-line color change
3. if condition judgment
4. encode the header
<?php header("content-type:text/html;charset=utf-8");$days= 31;echo "<table width='700px' border='1px'>";for($i=1;$i<=$days;){echo "<tr>";for($j=0;$j<7;$j++){if($i>$days){echo "<td> </td>";} else{echo "<td>{$i}</td>";}$i++;}echo "</tr>";}echo "</table>";?>
Add background color
<? Php header ("content-type: text/html; charset = UTF-8"); $ days = 31; echo "<table width = '700px 'border = '1px '> "; for ($ I = 1; $ I <= $ days;) {$ k ++; if ($ k % 2 = 1) {echo "<tr bgcolor = '# cccccc'>" ;}else {echo "<tr>" ;}for ($ j = 0; $ j <7; $ j ++) {if ($ I >$ days) {echo "<td> </td> ";} else {echo "<td >{$ I} </td>" ;}$ I ++;} echo "</tr> ";} echo "</table>" ;?> Abort Script 2. exit (); abort the script using 3.die(); <? Php echo "11111 <br>"; die ("Stop script from here"); echo "2222222";?>
PHP variable type conversion
1. int Is a simple Integer Conversion, while intval can use the second parameter to determine the hexadecimal conversion.
2. There is no function difference between float and floatval.
3. Note that intval and floatval are functions, while int and float are not functions.
Settype of the PHP variable type Conversion Function
This function is used to set the variable type. PHP built-in functions do not need to be configured.
Settype Function Structure
Bool settype (mixed var, string type)
The return value is a Boolean value, that is, success or failure. Both parameters are required. The above is the variable to set the type. The following is the type of the variable to be set.
There is no output at the end of the function operation. Only one real value is returned. It tells you whether the operation succeeds or fails.
Let's test a piece of code.
<? Php
$ Foo = "5bar"; // struct variable $ foo
Settype ($ foo, "integer"); // set the type of $ foo to integer.
Var_dump ($ foo); // print the variable information. The result is an integer of 5.
?>
After the variable is used, the type of the variable changes directly. If an error occurs, the parameter is not added.
============================
This is my personal habit.
<? PHP
$ Foo = "5bar ";
$ A = (int) $ foo; // declare the variable type
Var_dump ($ a); // The result is also an integer of 5.
?>