简体   繁体   English

PHP中的strpos()函数如何返回int以及布尔值?

[英]How does strpos() function in PHP return int as well as boolean?

This is what w3School says about the strpos() function : 这就是w3School关于strpos()函数的说法:

Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found. 返回第一次出现在另一个字符串中的字符串的位置,如果找不到该字符串,则返回FALSE。

How is the function defined? 功能是如何定义的? Is it possible to write such functions which returns values of 2 different datatypes in other languages as well? 是否可以编写这样的函数,这些函数在其他语言中也返回2种不同数据类型的值?

Many dynamically typed languages allow you to return whatever you want from a function. 许多动态类型语言允许您从函数返回任何您想要的内容。 Strongly typed languages generally don't. 强类型语言通常不会。

In PHP a function can return anything it wants, including booleans, ints, strings, null, arrays, all kinds of objects, whatever you need. 在PHP中,函数可以返回它想要的任何东西 ,包括布尔值,整数,字符串,null,数组,各种对象,无论你需要什么。 It's the same in for example Javascript and Lua, which are also dynamically typed. 它在例如Javascript和Lua中是相同的,它们也是动态类型的。

Just make sure to check the return type of the function if it can be different kinds of things. 如果它可以是不同类型的东西,请确保检查函数的返回类型。 Especially with things like int(0) and false, which are considered equivalent if you use an == comparison. 特别是对于int(0)和false之类的东西,如果你使用==比较,它们被认为是等价的。 Use === to make sure it's the right type. 使用===确保它是正确的类型。

Other languages do not have loosely typed variables and function returns . 其他语言没有loosely typed variables and function returns Take for example C. 以例如C.

Taken from php.net : 来自php.net

PHP is a dynamic, loosely typed language, that uses copy-on-write and reference counting.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM