Home Backend Development PHP Tutorial php流程控制语句以及条件判断中的数据类型转换

php流程控制语句以及条件判断中的数据类型转换

Jun 20, 2016 pm 01:01 PM
php

php流程控制语句以及条件判断中的数据类型转换

php的流程控制语句包括循环体语句和分支判断语句。

循环操作包括三组语句,分别是while、do-while、for循环。

分支判断体包括两组:if else语句、switch语句。

以下以几个题目来理解php流程控制语句以及条件判断中的数据类型转换。

***********************************************

题目一、if-else语句与条件判断涉及的数据类型转换

***********************************************

<?php /*习题*/
/*1。写出下列代码的运行结果*/
	if(5<4)
		echo "true";
	else
		echo "false-1"."<br />";
	if("0")
		echo "true";
	else
		echo "false-2"."<br>";
	if($i)
		echo "true";
	else
		echo "false-3"."<br>";
	if("false")
		echo "true";
	else
		echo "false-4"."<br>";
	if(false)
		echo "true";
	else
		echo "false-5"."<br>";

	if(0.0)
		echo "true";
	else
		echo "false-6"."<br>";
	if(php)
		echo "true";
	else
		echo "false-7"."<br>";
	if("")
		echo "true";
	else
		echo "false-8"."<br>";
	if(null)
		echo "true";
	else
		echo "false-9"."<br>";
	if(array(12))
		echo "true";
	else
		echo "false-10"."<br>";
	if(array())
		echo "true";
	else
		echo "false-11"."<br>";
		
/*
	输出结果:
	false-1
	false-2
	false-3
	true false-5
	false-6
	true false-8
	false-9
	true false-11
	
	分析:if语句当条件不满足(即条件表达式计算结果为false)都会返回假。php的if语句没有then,有elseif(同else if)
	当转换为boolean时候,以下值被认为是false:
	1)布尔值false;
	2)整型值0(零);
	3)浮点型值0.0(零);
	4)空白字符串和字符串“0”;
	5)没有成员变量的数组;
	6)没有单元的对象(仅适用于PHP4);
	7)特殊类型NULL(包括尚未设定的变量);
*/						



?>
Copy after login

***********************************************
题目二、switch-case-default语句与break语句的关系

***********************************************

<?php /*2。写出下列代码的运行结果*/
	$a = "linux";
	switch($a){
		//echo "hello";
		case 'lamp':
			echo "one";
			break;
		case php:
			echo "five";
			break;
		case 'linux'://用不用引号均可
			echo "six"."<br />";
		case 6:
			echo "??";	
		case '8':
			echo "|||";
		case 10:
			echo "10"."<br>";
			break;//如果去掉此break将输出default分支的@@
		default:
			echo "@@";
			break;
	
	}
/*
运行结果为:
six
??|||10

分析:请注意以上switch-case-default语句中,有的地方是没有break语句的。
①default在所有case开关中,总是最后执行。
②一旦switch的条件判断进入了一个开关(包括default),如果这个开关里面没有break跳出语句,则这个开关之后的语句被当做普通程序语句来执行,直到遇到break为止(比如:如果条件满足了case 2开关,而case 2开关里面没有break语句,则case 2之后的语句被当做普通程序语句执行,case判断不再有效。)。而如果进入的这个开关里面有break语句,则执行完之后就跳出switch。
③当前一语句的case语句为空时候,控制器自动转移到下一case语句。
由此可见,使用switch的时候,如果case里面没有break,这个程序是相当危险的,这其中潜伏着的漏洞即使经验丰富的测试人员也可能很难发现。

参考w3school的解释:

工作原理:

    1、对表达式(通常是变量)进行一次计算
    2、把表达式的值与结构中 case 的值进行比较
    3、如果存在匹配,则执行与 case 关联的代码
    4、代码执行后,break 语句阻止代码跳入下一个 case 中继续执行
    5、如果没有 case 为真,则使用 default 语句


*/
?>
Copy after login

***********************************************

题目三、switch-case-default语句和数据类型自动转换的问题

***********************************************

<?php /*3。写出下列代码的运行结果*/
	$a = "abc";  
	switch ($a) {
		case 0: 
			echo "a 为 0";
			break;
		case "cdef": 
			echo "a 为 cdef";
			break;
		case "abc": 
			echo "a 为 abc";
			break;
		default:
			echo "a 什么都不是";
			break;
	}
	/*输出结果:a 为 0
	
	分析:
	这个问题应该从PHP作为动态脚本语言说起:
	动态语言的变量类型是随存储的变量而变化的,即变量类型是根据具体环境而进行变化的。
	字符串"abc",从表面来看可能指确切字符串,但是对于PHP存储的时候它是不能确定的,因为随着语义的变化,其所起的作用也会变化
	第一个case 0:,PHP脚本将常量0解释为确切的整数,与字符串比较大小时,需要将字符串转换为整数。PHP脚本解释器会自动将字符串abc,从十进制(默认)转换为数字,但是abc很明显不能代表十进制数,即转换失败,此时PHP将结果置为0,如果将字符串表示为"0xabc",那么PHP脚本能够发现0x标识,将abc作为十六进制转换,所以能够得到abc标识的十六进制整数。

	如果学过C语言,可以做一个简单比较:
	C中的case必须是数字常量,而不能是字符串,就因为C是变量类型确定的,运行时不能将确定的变量按语义随意转换。而PHP可以,因为PHP语言是动态的,变量类型是动态的。

	或者从PHP的例子中学习:
	如果一个文件将111111字符串(不是数字)存储到文件,然后从文件直接读取到变量$a,如下语法就可以将$a作为整数计算:0+$a
	上述表达式得到的结果也可以作为动态语言的一个变现来看
	
	*/
?>
Copy after login

***********************************************

题目四、编程中的疏忽易错点——比较运算符和赋值运算符的差异

***********************************************

<?php /*4。写出下列代码的运行结果*/
	if($i = ""){
		echo "a";
	
	}
	else {
		echo "b";
	}
	/* 输出结果:b
	这里的条件表达式不是比较运算符==,而是赋值运算符=,显然,空字符串和字符串0都为false。
	*/

?>
Copy after login

通过以上四个例子,我们在php基础语法的前提下,更好的理解php语言在使用中的规则,尤其是php流程控制语句以及条件判断中的数据类型转换。这些知识的掌握有利于养成良好的代码编写习惯,规避一些现实中的陷阱,提高开发效率。


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 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
1655
14
PHP Tutorial
1253
29
C# Tutorial
1228
24
PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles