简体   繁体   English

如何在完成php页面之前确保满足每个条件?

[英]How to make sure every condition is met before completing a php page?

so say I have the php: 所以说我有PHP:

if(a === x)
{
    echo "a";
}

if(b === y)
{
    echo "b";
}

if(c === z)
{
    echo "c";
}

If a = x and b = y but c doesn't = z. 如果a = x且b = y但c = z。 Is there a way to stop a and b being "echoed"? 有没有办法阻止ab被“回应”?

and the same for if a = x but b doesn't = y and c = z. 如果a = x但b = y且c = z,则相同。 Is there a way to stop a and c being "echoed"? 有没有办法阻止ac被“回应”?


I know that if a doesn't = x and thus you don't want b and c to be echoed then you should do: 我知道如果a 不是 = x而且你不希望bc被回应那么你应该这样做:

if(a === x)
{
    echo "a";
}else
{
    die();
}

if(b === y)
{
    echo "b";
}

if(c === z)
{
    echo "c";
}

But I want to make sure that if one of the 3( a,b,c ) doesn't equal the corresponding 3(x,y,z) nothing is echoed. 但我想确保如果3( a,b,c )中的一个不等于相应的3(x,y,z),则不会回显任何内容。


I also can't use: 我也不能用:

if (a == x && b == y && c == z) {
    echo "success";
}

as each hypothetical condition is huge (ie a database insert and checking whether a url is working etc...) 因为每个假设条件都很大(即数据库插入并检查网址是否正常工作等)

With an example of checking whether a url is working: 通过检查URL是否正常工作的示例:

$file_headers = @get_headers("https://www.google.co.uk/");
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {

}
else {
    echo "Success concerning url";
}

and an example of inserting into a database: 以及插入数据库的示例:

if($result)
{
    echo "Success concerning inserting information to the database";
}

Is there a method on how to do this? 有没有办法如何做到这一点?

Whats the context of this? 这是什么背景? Anyway, just put them inside all together: 无论如何,只要将它们放在一起:

if($a === 'x' && $b === 'y' && $c === 'z') {
    // satisfied all three conditions, then proceed
} else {
    // if one of them fails, stop the process
}

Introducing boolean operators: and ( && ) and or ( || ). 介绍布尔运算符: && )和|| )。

if(a === x && b === y && c === y) {
    echo "a";
    echo "b";
    echo "c";
} else {
    die();
}

Similarly, you could kill the script if any of the conditions are not true. 同样,如果任何条件正确,您可以终止脚本。

if(a !== x || b !== y || c !== z) {
    die();
}

echo "a";
echo "b";
echo "c";

If you have many booleans ( a === x ) that make your conditional super long and difficult to read, you can always set the booleans to variables: 如果你有许多布尔值( a === x )使你的条件超长并且难以阅读,你可以随时将布尔值设置为变量:

$boolA = a === x;
$boolB = b === y;
$boolC = c === y;
$boolZ = z < 0 || z >= 100 || z === (a + b + c);

if(!$boolA || !$boolB || !$boolC || !$boolZ) {
    die();
}

// success

You could also make one or many functions to potentially clean up your code: 您还可以创建一个或多个函数来清理代码:

function isValid() {
    // perform lots of logic
    return true;
}

if(!isValid()) die();

// success

In the end of the day..the more complex your code gets, the more you should be using OOP . 在一天结束时......代码越复杂,您应该使用OOP越多。 There are a lot of PHP frameworks that will help a beginner get started in this mindset. 有很多PHP框架可以帮助初学者以这种心态开始。

Since I've been using Laravel recently, I'll use them as an example. 自从我最近使用Laravel以来,我将以它们为例。 First you have a routing file, which takes all requests to your site and dispatches them to certain classes & methods. 首先,您有一个路由文件,它将所有请求发送到您的站点并将它们分派给某些类和方法。

// Create a filter for checking if a URL is working
Route::filter('urlCheck', function()
{
    $file_headers = @get_headers("https://www.google.co.uk/");
    if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
        // return a 404 page
    }

    // continue with the normal response
});

// Create a route for a basic request to http://example.com/test
Route::get('test', array('before' => 'urlCheck', function() {
    // create page for successful request to GET /test
}));

But I want to make sure that if one of the 3(a,b,c) doesn't equal the corresponding 3(x,y,z) nothing is echoed. 但我想确保如果3(a,b,c)中的一个不等于相应的3(x,y,z),则不会回显任何内容。

is fulfilled by this: 通过以下方式实现:

if (a == x && b == y && c == z) {
    echo "all match";
}

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

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