
正文
php传参指定数据类型 php 传值
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何指定 PHP 数据类型
php 函数的参数类型可以指定为类名或数组类型array,比如 这样是对的public function Right( My_Class $a, array $b ) 这样是错的public function Wrong( string $a, boolean $b ) 如果需要其他类型
相关问答
Q1: PHP自定义函数时怎么指定参数类型?
class User{
public $name;
public $password;
function __construct($name,$password){
$this-name=$name;
$this-password=$password;
}
}
//参数可以指定对象类型
function f1(User $user){
echo $user-name,”,”,$user-password;
}
//参数可以指定数组类型
function f2(array $arr){}
//参数不可以指定基本类型,下面一句会出错
function f3(string $s){}
Q2: php 如何传参(将一个数组传参)
url传参不支持数组.
方法1: 把数组遍历出来,组成键值对传过去在接收.
方法2:把数组序列化,接收后在反转.
示例:$arr = array('1','2','3');
$str = base64_encode(serialize($arr));//此时数组已经转换为字符串
$a = unserialize(base64_decode($str));
print_r($a);
Q3: php函数设定参数类型
php 函数的参数类型可以指定为类名或数组类型array,比如
这样是对的public function Right( My_Class $a, array $b )
这样是错的public function Wrong( string $a, boolean $b )
如果需要其他类型,需要在函数内部进行类型检查
参考
这一段
public function Right( My_Class $a, array $b )
tells first argument have to by object of My_Class, second an array. My_Class means that you can pass also object of class that either extends My_Class or implements (if My_Class is abstract class) My_Class. If you need exactly My_Class you need to either make it final, or add some code to check what $a really.
Also note, that (unfortunately) "array" is the only built-in type you can use in signature. Any other types i.e.:
public function Wrong( string $a, boolean $b )
will cause an error, because PHP will complain that $a is not an *object* of class string (and $b is not an object of class boolean).
So if you need to know if $a is a string or $b bool, you need to write some code in your function body and i.e. throw exception if you detect type mismatch (or you can try to cast if it's doable).
Q4: php如何传递类参数
PHP类中,可能有多个属性参数。当使用new创建一个对象的时候,可能需要完成初始化操作,需要从外边传递参数进来。下面演示具体过程:
?php
class Test {
//定义私有变量name ,age
private $name, $age;
//构造函数,初始化的时候最先执行
public function __construct($name, $age) {
$this-name = $name;
$this-age = $age;
}
public function showMsg() {
return "大家好,我叫".$this-name.";今年".$this-age."岁了!";
}
}
//定义参数
$name="百度知道";
$age=10;
//初始化类的时候传递参数
$te=new Test($name, $age);
echo $te-showMsg();
//输出结果:大家好,我叫百度知道;今年10岁了!
?
Q5: php如何用标签传递参数?传递后如何接受该参数?
写法如下php传参指定数据类型:
a href='deal.php?id=5'
在deal.php里面:
用$_GET['id']来获取
?php
$result = $_GET["id"];
echo $result;
?
PHPphp传参指定数据类型,是英文超文本预处理语言Hypertext Preprocessorphp传参指定数据类型的递归缩写。PHP 是一种 HTML 内嵌式的语言php传参指定数据类型,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛地运用。可以生成Forms,ComboBoxes,Grid,Menus等的组件,并支持将数据转为XML/JSON格式。
PHP类中,可能有多个属性参数。当使用new创建一个对象的时候,可能需要完成初始化操作,需要从外边传递参数进来。
PHP通过引用传递参数用法的示例:
?php
function add_some_extra($string) // 引入变量,使用同一个存储地址
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // outputs 'This is a string, and something extra.'
?
输出:
This is a string, and something extra.
如果没有这个符号,
?php
function add_some_extra($string)
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // outputs 'This is a string, '
?
输出:
This is a string,
php传参指定数据类型的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php 传值、php传参指定数据类型的信息别忘了在本站进行查找喔。





