Home  >  Article  >  Backend Development  >  PHP Lesson 5 Automatic Type Conversion and Process Control_PHP Tutorial

PHP Lesson 5 Automatic Type Conversion and Process Control_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:48806browse

PHP Lesson 5 Automatic Type Conversion and Process Control

Learning Summary:

1. Understand what automatic type conversion is

2. Understand basic flow control statements

3. Example: How to write 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 case 0 "" "0" false array() null undefined


4) Forced type conversion
(int)$str
(float)$str
(string)$str




5)Constant
define("HOST","localhost");

6)Operator
①One yuan
++ --


②Binary
= - * / %
= += -= *= /= %=
> >= && || !


③Three yuan
? :


Process Control:
1. Process control
2. Function




Process control:


1. Branch structure
if...elseif....else
switch...case
If the condition is a fixed value, use the switch statement


2. Loop control
for
while


3. Abort the 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 loop
<?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




Tips: exit means suspending the following program
echo date("w");
date 中w表示星期几
<?PHP
 	header("content-type:text/html;charset=utf-8");
	echo date("Y-M-D");//分别表示年月日
	exit;
	echo "John";
	?>

5. The remaining part
1.do...while

				<?php
		 
		 	$score=31;
			do{
				echo "<h1>{$score}</h1>";
			}while($score>=60);
		?>



4. Multiplication table
<?php
		    for($i=1;$i<=9;$i++){
		    	for($j=1;$j<=$i;$j++){
		    		echo "$i*$j=".$i*$j." ";
		    	}
		    	echo "<br>";
		    }
	?>



3. PHP implements calendar table


Calendar form:
1. Two-layer for loop
2. Change colors every other row
3. Use if conditional judgment
4.Change the header encoding

<?php
  header("content-type:text/html;charset=utf-8");
	$days= 31;
	
	echo "<table width=&#39;700px&#39; border=&#39;1px&#39;>";
	for($i=1;$i<=$days;){
		echo "<tr>";
		for($j=0;$j<7;$j++){
			if($i>$days){
				echo "<td>&#160;</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=&#39;700px&#39; border=&#39;1px&#39;>";
	for($i=1;$i<=$days;){
		
		$k++;
		
		if($k%2==1){
			echo "<tr bgcolor=&#39;#cccccc&#39;>";
		}else{
			echo "<tr>";
		}
		
		for($j=0;$j<7;$j++){
			if($i>$days){
				echo "<td>&#160;</td>";
			} else{
			echo "<td>{$i}</td>";
			}
				$i++;
		}
		echo "</tr>";
		
	}
	
	echo "</table>";
?>
	中止脚本
	
		2.exit();中止脚本使用
	    3.die();
			    <?php
		 
		 echo "11111<br>";
		 die("从这儿开始脚本中止");
		 echo "2222222";
		?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871184.htmlTechArticlePHP Lesson 5 Automatic Type Conversion and Process Control Learning Summary: 1. Understand what automatic type conversion is 2. Understand basic flow control statements 3. Example: Implement automatic type writing of calendar tables...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn