Table of Contents
PHP第五课 自动类型转换与流程控制
{$score}
Home php教程 php手册 PHP第五课 自动类型转换与流程控制

PHP第五课 自动类型转换与流程控制

Jun 13, 2016 am 09:26 AM
process type

PHP第五课 自动类型转换与流程控制

学习概要:

1.了解自动类型转换的有哪些

2.了解基本的流程控制语句

3.实例:实现日历表格的写法


自动类型转换

1)整型转字符串
echo $num."abc";
Copy after login


2)字符串转整型
$str+3;
3)转布尔类型
为假情况 0 "" "0" false array() null 未定义


4)强制类型转换
(int)$str
(float)$str
(string)$str
Copy after login




5)常量
define("HOST","localhost");
Copy after login

6)运算符
①一元
++ --


②二元
= - * / %
= += -= *= /= %=
> >= < <= == != === !==
&& || !


③三元
? :


流程控制:
1.流程控制
2.函数




流程控制:


一.分支结构
if...elseif....else
switch...case
如果条件是固定值的话,就用switch语句


二.循环控制
for
while


三.中止循环
break:直接结束
<?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
Copy after login


continue:结束本次循环
<?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
Copy after login




小技巧:exit表示暂停下面的程序
echo date("w");
date 中w表示星期几
<?PHP
 	header("content-type:text/html;charset=utf-8");
	echo date("Y-M-D");//分别表示年月日
	exit;
	echo "John";
	?>
Copy after login

五.剩余部分
1.do...while

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



4.九九乘法表
<?php
		    for($i=1;$i<=9;$i++){
		    	for($j=1;$j<=$i;$j++){
		    		echo "$i*$j=".$i*$j." ";
		    	}
		    	echo "<br>";
		    }
	?>
Copy after login



3.PHP实现日历表格


日历表格:
1.两层for循环
2.隔行换色
3.用到if条件判断
4.header头改编码

<?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>";
?>
Copy after login

添加背景色


<?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";
		?>
Copy after login


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to change network type to private or public in Windows 11 How to change network type to private or public in Windows 11 Aug 24, 2023 pm 12:37 PM

Setting up a wireless network is common, but choosing or changing the network type can be confusing, especially if you don't know the consequences. If you're looking for advice on how to change the network type from public to private or vice versa in Windows 11, read on for some helpful information. What are the different network profiles in Windows 11? Windows 11 comes with a number of network profiles, which are essentially sets of settings that can be used to configure various network connections. This is useful if you have multiple connections at home or office so you don't have to set it all up every time you connect to a new network. Private and public network profiles are two common types in Windows 11, but generally

How to open multiple Toutiao accounts? What is the process for applying for a Toutiao account? How to open multiple Toutiao accounts? What is the process for applying for a Toutiao account? Mar 22, 2024 am 11:00 AM

With the popularity of mobile Internet, Toutiao has become one of the most popular news information platforms in my country. Many users hope to have multiple accounts on the Toutiao platform to meet different needs. So, how to open multiple Toutiao accounts? This article will introduce in detail the method and application process of opening multiple Toutiao accounts. 1. How to open multiple Toutiao accounts? The method of opening multiple Toutiao accounts is as follows: On the Toutiao platform, users can register accounts through different mobile phone numbers. Each mobile phone number can only register one Toutiao account, which means that users can use multiple mobile phone numbers to register multiple accounts. 2. Email registration: Use different email addresses to register a Toutiao account. Similar to mobile phone number registration, each email address can also register a Toutiao account. 3. Log in with third-party account

Implementing dynamic arrays in Python: from beginner to proficient Implementing dynamic arrays in Python: from beginner to proficient Apr 21, 2023 pm 12:04 PM

Part 1 Let’s talk about the nature of Python sequence types. In this blog, let’s talk about Python’s various “sequence” classes and the three built-in commonly used data structures – list, tuple and character. The nature of the string class (str). I don’t know if you have noticed it, but these classes have an obvious commonality. They can be used to save multiple data elements. The most important function is: each class supports subscript (index) access to the elements of the sequence, such as using SyntaxSeq[i]​. In fact, each of the above classes is represented by a simple data structure such as an array. However, readers familiar with Python may know that these three data structures have some differences: for example, tuples and strings cannot be modified, while lists can.

Are Douyin sleep anchors profitable? What are the specific procedures for sleep live streaming? Are Douyin sleep anchors profitable? What are the specific procedures for sleep live streaming? Mar 21, 2024 pm 04:41 PM

In today's fast-paced society, sleep quality problems are plaguing more and more people. In order to improve users' sleep quality, a group of special sleep anchors appeared on the Douyin platform. They interact with users through live broadcasts, share sleep tips, and provide relaxing music and sounds to help viewers fall asleep peacefully. So, are these sleep anchors profitable? This article will focus on this issue. 1. Are Douyin sleep anchors profitable? Douyin sleep anchors can indeed earn certain profits. First, they can receive gifts and transfers through the tipping function in the live broadcast room, and these benefits depend on their number of fans and audience satisfaction. Secondly, the Douyin platform will give the anchor a certain share based on the number of views, likes, shares and other data of the live broadcast. Some sleep anchors will also

How to create a video matrix account? What types of matrix accounts do it have? How to create a video matrix account? What types of matrix accounts do it have? Mar 21, 2024 pm 04:57 PM

With the popularity of short video platforms, video matrix account marketing has become an emerging marketing method. By creating and managing multiple accounts on different platforms, businesses and individuals can achieve goals such as brand promotion, fan growth, and product sales. This article will discuss how to effectively use video matrix accounts and introduce different types of video matrix accounts. 1. How to create a video matrix account? To make a good video matrix account, you need to follow the following steps: First, you must clarify what the goal of your video matrix account is, whether it is for brand communication, fan growth or product sales. Having clear goals helps develop strategies accordingly. 2. Choose a platform: Choose an appropriate short video platform based on your target audience. The current mainstream short video platforms include Douyin, Kuaishou, Huoshan Video, etc.

What is the type of return value of Golang function? What is the type of return value of Golang function? Apr 13, 2024 pm 05:42 PM

Go functions can return multiple values ​​of different types. The return value type is specified in the function signature and returned through the return statement. For example, a function can return an integer and a string: funcgetDetails()(int,string). In practice, a function that calculates the area of ​​a circle can return the area and an optional error: funccircleArea(radiusfloat64)(float64,error). Note: If the function signature does not specify a type, a null value is returned; it is recommended to use a return statement with an explicit type declaration to improve readability.

Best practices for type hints in Python Best practices for type hints in Python Apr 23, 2023 am 09:28 AM

It’s great to use dynamic language for a while, and the code is reconstructed in the crematorium. I believe you must have heard this sentence. Like unit testing, although it takes a small amount of time to write code, it is very worthwhile in the long run. This article shares how to better understand and use Python's type hints. 1. Type hints are only valid at the syntax level. Type hints (introduced since PEP3107) are used to add types to variables, parameters, function parameters, and their return values, class properties, and methods. Python's variable types are dynamic and can be modified at runtime to add type hints to the code. It is only supported at the syntax level and has no impact on the running of the code. The Python interpreter will ignore the type hints when running the code. Therefore the type

What are the main self-media platforms? What are the types of self-media platforms? What are the main self-media platforms? What are the types of self-media platforms? Mar 21, 2024 pm 06:36 PM

With the rapid development of the Internet, self-media has become an important channel for information dissemination. We-media platforms provide a stage for individuals and companies to showcase themselves and spread information. Currently, the main self-media platforms on the market include WeChat official accounts, Toutiao, Yidian News, Penguin Media Platform, etc. Each of these platforms has its own characteristics and provides a wealth of creative space for the majority of self-media practitioners. Next, we will introduce these platforms in detail and explore the types of self-media platforms. 1. What are the main self-media platforms? WeChat official account is a self-media platform launched by Tencent to provide information release and dissemination services for individual and corporate users. It is divided into two types: service account and subscription account. Service account mainly provides services for enterprises, while subscription account focuses on information dissemination. Depend on

See all articles