简体   繁体   English

PHP函数检查2个变量是否为空

[英]PHP function to check if 2 variables are empty

I have an index page, I want it to include a page called splash.php and not display.php when a user lands on index.php, but once a user does something (sets a variable) ie if a user searches (variable "query") i want it to include display.php and not include splash.php 我有一个索引页,当用户登陆index.php时,我希望它包含一个名为splash.php而不是display.php的页面,但是一旦用户执行了某项操作(设置了变量),即用户搜索了(变量“查询”),我希望它包括display.php而不包括splash.php

What is wrong with this code? 此代码有什么问题?

function hasGet()
{
    return !empty($_GET['fact']);
    return !empty($_POST['query']);
}

if (hasGet()) {
    include("display.php");
}

else {
    include("splash.php");
}

This question should be removed 这个问题应该删除

Only the first return statement is executed. 仅执行第一个return语句。 Try: 尝试:

return !empty($_GET['fact']) && !empty($_POST['query']);

A better way to accomplish what you are trying to do is use sessions. 完成会话的一种更好的方法是使用会话。

index.php 的index.php

<?php

session_start();

if (!isset($_SESSION['visited'])) {
    $_SESSION['visited'] = true;
    include 'splash.php';
} else {
    include 'display.php';
}

?>

This way after a user visits index.php for the first time, $_SESSION['visited'] is set to true and it won't show the splash page throughout their visit. 这样,在用户首次访问index.php之后, $_SESSION['visited']设置为true,并且不会在整个访问过程中显示初始页面。

You cannot have two returns as you are doing. 您不能像做的那样有两个回报。 Try 尝试

return (!empty($_GET['fact']) && !empty($_GET['query']));

You might want to try this... 您可能想尝试一下...

if($_SERVER["SCRIPT_NAME"] == "/index.php"){
    include("splash.php");
}else{
    include("display.php");
}

2. 2。

if(!empty($_GET["fact"]) || !empty($_POST["query"])){
    include("display.php");
}else{
    include("splash.php");
}

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

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