$sessions['lefthand'] = 'apple';
$sessions['righthand'] = '';
$sessions['head'] = 'hat';
$sessions['cloth'] = '';
$sessions['pants'] = '';
// here is the homework function
CheckSession('lefthand,righthand,head,cloth,pants');
我们有一些字符串“左手,右手,头,布,裤子” 问题是:“我们如何检查五个会话是否为空或存在并显示哪个会话为空(如果存在空会话),如果全部存在则返回true?
empty righthand , pants, and cloth.
这就是我的想法
这是我做的进展* edit4,:)
function CheckSession($sessions){
$all_sessions_exist = true;
$keys = explode(',',$sessions);
$error = array();
// Search for Session that are not exist
foreach ($keys as $key) {
if (!isset($_SESSION[$key]) && empty($_SESSION[$key])) {
echo "no $key</br>";
$all_sessions_exist = false;
}
}
return $all_sessions_exist;
}
谢谢你看看
Adam Ramadhan
答案 0 :(得分:2)
看到它的功课,你将无法获得解决方案。你虽然走在正确的轨道上。它由分隔符explode()
组成。您可以使用foreach
循环播放它并使用empty()
检查它们是否已设置。您可以访问$_SESSION[$key]
之类的会话。保留一组匹配的数据。
答案 1 :(得分:0)
function CheckSession($string){
$all_sessions_exist = true; #this will change if one of the session keys does not exist
$keys = explode(',', $string); #get an array of session keys
foreach($keys as $key){
if(isset($_SESSION[$key])) {
if(!empty($_SESSION[$key]))
echo '$_SESSION['.$key.'] is set and contains "'.$_SESSION[$key].'".'; #existing non-empty session key
else echo '$_SESSION['.$key.'] is set and is empty.' ;
}else {
echo '$_SESSION['.$key.'] is not set.'; #key does not exist
$all_sessions_exist = false; #this will determine if all session exist
}
echo '<br />'; #formatting the output
}
return $all_sessions_exist;
}
答案 2 :(得分:0)
只是清理你的功能
function CheckSession($sessions)
{
$session = explode(',',$sessions);
$return = array();
foreach ($session as $s)
{
$return[$s] = (isset($_SESSION[$s]) && !empty($_SESSION[$s]) ? true : false)
}
return $return;
}
$sessions['lefthand'] = 'apple';
$sessions['righthand'] = '';
$sessions['head'] = 'hat';
$sessions['cloth'] = '';
$sessions['pants'] = '';
检查部分
// here is the homework function
$Results = CheckSession('lefthand,righthand,head,cloth,pants');
修改
//$value1= false; // Commented out so does not get set
$value2= false; //Still set to a bool
error_reporting(E_ALL);
empty($value1); // returns false but still strikes E_NOTICE ERROR
empty($value2); // returns false with no error
注意,empty不会触发错误,但是这个例子会影响很多其他的php函数。