通过PHP GET传递布尔值 [英] Passing a boolean through PHP GET

查看:371
本文介绍了通过PHP GET传递布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单的问题在这里,但不知道答案。我可以通过获取布尔变量吗?例如:

  http://example.com/foo.php?myVar=true 


$ b

然后我有

  $ hopefullyBool = $ _GET ['myVar']; 

$ hopefullyBool 一个布尔值或字符串?我的假设是这是一个字符串,但有人可以告诉我吗?谢谢

解决方案

所有GET参数都是PHP中的字符串。为了得到布尔类型,传递值为 true false ,例如 0 1 ,然后使用:

  $ hopefullyBool =(布尔)$ _ GET ['myVar']; 

如果您想传递字符串 true false 然后:

  $ hopefullyBool = $ _GET ['myVar'] =='真'?真假; 

甚至更好:

  $ hopefullyBool = filter_var($ _ GET ['myVar'],FILTER_VALIDATE_BOOLEAN); 


Pretty simple question here, not sure the answer though. Can I pass a boolean variable through get? For example:

http://example.com/foo.php?myVar=true

then I have

$hopefullyBool = $_GET['myVar'];

Is $hopefullyBool a boolean or a string? My hypothesis is that it's a string but can someone let me know? Thanks

解决方案

All GET parameters will be strings in PHP. To get the type boolean, pass as something that evaluates to true or false like 0 or 1, then use:

$hopefullyBool = (bool)$_GET['myVar'];

If you want to pass string true or false then:

$hopefullyBool = $_GET['myVar'] == 'true' ? true : false;

Or even better:

$hopefullyBool = filter_var($_GET['myVar'], FILTER_VALIDATE_BOOLEAN);

这篇关于通过PHP GET传递布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆